# -*-shell-script-*-

# generic functions library, which imports all the other

###
#
# 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)
# Copyright 2002,2003 Hongli Lai (h.lai@chello.nl)
#
###


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

source "$autopackage_prefix/share/autopackage/apkg-defs"
source "$autopackage_prefix/share/autopackage/apkg-bashlib"
source "$autopackage_prefix/share/autopackage/apkg-failsafelib"
source "$autopackage_prefix/share/autopackage/apkg-io"
source "$autopackage_prefix/share/autopackage/apkg-dep"
source "$autopackage_prefix/share/autopackage/apkg-script-utils"
source "$autopackage_prefix/share/autopackage/apkg-db"
trace funclib: imports completed

# will test the dependancies of package $1 and print the root names of the failed deps
function verifyPackage() {
    if ! name=`resolveName "$1"`; then return 1; fi
    # now get its dependancies .....
    if [[ -e "$autopackage_db_location/$name/dependancies" ]]; then
	deps=`cat "$autopackage_db_location/$name/dependancies"`
	# iterate through each dependancy
	vp_c=`getLineCount "$deps"`
	vp_i=1
	while (( vp_i <= vp_c )); do
	    # run the test, and ensure it returns 0
	    vp_d=$( getLine "$deps" $vp_i )
	    vp_d=$( justRootName "$vp_d" )
	    if ! checkForPackage $vp_d --prefix "$autopackage_db_location"; then
		echo $vp_d;
		return 2; 
	    fi
	    (( vp_i += 1 ));
	done
    else
	# otherwise if no dependancies return 0
	return 0;
    fi    
        
}

##
# reverseLog
#
# Do the opposite of what's described in the install log.
# In other words: this function automatically uninstalls what has been installed.
#
# Example:
# # This is part of an autopackage specfile
# [Script-Install]
# copyFiles share/* "$prefix/share"
# installLib lib/*
#
# [Script-Uninstall]
# reverseLog
function reverseLog() {
    eval "$log"
}

# return of 0 = uninstalled ok
# return of 1 = package not found/database corrupt
# return of 2 = completed with errors
# return of 3 = this package supports some others: listed in uninstall_result (unless $2 = force)
# return of 4 = this package is in the database but wasn't installed by autopackage
# return of 5 = this package was installed by root and you're not root, so piss off
# return of 6 = this package is in the user db, but you're running as root
function uninstallPackage() {
    # first ensure we have the root name
    local name
    if ! name=`resolveName "$1"`; then return 1; fi
    trace name resolved to $name

    # the name may have been resolved to a database we can't/shouldn't actually access.
    
    # firstly, if we are root, check that the package is in the global database. it could be in the user db if the user ran package
    # using sudo, but we don't want to touch the user db as the root user, otherwise things get complicated.
    if [[ `id -u` == "0" ]] && [ -e "$autopackage_user_db_location/$name" ]; then
	return 6;
    fi

    # next, if we are not root and the package exists in the global db but not the user db, we can't proceed
    if [ -e "$autopackage_global_db_location/$name" ] && [ ! -e "$autopackage_user_db_location/$name" ] && [[ `id -u` != "0" ]]; then
	return 5;
    fi
    
    # Check we don't support anything
    if [[ `echo x; cat "$autopackage_db_location/$name/supports" 2>/dev/null` != "x" ]] && [[ "$2" == "" ]]; then
	export uninstall_result=`cat "$autopackage_db_location/$name/supports"`
	return 3;
    fi
    
    if [[ ! -e "$autopackage_db_location/$name/info" ]]; then return 4; fi

    # now get the script section
    trace retrieving scripts....
    local script=`getSection "$autopackage_db_location/$name"/info Script-Uninstall`
    local log=`getSection "$autopackage_db_location/$name"/info Log`
    local meta=`getSection "$autopackage_db_location/$name"/info Meta`
    local shortname=`getKey "$meta" ShortName`
    local softwareversion=`getKey "$meta" SoftwareVersion`
    local part_name=`echo "$name" | awk 'BEGIN { FS="/" } { print $1 }'` # only the first part (up until the /)
    export uninstall_result=`(
        trace sourcing prefixes file
        source "$autopackage_db_location/$name/prefixes" # import prefixes
        trace evaluating script
        trace "$script"
	eval "$script" 

	# remove support entries from dependancies
	unforgeDependancies "$name"
	# now remove package info and links
	pushd $autopackage_db_location/$name/ >/dev/null
	rm info files prefixes
	[ -e files ] && rm files
	[ -e dependancies ] && rm dependancies
        [ -e supports ] && rm supports
	popd >/dev/null
	prune "$autopackage_db_location/$name" >/dev/null

	if [[ $( ls -1 "$autopackage_db_location/$shortname" 2>/dev/null ) == "" ]]; then
	  rm -f "$autopackage_db_location/$shortname"
	  rm -f "$autopackage_db_location/${shortname}-${softwareversion}"
	fi

    )`
    # result contains the output of stderr
    if [[ "$uninstall_result" != "" ]]; then return 2; else return 0; fi
}

function checkConfigVersion() {
    if [[ "$autopackage_config_version" != "1" ]]; then
	red; outn "$intl_FAIL"; normal;
	out "$intl_BAD_CONFIG_VERSION";
	return 1;
    fi
    return 0;
}
