#!/bin/sh

. /lib/functions.sh

SMART=$(which smart)
RPM=$(which rpm)
repo_add_log="/tmp/srm_repo_add.log"
repo_del_log="/tmp/srm_repo_del.log"
repo_local_log="/tmp/srm_repo_local.log"
repo_status_log="/tmp/srm_repo_status.log"
rpm_add_log="/tmp/srm_rpm_add.log"
rpm_del_log="/tmp/srm_rpm_del.log"
DOWNLOADDIR="/tmp/rpmrepo"
UCI_CFG="/etc/config/rpm_repo"


config_cb() {
    local cfg_type="$1"
    local cfg_name="$2"
    local option
    case "$cfg_type" in
    repos)
        append REPOS "$cfg_name" "$N"
    ;;
    esac
}


#$1 repo name
#$2 repo type
#$3 repo url
add_uci_cfg()
{
    local cfg
    for cfg in $REPOS; do
        name=$(uci_get rpm_repo $cfg name)
        if  [ "$1" = "$name" ]; then
            uci_remove rpm_repo $cfg
        fi
    done
    uci_commit rpm_repo
    rm -f /tmp/.uci/rpm_repo > /dev/null 2>&1

    uci_add rpm_repo repos ""; repo_cfg="$CONFIG_SECTION"
    uci_set rpm_repo $repo_cfg name $1
    uci_set rpm_repo $repo_cfg type $2
    uci_set rpm_repo $repo_cfg url  $3
    uci_commit rpm_repo
    rm -f /tmp/.uci/rpm_repo > /dev/null 2>&1

}

#$1 repo name
del_uci_cfg()
{
    local found=0
    local cfg
    for cfg in $REPOS; do
        name=$(uci_get rpm_repo $cfg name)
        if  [ "$1" = "$name" ]; then
            uci_remove rpm_repo $cfg
            found=1
        fi
    done
    uci_commit rpm_repo
    rm -f /tmp/.uci/rpm_repo > /dev/null 2>&1

    if [ "$found" = "1" ]; then
        return 0
    else
        echo "Can not find $1 in uci config file"
        return 1
    fi
}

srm_repo_status()
{
    local name="$@"
    $SMART channel --show "${name}" > ${repo_status_log} 2>&1
    cat ${repo_status_log} | grep "warning" | grep "not found" > /dev/null 2>&1
    return $?
}

srm_repo_get_oldurl()
{
    local name="$@"
    oldurl=`$SMART channel --show "${name}" | grep baseurl | awk '{; print $3;}'`
}

srm_repo_add()
{
    local name="$1"
    local url="$2"
    local type="rpm-md"

    if [ "${name}" = "localrpmrepo" ]; then
        echo "localrpmrepo is name for local repository, please change another name for your remote repository."
        return 3
    fi

    srm_repo_status "${name}"
    if [ "$?" -eq 0 ]; then
        $SMART channel -y --add "${name}" type="${type}" baseurl="${url}/" > ${repo_add_log} 2>&1
        if [ "$?" -eq 0 ]; then
            $SMART update "${name}" >> ${repo_add_log} 2>&1
	    if [ "$?" -ne 0 ]; then
            $SMART channel -y --remove "${name}" >> ${repo_add_log} 2>&1
		echo "Updating remote repository ${name} error when adding repository"
		return 2
	    fi
        else
	    echo "Adding remote repository ${name} error"
	    return 1
        fi
    else
	srm_repo_get_oldurl "${name}"
        $SMART channel -y --set "${name}" baseurl="${url}/" > ${repo_add_log} 2>&1
        if [ "$?" -eq 0 ]; then
            $SMART update "${name}" >> ${repo_add_log} 2>&1
	    if [ "$?" -ne 0 ]; then
            $SMART channel -y --remove "${name}" >> ${repo_add_log} 2>&1
            $SMART channel -y --add "${name}" type="${type}" baseurl="${oldurl}/" > ${repo_add_log} 2>&1
		echo "Updating remote repository ${name} error when adding repository"
		return 2
	    fi
        else
	    echo "Adding remote repository ${name} error"
	    return 1
        fi
    fi

    echo "Adding remote repository ${name} successfully"
    if [ -e "$UCI_CFG" ]; then
        add_uci_cfg ${name} ${type} ${url}
    fi
    return 0
}

srm_repo_del()
{
    local name="$@"

    srm_repo_status "$name"
    if [ "$?" -eq 1 ]; then
        $SMART channel -y --remove "${name}" > ${repo_del_log} 2>&1
        if [ "$?" -ne 0 ]; then
            echo "Removing repository ${name} error"
            return 1
        else
            echo "Removing repository ${name} successfully"
            if [ -e "$UCI_CFG" ]; then
                del_uci_cfg ${name}
                if [ "$?" = "0" ]; then
                    return 0
                else
                    return 1
                fi
            fi
        fi
    else
        echo "Repository ${name} is not exist"
	return 2
    fi
}

srm_repo_local()
{
    local LOCAL_RPM_DIR="$1"

    if [ ! -d ${LOCAL_RPM_DIR} ]; then
        echo "Directory ${LOCAL_RPM_DIR} is not exist"
        return 2
    fi

    srm_repo_status "localrpmrepo"

    if [ "$?" -eq 0 ]; then
        $SMART channel -y --add localrpmrepo type=rpm-dir name="default rpm channel" path="${LOCAL_RPM_DIR}" > ${repo_local_log} 2>&1
    else
        $SMART channel -y --set localrpmrepo path="${LOCAL_RPM_DIR}" > ${repo_local_log} 2>&1
    fi
    if [ "$?" -ne 0 ]; then
        echo "Adding local repository ${name} error"
        return 1
    else
        echo "Adding local repository ${name} successfully"
        if [ -e "$UCI_CFG" ]; then
            add_uci_cfg "localrpmrepo" "rpm-dir" ${LOCAL_RPM_DIR}
        fi
        return 0
    fi
}

