#!/bin/sh
#
# Copyright (C) 2012  Etersoft
# Copyright (C) 2012  Vitaly Lipatov <lav@etersoft.ru>
#
# This file 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
# (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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

PROGDIR=$(dirname $0)

load_helper()
{
    [ -r "$PROGDIR/$1" ] || fatal "Have no $PROGDIR/$1 helper file"
    . $PROGDIR/$1
}

load_helper epm-sh-functions

set_sudo

check_tty

#############################

phelp()
{
	echo "$Descr
$Usage
 Commands:
$(get_help HELPCMD)

 Options:
$(get_help HELPOPT)
"
}

print_version()
{
        echo "EPM package manager version @VERSION@"
        echo "Running on $($DISTRVENDOR) ('$PMTYPE' package manager uses '$PKGFORMAT' package format)"
        echo "Copyright (c) Etersoft 2012"
        echo "This program may be freely redistributed under the terms of the GNU GPL."
}

Usage="Usage: epm [options] <command> [package name(s), package files]..."
Descr="epm - EPM package manager"

set_pm_type

verbose=
quiet=
nodeps=
force=
non_interactive=
skip_installed=
show_command_only=
epm_cmd=
pkg_files=
pkg_names=

progname="${0##*/}"
case $progname in
    epmi)
        epm_cmd=install
        ;;
    epme)
        epm_cmd=remove
        ;;
    epms)
        epm_cmd=search
        ;;
    epmqf)
        epm_cmd=query_file
        ;;
    epmqa)
        epm_cmd=packages
        ;;
    epmqp)
        epm_cmd=query_package
        ;;
    epm|upm|eepm)
        ;;
    *)
        fatal "Unknown command: $progname"
        ;;
esac

check_command()
{
    # do not override command
    [ -z "$epm_cmd" ] || return

    case $1 in
    -i|install|add)       # HELPCMD: install package(s) from remote repositories or from local file
        epm_cmd=install
        ;;
    reinstall)            # HELPCMD: reinstall package(s) from remote repositories or from local file
        epm_cmd=reinstall
        ;;
    -e|-P|remove|delete)  # HELPCMD: remove (delete) package(s) from the database and the system
        epm_cmd=remove
        ;;
    -qi|info|show)        # HELPCMD: print package detail info
        epm_cmd=info
        ;;
    requires|deplist)     # HELPCMD: print package requires
        epm_cmd=requires
        ;;
    clean)                # HELPCMD: clean local package cache
        epm_cmd=clean
        ;;
    -qa|list|packages|-l) # HELPCMD: list of installed package(s)
        epm_cmd=packages
        ;;
    -qp|query_package)    # HELPCMD: search in the list of installed packages
        epm_cmd=query_package
        ;;
    update)               # HELPCMD: update remote package repository databases
        epm_cmd=update
        ;;
    dist-upgrade|upgrade) # HELPCMD: performs upgrades of package software distributions
        epm_cmd=upgrade
        ;;
    -s|search)            # HELPCMD: search in remote package repositories
        epm_cmd=search
        ;;
    -q|installed)         # HELPCMD: check presence of package(s)
        epm_cmd=query
        ;;
    -qf|which|belongs)    # HELPCMD: query package(s) owning file
        epm_cmd=query_file
        ;;
    -sf|filesearch|search_file) # HELPCMD: search in which package a file is included
        epm_cmd=search_file
        ;;
    -ql|filelist)         # HELPCMD: print package file list
        epm_cmd=filelist
        ;;
    check|fix|verify)     # HELPCMD: check local package base integrity and fix it
        epm_cmd=check
        ;;
    checkpkg|integrity)   # HELPCMD: check package integrity
        epm_cmd=checkpkg
        ;;
    simulate)             # HELPCMD: simulate install (check requires)
        epm_cmd=simulate
        ;;
    addrepo|ar)           # HELPCMD: add package repo
        epm_cmd=addrepo
        ;;
    repolist|sl|listrepo) # HELPCMD: print repo list
        epm_cmd=repolist
        ;;
    removerepo|rr)        # HELPCMD: remove package repo
        epm_cmd=removerepo
        ;;
    *)
        return 1
    esac
    return 0
}

for opt in "$@" ; do
    check_command $opt && continue
    case $opt in
    -h|--help|help)       # HELPOPT: this help
        phelp; exit 0
        ;;
    -v|--version)         # HELPOPT: print version
        print_version; exit 0
        ;;
    --verbose)            # HELPOPT: verbose mode
        verbose=1
        ;;
    --skip-installed)     # HELPOPT: skip already install during install
        skip_installed=1
        ;;
    --show-command-only)  # HELPOPT: show command only, do not any action
        show_command_only=1
        ;;
    --quiet)              # HELPOPT: quiet mode (do not print commands before exec)
        quiet=1
        ;;
    --nodeps)             # HELPOPT: skip dependency check (during install/simulate and so on)
        nodeps="--nodeps"
        ;;
    --force)              # HELPOPT: force install/remove package (f.i., override)
        force="--force"
        ;;
    --auto)               # HELPOPT: non interactive mode
        non_interactive=1
        ;;
    *)
        if [ -f "$opt" ] ; then
            pkg_files="$pkg_files $opt"
        else
            pkg_names="$pkg_names $opt"
        fi
        ;;
    esac
done

pkg_files=$(strip_spaces "$pkg_files")
pkg_names=$(strip_spaces "$pkg_names")

pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")

echover "pkg_files=$pkg_files"
echover "pkg_names=$pkg_names"

# Run helper for command
if [ -z "$epm_cmd" ] ; then
    print_version
    echo
    fatal "Run $ epm --help for get help"
fi

load_helper epm-$epm_cmd
epm_$epm_cmd
# return last error code