#!/bin/bash
#-------------------------------------------------------------------------------#
#
# MPM (Meta Package Manager)
# Copyright (C) 2024-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#-------------------------------------------------------------------------------

command="$1"
arg="$2"



if [[ -f /etc/mpm.conf ]]
then
    . /etc/mpm.conf
else
    MPM_REPO="https://www.rx3.net/mpm-repository"
fi

VERSION=1.1.0





#-----------------------------------------------------------------------------------------------------------------------------------
# Exec CMD
#-----------------------------------------------------------------------------------------------------------------------------------

exec_cmd()
{
    cmd="$1"


    if [[ "${verbose}" == "true" ]]
    then
	echo "${cmd}" 1>&2
    fi

    if [[ "${dry_run}" != "true" ]]
    then
	eval "${cmd}"
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM Help
#-----------------------------------------------------------------------------------------------------------------------------------

function mpm_version_print()
{
    echo "MPM Version: [${VERSION}]"
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM Help
#-----------------------------------------------------------------------------------------------------------------------------------

function mpm_help_print()
{
    mpm_version_print
    echo "Repository URL: [${MPM_REPO}]"
    echo "usage: mpm [-h | --help] | [-V | --version] | [-l | --list] | [-i | --install <mpm_name>] [-T | --test] [-v | --verbose] [-r | --repo <repository_url>]"
}





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

function mpm_args_parse()
{
        mode="default"
     verbose="false"
     dry_run="false"

    
    tmp_args=$(getopt -o hi:lr:TvV --long help,install:,list,repo:,test,verbose,version -- "$@")

    if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

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

    while true ; do
	case "$1" in
	    # Path options
            -r|--repo)                       shift;        MPM_REPO="$1";                         shift;;

	    # Options

	    # Mode switches
	    -h|--help)                                         mode="exit";    mpm_help_print;    shift;;
            -i|--install)                     shift;           mode="install"; mpm_name="$1";     shift;;
	    -l|--list)                                         mode="list";                       shift;;
	    -V|--version)                                      mode="exit";    mpm_version_print; shift;;

	    # Global options
            -T|--test)                                      dry_run="true";                       shift;;
            -v|--verbose)                                   verbose="true";                       shift;;

	    #   
	    --)                                                                                   shift;  break;;
	    *) echo "args_parse internal error [$1] !"; exit 1;;
	esac
    done
    
    if [[ "${mode}" == "default" ]]
    then
        echo "Help, Install, List or Version should invoked!"

        mode="exit"
        mpm_help_print

        exit 1
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM Meta Package List
#-----------------------------------------------------------------------------------------------------------------------------------

function mpm_mpkg_list()
{
    if [[ "${MPM_REPO}" =~ ^https?:// ]]
    then
        echo "$(curl -L 2>/dev/null "${MPM_REPO}" | grep -e '\.mpm</a>' | sed -e 's/.*href="//' -e 's/\.mpm".*//')"
    else
        echo "$(ls ${MPM_REPO}/*.mpm | sed -e "s%^${MPM_REPO}/%%" -e 's/\.mpm$//')"
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM Meta Package Tag Read
#-----------------------------------------------------------------------------------------------------------------------------------

function mpm_mpkg_tag_read()
{
    mpkg="$1"
     tag="$2"
     
    if [[ "${MPM_REPO}" =~ ^https?:// ]]
    then
        echo "$(curl -L 2>/dev/null "${MPM_REPO}/${mpkg}.mpm" | grep -e "${tag}: " | sed -e "s/^${tag}: //")"
    else
        echo "$(cat 2>/dev/null "${MPM_REPO}/${mpkg}.mpm" | grep -e "${tag}: " | sed -e "s/^${tag}: //")"
    fi
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM Meta Package Description
#-----------------------------------------------------------------------------------------------------------------------------------

function mpm_mpkg_desc()
{
    mpkg="$1"

    mpm_mpkg_tag_read "${mpkg}" "PKG_DESC"
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM Meta Package List
#-----------------------------------------------------------------------------------------------------------------------------------

function mpm_mpkg_pkglist()
{
    mpkg="$1"

    mpm_mpkg_tag_read "${mpkg}" "PKG_LIST"
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM List
#-----------------------------------------------------------------------------------------------------------------------------------

function mpm_list()
{
    echo "            MPM Name | MPM Description                  | Package List                                                              "
    echo "---------------------+----------------------------------+---------------------------------------------------------------------------"
    
    mpm_mpkg_list | while read mpkg
    do
        desc="$(mpm_mpkg_desc $mpkg)"
        pkglist="$(mpm_mpkg_pkglist $mpkg)"

        printf "%20s | %-32s | %s\n" "${mpkg}" "${desc}" "${pkglist}"
    done
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM Install
#-----------------------------------------------------------------------------------------------------------------------------------

function mpm_install
{
    mpkg=$1

    if [[ "${mpkg}" == "" ]]
    then
        echo "missing required mpkg name"
        mpm_help

        exit 1
    fi

    
    mpm_mpkg_list | grep -e "^${mpkg}$" -e "^${mpkg} " -e " ${mpkg}$" -e " ${mpkg} " 2>&1 >/dev/null

    if [[ "$?" == 1 ]]
    then
        echo "mpkg [${mpkg}] not found!"
        
        exit 1
    fi

    
    pkglist="$(mpm_mpkg_pkglist $mpkg)"
    
    if [[ "${pkglist}" == "" ]]
    then
        echo "package list is empty!"
        
        exit 1
    fi

    echo "installing MPM: [$mpkg]:[${pkglist}]"

    exec_cmd "urpmi --force ${pkglist}"
}





#-----------------------------------------------------------------------------------------------------------------------------------
# MPM Main
#-----------------------------------------------------------------------------------------------------------------------------------

shopt -s extglob

mpm_args_parse "$@"



if [[ ${mode} == "exit" ]]
then
    exit 0
fi



echo "mode: [${mode}]   verbose: [${verbose}]   dry_run: [${dry_run}]   Repo_URL: [${MPM_REPO}]" 1>&2

case "${mode}"
in
    "list")
        mpm_list
    ;;
    
    "install")
        mpm_install "${mpm_name}"
    ;;   
esac

exit 0