srm_repo_list()
{
    echo "Below is all repositories on the target:"
    $SMART channel --list
    return $?
}

#List rpm package more information
srm_list_pkg_more()
{
    ${RPM} -qai 2>/dev/null
}

srm_install_rpm()
{
    pkg_ver=${1##*/}
    pkg=${pkg_ver%%.*}
    if [ "$pkg" != "$pkg_ver" ]; then
        pkg=${pkg%-*}
    fi

    for find in `rpm -qa | grep ${pkg} >${rpm_add_log} 2>&1`
    do
        find=${find%%.*}
        find=${find%-*}
	if [ "${find}" = "${pkg}" ];then
            echo "package $1 already installed"
            return 4
        fi

    done
    ls -l ${1}* >>${rpm_add_log} 2>&1
    if [ "$?" != 0 ]; then
        [ -d ${DOWNLOADDIR} ] || mkdir ${DOWNLOADDIR}
        if [ "${1##*.}" = "rpm" ]; then
            m=${1%.rpm}
            arch=${m##*.}
            pkg=${m%.*}
            pkg_arch=${pkg}@${arch}
            smart download --target=${DOWNLOADDIR} ${pkg_arch} >>${rpm_add_log} 2>&1
        else
            smart download --target=${DOWNLOADDIR} $1 >>${rpm_add_log} 2>&1
        fi
        if [ "$?" != 0 ]; then
            echo "Download error"
            return 1
        else
            ${RPM} -i ${DOWNLOADDIR}/* >>${rpm_add_log} 2>&1
            if [ "$?" = 0 ]; then
                rm -rf ${DOWNLOADDIR} >>${rpm_add_log} 2>&1
                echo "install $1 successfully"
                return 0
            else
                rm -rf ${DOWNLOADDIR} >>${rpm_add_log} 2>&1
                echo "install $1 error"
                return 2
            fi
        fi
    fi
    ${RPM} -i ${1}* >>${rpm_add_log} 2>&1
    if [ "$?" != 0 ]; then
        echo "install $1 error"
        return 3
    fi
    echo "install $1 successfully"
    return 0
}
srm_delete_rpm()
{
    pkg_ver=${1##*/}
    pkg=${pkg_ver%%.*}
    if [ "$pkg" != "$pkg_ver" ]; then
        pkg=${pkg%-*}
    fi
    ${RPM} -qa | grep ${pkg} >${rpm_del_log} 2>&1
    if [ "$?" != 0 ]; then
        echo "package $1 is not installed"
        return 1
    fi
    ${RPM} -e ${pkg} >>${rpm_del_log} 2>&1
    if [ "$?" != 0 ]; then
        echo "delete package $1 failed"
        return 1
    fi
    echo "delete package $1 successfully"
    return 0
}
srm_list_rpm()
{
   echo "Below is all rpm packages on the target:"
   ${RPM} -qa 2> /dev/null
   if [ "$?" != 0 ]; then
       echo "List package(s) failed"
       return 1
   fi
   return 0
}
#List rpm package more information
srm_list_rpm_more()
{
    srm_list_pkg_more
    if [ $? = 0 ]; then
        echo "List package(s) more information successfully"
        return 0
    else
        echo "List package(s) more information failed"
        return 1
    fi
}

Usage()
{
    echo "Usage:spm_repo    --addremote  <Repo Name> <Repo URL>"
    echo "                  --deleterepo <Repo Name>"
    echo "                  --addlocal   <Local RPM Dir>"
    echo "                  --listrepo"
    echo "                  --installrpm <Package Name>"
    echo "                  --deleterpm  <Package Name>"
    echo "                  --listrpm (--verbose)"
    exit 1;
}

if [ ! -e "$SMART" ]; then
    echo "Command \"smart\" does not exist!"
    exit 1
fi

if [ ! -e "$RPM" ]; then
    echo "Command \"rpm\" does not exist!"
    exit 1
fi

if [ -e "$UCI_CFG" ]; then
    uci_load rpm_repo
fi

case $1 in
    --addremote)
        repo_name="$2"
        repo_url="$3"
        [ -z "${repo_name}" ] || [ -z "${repo_url}" ] && Usage;
        srm_repo_add "${repo_name}" "${repo_url}";;
    --deleterepo)
        shift
        repo_name="$@"
        [ -z "${repo_name}" ] && Usage;
        srm_repo_del "${repo_name}";;
    --addlocal)
        shift
        [ -z "$@" ] && Usage;
        srm_repo_local "$@";;
    --listrepo)
        shift
        srm_repo_list "$@";;
    --installrpm)
        shift
        rpm_name="$@"
        [ -z "${rpm_name}" ] && Usage;
        srm_install_rpm "${rpm_name}";;
    --deleterpm)
        shift
        rpm_name="$@"
        [ -z "${rpm_name}" ] && Usage;
        srm_delete_rpm "${rpm_name}";;
    --listrpm)
	    if [ "$#" = "1" ]; then
	        srm_list_rpm
	    elif [ "$#" = "2" -a "$2" = "--verbose" ]; then
	        srm_list_rpm_more
        else
            Usage
	fi;;
    *)
        Usage;;
esac
