#!/bin/bash
# Remove script for Autopackage Support Code

###
#
# 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-2004 Curtis Knight (knighcl@fastmail.fm)
#
###


##########
###
### Autopackage Framework
###
##########

# setup XDG configuration variables scoped for autopackage
#
#   AUTOPACKAGE_CONFIG_HOME     User configuration directory
#   AUTOPACKAGE_CONFIG_DIRS     System configuration directories
#   AUTOPACKAGE_CONFIG_DIR      Determined system configuration directory for autopackage

if [ -z "$AUTOPACKAGE_CONFIG_HOME" ]; then
    export AUTOPACKAGE_CONFIG_HOME
    if [ -z "$XDG_CONFIG_HOME" ]; then
        AUTOPACKAGE_CONFIG_HOME="$HOME/.config"
    else
        AUTOPACKAGE_CONFIG_HOME="$XDG_CONFIG_HOME"
    fi
fi

if [ -z "$AUTOPACKAGE_CONFIG_DIRS" ]; then
    export AUTOPACKAGE_CONFIG_DIRS
    if [ -z "$XDG_CONFIG_DIRS" ]; then
        AUTOPACKAGE_CONFIG_DIRS="/etc/xdg"
    else
        AUTOPACKAGE_CONFIG_DIRS="$XDG_CONFIG_DIRS"
    fi
fi

if [ -z "$AUTOPACKAGE_CONFIG_DIR" ]; then
    export AUTOPACKAGE_CONFIG_DIR
    _AUTOPACKAGE_CONFIG_DIRS=$( echo "$AUTOPACKAGE_CONFIG_DIRS" | tr ':' ' ' )
    for _CONFIGURATION_DIR in $_AUTOPACKAGE_CONFIG_DIRS; do
        if [ -e "$_CONFIGURATION_DIR/autopackage/config" ]; then
            AUTOPACKAGE_CONFIG_DIR="$_CONFIGURATION_DIR"
            break
        fi
    done
    unset _AUTOPACKAGE_CONFIG_DIRS
fi

# set defaults that might not be loaded from system configurations
export autopackage_deny_user=false

# load system configurations
[ -e /etc/autopackage/config ] && source /etc/autopackage/config;
[ -e "$AUTOPACKAGE_CONFIG_DIR/autopackage/config" ] && source "$AUTOPACKAGE_CONFIG_DIR/autopackage/config";

# load user configuration if allowed from system configuration
if ! $autopackage_deny_user; then
    [ -e "$AUTOPACKAGE_CONFIG_HOME/autopackage/config" ] && source "$AUTOPACKAGE_CONFIG_HOME/autopackage/config";
fi

# probe configuration file to locate autopackage/main directory
apkg_main_dir="$autopackage_prefix"

# link to autopackage functions and environment
# sideload by changing into dir to source files within that directory
oPWD=`pwd`
cd "$apkg_main_dir/share/autopackage"
source "apkg-funclib"
cd "$oPWD"


##########
###
### Determine directories
###
##########

# Determine configuration directory by user
etc_dir=`_userConfig`


##########
###
### Setting runtime parameters
###
##########

while [ $# -gt 0 ]
do
	case "$1" in

	# Silent operation
	--silent)
		autopackage_silent=true
		shift
		;;

	# Unrecognized parameters
	*)
		shift
		;;

	esac
done


##########
###
### Autopackage Banner
###
##########

if ! "$autopackage_silent"; then
 	out "$intl_PACKAGE_VERSION" "$autopackage_version remove tools script"
	out "
--------------------------------------------------------------------------
  autopackage - the distro neutral packaging framework for Linux systems
--------------------------------------------------------------------------
"
 	out "      Press any key to REMOVE autopackage tools or Ctrl-C to CANCEL."
	read
fi


##########
###
### Remove Environment
###
##########

! "$autopackage_silent" && out "Removing Autopackage version $autopackage_version ..."

# remove translation machine objects
if [ -d "$apkg_main_dir/share/locale" ]; then
    cd "$apkg_main_dir/share/locale"
    for d in `ls -1`; do
        if [ -d "$d" ]; then
            [ -f "$d/LC_MESSAGES/autopackage.mo" ] && rm -f "$d/LC_MESSAGES/autopackage.mo"
            # this can delete the present directory - cover up the error when this happens
            removeDir "$d/LC_MESSAGES" &>/dev/null
        fi
    done
    cd "$oPWD"
fi
[ -d "$apkg_main_dir/share/locale" ] && removeDir "$apkg_main_dir/share/locale"

removeFile "$apkg_main_dir/bin/package"
removeDir "$apkg_main_dir/bin"
removeFile "$etc_dir/autopackage/config"
removeDir "$etc_dir/autopackage"
# do not use remove functions as we are removing ourselves
rm -rf  "$apkg_main_dir/share/autopackage" && rmdir "$apkg_main_dir/share" &>/dev/null
rm -rf  "$apkg_main_dir/libexec/autopackage" && rmdir "$apkg_main_dir/libexec" &>/dev/null
rmdir "$apkg_main_dir" &>/dev/null

! "$autopackage_silent" && out "Autopackage is removed and completed."

### DONE
! "$autopackage_silent" && echo
exit 0
