#!/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)
#
###

# some initial values, these should be subbed in by setup
[ -e /etc/autopackage ] && source /etc/autopackage;
[ -e ~/.autopackage ] && source ~/.autopackage;

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

[ ! checkConfigVersion ] && exit 1;

executed_from_directory=`pwd`

initDB

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

if [[ "$1" == "info" ]] || [[ "$1" == "query" ]]; then
    # we currently only support querying the package database & package files
    # TODO: query package in autopackage network
    if [[ "$2" == "" ]]; then
	red; outn "$intl_FAIL"; normal; out "$intl_QUERY_MUST_SPECIFY" "$2";
	exit 1;

    # query a package file
    elif [[ "$2" == "-p" ]]; then	
	if [[ "$3" == "" ]]; then
	    red; outn "$intl_FAIL"; normal; out "$intl_QUERY_MUST_SPECIFY";
	    exit 1;
	elif [ ! -e "$3" ]; then
	    red; outn "$intl_FAIL"; normal; out "$intl_QUERY_PACKAGE_NOT_FOUND" "$3";
	    exit 1;
	fi
	
        ## get the length of the header, and subtract the shebang line and the skipLines line itself
	header_length=`head -n 2 "$3" | grep skipLines | awk ' BEGIN { FS="=" } { print $2 } '`
	if [[ "$header_length" = "" ]]; then
	    red; outn "$intl_FAIL"; normal; out "$intl_QUERY_PACKAGE_INVALID_FORMAT" "$3"
	    exit 1
	fi
	(( header_length = header_length - 1 ))
	
	out "$intl_PACKAGE_INFO_SHORTNAME" "`head -n $header_length \"$3\" | grep SHORTNAME | sed 's/# SHORTNAME //' `"
	out "$intl_PACKAGE_INFO_DISPLAYNAME" "`head -n $header_length \"$3\" | grep DISPLAYNAME | sed 's/# DISPLAYNAME //' `"
	out "$intl_PACKAGE_INFO_ROOTNAME" "`head -n $header_length \"$3\" | grep ROOTNAME | sed 's/# ROOTNAME //' `"
	out "$intl_PACKAGE_INFO_VERSION" "`head -n $header_length \"$3\" | grep SOFTWARE-VERSION | sed 's/# SOFTWARE-VERSION //' `"

    # query the package database (user first, then global)
    elif [ -e "$autopackage_user_db_location/$2" ]; then
	out "$intl_PACKAGE_INFO_SHORTNAME" "` grep \"ShortName: \" \"$autopackage_user_db_location/$2/info\" | sed 's/^ShortName: //' `"
	out "$intl_PACKAGE_INFO_DISPLAYNAME" "`grep \"DisplayName: \" \"$autopackage_user_db_location/$2/info\" | sed 's/^DisplayName: //' `"
	out "$intl_PACKAGE_INFO_ROOTNAME" "`grep \"RootName: \" \"$autopackage_user_db_location/$2/info\" | sed 's/^RootName: //' `"
	out "$intl_PACKAGE_INFO_VERSION" "`grep \"SoftwareVersion: \" \"$autopackage_user_db_location/$2/info\" | sed 's/^SoftwareVersion: //' `"

    elif [ -e "$autopackage_global_db_location/$2" ]; then
	out "$intl_PACKAGE_INFO_SHORTNAME" "` grep \"ShortName: \" \"$autopackage_global_db_location/$2/info\" | sed 's/^ShortName: //' `"
	out "$intl_PACKAGE_INFO_DISPLAYNAME" "`grep \"DisplayName: \" \"$autopackage_global_db_location/$2/info\" | sed 's/^DisplayName: //' `"
	out "$intl_PACKAGE_INFO_ROOTNAME" "`grep \"RootName: \" \"$autopackage_global_db_location/$2/info\" | sed 's/^RootName: //' `"
	out "$intl_PACKAGE_INFO_VERSION" "`grep \"SoftwareVersion: \" \"$autopackage_global_db_location/$2/info\" | sed 's/^SoftwareVersion: //' `"

    # the specified package isn't installed
    else
	red; outn "$intl_FAIL"; normal; out "$intl_QUERY_PACKAGE_NOT_INSTALLED" "$2";
    fi
    exit 0

