#!/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-2005 Mike Hearn (mike@navi.cx)
#
###

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

if [ -e "$autopackage_prefix/share/autopackage/apkg-funclib" ]; then
	source "$autopackage_prefix/share/autopackage/apkg-funclib"
else
	# do not use support functions as they are probably not available
	echo; echo "The support code could not find its installation directory.

Remove the support code files from these possible locations and re-install.
       file: /usr/bin/package
       file: ~/.local/bin/package
       file: /usr/bin/makeinstaller
       file: ~/.local/bin/makeinstaller
  directory: /usr/share/autopackage/
  directory: ~/.local/share/autopackage/
  directory: /usr/libexec/autopackage/
  directory: ~/.local/libexec/autopackage/
  directory: /etc/autopackage
  directory: ~/.config/autopackage
"
	exit 1
fi

[ ! 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
		shift
		_listPackages "$1"
		exit $?
		;;

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

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

	"info" | "query") # info mode
		# we currently only support querying the package database & package files
		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
		;;


	"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
