#!/bin/bash
# This is the main CLI interface to autopackage

###
#
# This code 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 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright 2002 Mike Hearn (mike@theoretic.com)
#
###

[ -e /etc/autopackage/config ] && source /etc/autopackage/config;
[ -e ~/.config/autopackage/config ] && source ~/.config/autopackage/config;

source "$autopackage_prefix/share/autopackage/apkg-funclib"

[ ! checkConfigVersion ] && exit 1;

executed_from_directory=`pwd`

# initialize variables
_initializeAutopackage

##
# systemInfo
#
# Prints out system configuration information about host machine to file and to
# screen. Use with trace output file to debug efficiently. File output is to the
# respective packages location. Trace output is in packages/logs.
function systemInfo() {

	locateCommand kde-config "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand bash "-version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand gnome-config "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand pkg-config "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand gnome-panel "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand info "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand gettext "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand wc "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand grep "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand perl "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand awk "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	locateCommand sed "--version"
	_systemInfo "$lc_command" "$lc_location" "$lc_output"

	_systemInfo "glibc" "/lib/libc.so.6" "`/lib/libc.so.6`"

	echo "System configuration information saved to file: $AUTOPACKAGE_DEBUG_LOGFILE"
	echo "System configuration information saved to file: $AUTOPACKAGE_DEBUG_LOGFILE" >> "$AUTOPACKAGE_DEBUG_LOGFILE"

	return 0
}

# do we have any arguments at all?
if [ $# -eq "0" ]; then
	out "$intl_PACKAGE_HELP"
	exit 1
fi


case "$1" in
	"-h" | "--help" | "help") # help mode
		out "$intl_PACKAGE_HELP"
		exit 0
		;;

	"-v" | "--version" | "version") # version mode
		out "$intl_PACKAGE_VERSION" "$autopackage_version"
		exit 0
		;;

	"-s" | "--system" | "system") # system mode
		systemInfo
		exit $?
		;;

	"-l" | "--list" | "list") # list mode
		_listPackages
		exit $?
		;;

	"-f" | "--files" | "files") # filelist mode
		shift
		_listPackageFiles "$@"
		exit $?
		;;

	"-g" | "--log" | "log") # log mode
		shift
		_listPackageLog  "$@"
		exit $?
		;;

	"info" | "query") # info mode
		# we currently only support querying the package database & package files
		# TODO: query package in autopackage network
		if [[ "$2" == "" ]]; then
			_packageReturnCode "7"
			exit "$?";
		# query the package database (user first, then global)
		elif _loadPackage "$2"; then
			_packageInfo "$2"
		# the specified package isn't installed
		else
			_packageReturnCode "1" "$2"
		fi
		exit 0
		;;

	"remove" | "uninstall") # remove mode
		shift;
		if [[ "$@" == "" ]]; then _packageReturnCode "7"; exit "$?"; fi;

		# process the package names from text lines in declared file
		if [[ "$1" == "-F" ]]; then
			shift;
			remove_filename="$1"
			if [ ! -r "$remove_filename" ]; then
				_packageReturnCode "10" "$remove_filename"
				exit "$?";
			fi
			while read;
			do
				line=`echo "$REPLY"`
				outn "$intl_UNINSTALLING" "$line"
				uninstallPackage "$line"
			done <"$remove_filename"
		elif [[ "$1" == "-f" ]]; then
			force="force";
			shift;
		fi

		# process the command line package names
		if [ ! "$remove_filename" ]; then
			for arg do
				outn "$intl_UNINSTALLING" "$arg"
				uninstallPackage "$arg" $force
			done
		fi
		exit $r
		;;

	"install") # install mode
		shift;
		if [[ "$@" == "" ]]; then _packageReturnCode "7"; exit "$?"; fi;

		# process the package names from text lines in declared file
		if [[ "$1" == "-F" ]]; then

			shift;
			install_filename="$1"
			if [ ! -r "$install_filename" ]; then
				_packageReturnCode "10" "$install_filename"
				exit "$?";
			fi
			while read;
			do
				line=`echo "$REPLY"`
				# if retrieve found the file, try to install
				if r=`retrieve "$line"`; then
					if bash "$r"; then
						_packageReturnCode "12" "$line"
					else
						_packageReturnCode "13" "$line"
						exit "$?"
					fi
				else
					_packageReturnCode "1" "$line"
					continue;
				fi
			done <"$install_filename"

		else

			for arg do
				# if retrieve found the file, try to install
				if r=`retrieve "$arg"`; then
					if bash "$r"; then
						_packageReturnCode "12" "$arg"
					else
						_packageReturnCode "13" "$arg"
						exit "$?"
					fi
				else
					_packageReturnCode "1" "$arg"
					continue;
				fi
			done

		fi
		exit 0
		;;

	"verify") # verify mode
		shift;
		if [[ "$@" == "" ]]; then _packageReturnCode "7"; exit "$?"; fi;
		export _context="verify"
		export autopackage_pipe="${TMP}/apkg-fifo$$"
		rm -f "$autopackage_pipe" # in case we get an old crashed pid
		mkfifo "$autopackage_pipe"
		(
		    waitForHELLO
		    for arg do
			trace verifying $arg
			identify "$arg" "0" "verify"
			result=`verifyPackage "$arg"`
			r=$?
			if [[ $r -eq 2 ]]; then
				## what gives this result?!?
				outputFail "$intl_PACKAGE_BROKEN"
				#out "$result" ## CONVERT TO SUMMARY VIEW
				err "self repair goes here ;)"
			elif [[ $r -eq 0 ]]; then
				_output "DISPLAY-SUMMARY
verify
success

"
			fi
		    done
		    terminateFE
		    exit $r
		) &

		( _launchFE "$autopackage_pipe" )
		;;

	*) # default mode [ALL]
		_packageReturnCode "15" "$1"
		exit "$?";
		;;

esac
