#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# URPMI Setup
# 
# Copyright (C) 2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
# 
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; If not, see
# <https://www.gnu.org/licenses/>.
# 
#-----------------------------------------------------------------------------------------------------------------------------------



#-----------------------------------------------------------------------------------------------------------------------------------
# Includes
#-----------------------------------------------------------------------------------------------------------------------------------

: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/base.bash"





#-----------------------------------------------------------------------------------------------------------------------------------
# Global Variables
#-----------------------------------------------------------------------------------------------------------------------------------

declare -g            VERSION="1.0.2"
declare -g               NAME="URPMI Setup"
declare -g               HELP="usage: urpmi-setup [ [-d | --distrib <Distrib>] [-m | --mirror-mga <Mirror_List>] [-r | --mirror-rx3 <Mirror_List>] ] | [-h | --help] | [-V | --version] [ [ -D | --dump]  | [-T | --test] ] [-u | --update] [-v | --verbose] ) ]"

declare -g        DISTRIB_DEF="9"

declare -g          EXIT_CODE=0
declare -g               MODE="DEFAULT"
declare -g            DISTRIB=""
declare -g     MIRRORLIST_MGA=""
declare -g     MIRRORLIST_RX3=""
declare -g             UPDATE="FALSE"





#-----------------------------------------------------------------------------------------------------------------------------------
# MirrorList Init
#-----------------------------------------------------------------------------------------------------------------------------------

function us_mirrorlist_init()
{
    if [[ "${MIRRORLIST_MGA}" == "" ]]
    then
        MIRRORLIST_MGA="http://mirror.xor.rx3/mageia/distrib/${DISTRIB}/x86_64 https://mirror.rx3.net/mageia/distrib/${DISTRIB}/x86_64 ftp://ftp.proxad.net/mirrors/mageia.org/distrib/${DISTRIB}/x86_64 http://distrib-coffee.ipsl.jussieu.fr/pub/linux/Mageia/distrib/${DISTRIB}/x86_64"
    fi
    
    if [[ "${MIRRORLIST_RX3}" == "" ]]
    then
        MIRRORLIST_RX3="http://mirror.xor.rx3/rx3/distrib/${DISTRIB}/x86_64    https://mirror.rx3.net/rx3/distrib/${DISTRIB}/x86_64"
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Arg Parse
#-----------------------------------------------------------------------------------------------------------------------------------

function us_args_parse()
{    
    tmp_args=$(getopt -o d:Dhm:r:Tuv --long distrib:,dump,help,mirror-mga:,mirror-rx3:,test,update,verbose -- "$@")

    if [ $? != 0 ] ; then echo_error "Terminating..."; exit 1 ; fi

    # Note the quotes around `$tmp_args': they are essential!
    eval set -- "${tmp_args}"

    while true ; do
	case "$1" in
            
	    # Options
            -d|--distrib)           shift;                  DISTRIB="$1";                         shift;;
            -m|--mirror-mga)        shift;           MIRRORLIST_MGA="$1";                         shift;;
            -r|--mirror-rx3)        shift;           MIRRORLIST_RX3="$1";                         shift;;

	    # Mode switches
	    -h|--help)                                         MODE="HELP";                       shift;;
	    -V|--version)                                      MODE="VERSION";                    shift;;

	    # Global options
            -D|--dump)                                         DUMP="TRUE";                       shift;;
            -T|--test)                                      DRY_RUN="TRUE";                       shift;;
            -u|--update)                                     UPDATE="TRUE";                       shift;;
            -v|--verbose)                                   VERBOSE="TRUE";                       shift;;

	    #   
	    --)                                                                                   shift;  break;;
	    *) echo_error "args_parse internal error [$1] !"; exit 1;;
	esac
    done

    if [[ ( "${DUMP}" == "TRUE" ) && ( "${DRY_RUN}" == "TRUE" ) ]]
    then
        echo_error "Dump cannot be enabled with Test!"
        
        MODE="HELP";
        EXIT_CODE=1
    else    
        if [[ ${DISTRIB} == "" ]]
        then
            DISTRIB=${DISTRIB_DEF}
        fi

        us_mirrorlist_init
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Version Print
#-----------------------------------------------------------------------------------------------------------------------------------

us_version_print()
{
    version_print
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Help Print
#-----------------------------------------------------------------------------------------------------------------------------------

us_help_print()
{
    version_print
    help_print
}





#-----------------------------------------------------------------------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------------------------------------------------------------------

orig_args=("$@")

us_args_parse "$@"


case "${MODE}" in
            
    "EXIT")
        exit ${EXIT_CODE}
    ;;
    
    "HELP")
        us_help_print
        exit ${EXIT_CODE}
    ;;

    "VERSION")
        us_version_print
        exit ${EXIT_CODE}
    ;;
esac



echo_error "${NAME}: Mode: [${MODE}]   Verbose: [${VERBOSE}]   Dump: [${DUMP}]   Dry_Run: [${DRY_RUN}]   Distrib: [${DISTRIB}]   MirrorList_MGA: [${MIRRORLIST_MGA}]   MirrorList_RX3: [${MIRRORLIST_RX3}]   Update: [${UPDATE}]"

cmd_exec urpmi.removemedia -a
cmd_exec rm -f /var/cache/urpmi/mirrors.cache
cmd_exec urpmi.addmedia --distrib --mirrorlist "${MIRRORLIST_MGA}"
cmd_exec urpmi.addmedia --distrib --mirrorlist "${MIRRORLIST_RX3}"

for arch in "" "32bit "
do
    for media in "Core" "Nonfree" "Tainted"
    do
        for type in "Release" "Updates"
        do
            cmd_exec urpmi.update --no-ignore "${media} ${arch}${type}"
        done
    done
done

cmd_exec urpmi.update -a

if [[ "${UPDATE}" == "TRUE" ]]
then
    cmd_exec urpmi --force --auto-update
fi

exit ${EXIT_CODE}