elif [[ "$1" == "uninstall" ]] || [[ "$1" == "remove" ]]; then
    shift;
    if [[ "$1" == "-f" ]]; then
      force="force";
      shift;
    fi
    if [[ "$@" == "" ]]; then red; outn "$intl_FAIL"; normal; out "$intl_UNINSTALL_MUST_SPECIFY"; fi;

    # process the package names
    for arg do
	outn "$intl_UNINSTALL_UNINSTALLING" "$arg"
	uninstallPackage "$arg" $force
	r=$?
	if [[ $r == 0 ]]; then
	    green; echo "$intl_DONE"; normal;
	elif [[ $r == 1 ]]; then
	    red; out "$intl_UNINSTALL_PACKAGE_NOT_FOUND"; normal;
	elif [[ $r == 2 ]]; then
	    red; out "$intl_FAILED";
	    cyan; out "$intl_ERRORS"; normal;
	    echo "$uninstall_result";
	elif [[ $r == 3 ]]; then
	    red; out "$intl_FAILED"; normal;
	    out "$intl_UNINSTALL_WOULD_BREAK"; echo "$uninstall_result"; echo;
	    out "$intl_UNINSTALL_DO_IT_ANYWAY"; echo;
	elif [[ $r == 4 ]]; then
	    red; out "$intl_FAILED"; normal;
	    out "$intl_UNINSTALL_NOT_AUTOPACKAGE";
	elif [[ $r == 5 ]]; then
	    red; out "$intl_FAILED"; normal;
	    out "$intl_UNINSTALL_MUST_BE_ROOT";
	elif [[ $r == 6 ]]; then
	    red; out "$intl_FAILED"; normal;
	    out "$intl_UNINSTALL_NO_SUDO";
	else
	    assertNotReached
	fi
    done


    exit $r
elif [[ "$1" == "install" ]]; then
    shift;
    if [[ "$@" == "" ]]; then red; outn "$intl_FAIL"; normal; out "$intl_INSTALL_MUST_SPECIFY"; fi;

    for arg do
        # if retrieve found the file, try to install
        if r=`retrieve "$arg"`; then            
            if /bin/bash "$r"; then
		green; out "$intl_INSTALL_SUCCESS" $arg; normal;
	    else
		red; out "$intl_INSTALL_ERROR" $arg; normal;
		exit 1
	    fi        
        else
          red; out "$intl_INSTALL_PACKAGE_NOT_FOUND" $arg; normal;
          continue;
        fi

    done
    exit 0
    
elif [[ "$1" == "verify" ]]; then
    shift;
    if [[ "$@" == "" ]]; then red; outn "$intl_FAIL"; normal; out "$intl_APKG_VERIFY_MUST_SPECIFY"; fi;
    for arg do
	out "$intl_APKG_VERIFY_VERIFYING" $arg
	result=`verifyPackage "$arg"`
	r=$?
	if [[ $r -eq 1 ]]; then
	    red; outn "$intl_FAIL"; normal; out "$intl_APKG_VERIFY_PACKAGE_NOT_FOUND" $arg
	elif [[ $r -eq 2 ]]; then
	    red; outn "$intl_FAIL"; normal; out "$intl_APKG_VERIFY_PACKAGES_ARE_BROKEN"
	    echo "$result"
	    cyan; out "SELF-REPAIR GOES HERE! :)"; normal;
	elif [[ $r -eq 0 ]]; then
	    out "$intl_APKG_VERIFY_SUCCESS";
	fi
    done

    exit $r
else
    red; outn "$intl_FAIL"; normal; out "$intl_PACKAGE_UNKNOWN_COMMAND" "$1"
    exit 1;
fi
