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

# probe configuration file to locate autopackage/main directory
[ -e "/etc/autopackage/config" ] && source "/etc/autopackage/config"
[ -e ${XDG_CONFIG_HOME:-~/.config}/autopackage/config ] && source ${XDG_CONFIG_HOME:-~/.config}/autopackage/config;
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="1"
		shift
		;;

	# Unrecognized parameters
	*)
		shift
		;;

	esac
done


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

if [ ! "$autopackage_silent" ]; then
 	out "$intl_PACKAGE_VERSION" "$autopackage_version remove tools script"
 	out "$intl_APKG_BANNER"
 	out "      Press any key to REMOVE autopackage tools or Ctrl-C to CANCEL."
	read
fi


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

[ ! "$autopackage_silent" ] && out "$intl_UNINSTALLING_AUTOPACKAGE" "$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 "$intl_UNSETUP_FINISH"

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