#!/bin/bash
# GPack Package Manager
# Package Manager Interface
# $Id: gpack,v 1.10 2005/07/04 00:34:46 nymacro Exp $

########################################################################
#
# Copyright 2005
# Aaron Marks.
#
# GPack 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, or (at your option) any
# later version.
#
# GPack 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 GPack; see the file COPYING.  If not, write to the Free
# Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
########################################################################

if ! . ./gpack.sh ; then
    echo "ERROR: Could not find GPack internals (gpack.sh)."
    exit 1
fi

ARGS=(`echo $@`)

for (( i=0 ; $i < $# ; i=$i+1 )) ; do
    case "${ARGS[$i]}" in
	--no-deps|-n)
	    warn "Disabling dependancy checking"
	    FORCE_NO_DEPS=yes
	    unset ARGS[$i]
	    ;;
	--overwrite|-o)
	    warn "Force overwrite enabled"
	    FORCE_OVERWRITE=yes
	    unset ARGS[$i]
	    ;;
	--keep|-k)
	    warn "Keep existing files on system enabled"
	    FORCE_KEEP=yes
	    unset ARGS[$i]
	    ;;
	*)
	    if [ -z "$COMMAND" ]; then
		COMMAND=${ARGS[$i]}
		unset ARGS[$i]
	    fi
	    ;;
    esac
done

case "$COMMAND" in
    "build")
	for i in "${ARGS[@]}"; do
	    pkg_build `pkg_find $i`
	done
	;;
    "install")
	for i in "${ARGS[@]}"; do
	    pkg_install `pkg_find_bin $i`
	done
	;;
    "remove")
	for i in "${ARGS[@]}"; do
	    pkg_remove $i
	done
	;;
    "depinst")
	for i in "${ARGS[@]}"; do
	    pkg_depinst `pkg_find_bin $i`
	done
	;;
    "info")
	for i in "${ARGS[@]}"; do
	    pkg_info `pkg_find $i`
	done
	;;
    "sync")
	echo "Syncing packages not implemented yet"
	;;
    "list")
	ls -1 $PKG_FILE_DIR | grep $PKG_FILE | sed -e "s|\.$PKG_FILE||"
	;;
    "installed")
	ls -1 $PKG_CONF_DIR
	;;
    "clean")
	for i in "${ARGS[@]}"; do
	    rm `pkg_find_bin $i`
	done
	;;
    "rebuild")
	for i in "${ARGS[@]}"; do
	    sh $0 clean $i
	    sh $0 build $i
	done
	;;
    "find")
	for i in "${ARGS[@]}"; do
	    pkg_find $i
	done
	;;
    *)
	echo "GPack $VERSION - Aaron Marks 2005 - See COPYING for license details"
	echo "usage:  $0 <command> [<argument>*]"
	echo "    commands:"
	echo "        build             build package"
	echo "        install           install package"
	echo "        remove            remove package"
	echo "        depinst           install package and all dependancies"
	echo "        info              print package information"
	echo "        list              list available packages"
	echo "        installed         list installed packages"
	echo "        find              find package"
	echo "        rebuild           build a package from scratch"
	echo "        clean             clean up a package"
	echo
	echo "    options:"
	echo "        -o --overwrite    Overwrite existing files in installation"
	echo "        -k --keep         Keep existing files on system"
	echo "        -n --no-deps      Disable dependancy checking"
	;;
esac
