#!/bin/bash
# Install 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-2003 Curtis Knight (knighcl@fastmail.fm)
#
###


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

# locate directory and make absolute reference
apkg_dir=`dirname "$0"`
apkg_dir=`cd "$apkg_dir" 2>&-; pwd`

# load installed autopackage support code
# otherwise load installing support code

# 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

# link to autopackage functions if found
# otherwise sideload by changing into installing directory to source files
if [ -e "$autopackage_prefix/share/autopackage/apkg-funclib" ]; then
    source "$autopackage_prefix/share/autopackage/apkg-funclib"
else
    oPWD_install=`pwd`
    cd "$apkg_dir/share"
    source "../etc/config"
    source "apkg-funclib"
    cd "$oPWD_install"
fi


##########
###
### Checks
###
##########

_checkVersionRequirements


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

# Determine installation prefix directory by user
prefix=`_apkgPrefix`

# Determine configuration directory by user
etc_dir=`_userConfig`

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

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

	# Adjust prefix location
	--prefix)
		prefix="$2"
		shift 2
		;;

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

	# Unrecognized parameters
	*)
		shift 1
		;;

	esac
done

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

_removeAutopackage

"$autopackage_silent" || out "Installing Autopackage..."

mkdir -p "$prefix/bin"
mkdir -p "$prefix/libexec/autopackage"
mkdir -p "$prefix/share/autopackage"
mkdir -p "$etc_dir/autopackage"

set +e
cp -f "$apkg_dir/package"               "$prefix/bin/package"
cp -f "$apkg_dir/etc/config"            "$etc_dir/autopackage/"
cp -f "$apkg_dir/libexec"/*             "$prefix/libexec/autopackage/"
cp -f "$apkg_dir/share"/apkg-*          "$prefix/share/autopackage/"
cp -fr "$apkg_dir/share/locale"         "$prefix/share/"
cp -f "$apkg_dir/share/remove"          "$prefix/share/autopackage/"
pushd "$prefix/libexec/autopackage" >/dev/null
linkFile --silent --nolog libuau.so.3.0.0 libuau.so.3
linkFile --silent --nolog libcurl.so.2.0.2 libcurl.so.2
popd >/dev/null
# FIXME: should NOT be commented - this is done so that some return code does not fail
# the install and stop the auto-setup code before gtkfe can be installed.
#set -e

# if we have write access to the linker cache, refresh it (ie if we are root)
if [ -w /etc/ld.so.cache ]; then
    echo -n "Refreshing linker cache, please wait ... "
    /sbin/ldconfig
    echo "done"
fi

_refresh_again=false

# install libgcc_s.so.1 if needed
if ! /sbin/ldconfig -p | grep -q libgcc_s.so.1 && [ ! -e "$prefix/lib/libgcc_s.so.1" ]; then
    echo "Your system is missing the GCC support library, putting a copy in $prefix/lib"
    mkdir -p "$prefix/lib"
    cp "$apkg_dir/libexec/libgcc_s.so.1" "$prefix/lib/libgcc_s.so.1"
    _refresh_again=true
fi

if ! /sbin/ldconfig -p | grep -q libstdc++.so.5 && [ ! -e "$prefix/lib/libstdc++.so.5" ]; then
    echo "Your system is missing v5 of the C++ support library, putting a copy in $prefix/lib"
    mkdir -p "$prefix/lib"
    cp "$apkg_dir/libexec/libstdc++.so.5.0.2" "$prefix/lib/libstdc++.so.5.0.2.apkg"
    ln -s "$prefix/lib/libstdc++.so.5.0.2.apkg" "$prefix/lib/libstdc++.so.5"
    _refresh_again=true
fi

if ! /sbin/ldconfig -p | grep -q libstdc++.so.6 && [ ! -e "$prefix/lib/libstdc++.so.6" ]; then
    echo "Your system is missing v6 of the C++ support library, putting a copy in $prefix/lib"
    mkdir -p "$prefix/lib"
    cp "$apkg_dir/libexec/libstdc++.so.6.0.5" "$prefix/lib/libstdc++.so.6.0.5.apkg"
    ln -s "$prefix/lib/libstdc++.so.6.0.5.apkg" "$prefix/lib/libstdc++.so.6"
    _refresh_again=true
fi

if $_refresh_again && [ -w /etc/ld.so.cache ]; then
    echo -n "Refreshing linker cache again, please wait ... "
    /sbin/ldconfig
    echo "done"
fi

# sub in autopackage prefix into config file
safeSed "$etc_dir/autopackage/config" "s|%AutopackagePrefix%|$prefix|g"

"$autopackage_silent" || out "Autopackage setup is finished and completed."


# load installed autopackage support code
[ -e /etc/autopackage/config ] && source /etc/autopackage/config;
[ -e ${XDG_CONFIG_HOME:-~/.config}/autopackage/config ] && source ${XDG_CONFIG_HOME:-~/.config}/autopackage/config;

# link to autopackage functions - required for upgrading installation session
if [ -e "$autopackage_prefix/share/autopackage/apkg-funclib" ]; then
    source "$autopackage_prefix/share/autopackage/apkg-funclib"
fi

# add marker file to denote that this is a first time installation session
# of the support code; _initializeAutopackage converts this to variable
# _autopackage_support_install=1
touch "$WORKING_DIRECTORY/apkg-support-install"

# initialize autopackage variables (cpu_architecture)
_initializeAutopackage

# install GTK+ graphical front end
# set variable to install in same location as support code
export first_autosu="true"
_installGTKFE
r="$?"

# unset variable to ask user where to install selected packages
unset first_autosu

# return value in case GTK+ graphical front end fails
if [[ "$r" != "0" ]]; then
    exit 5
fi

# add to PATH if necessary
if ! isInList "$prefix/bin" "$PREFIX"; then
    updateEnv PATH "$prefix/bin"
    out "Please run 'exec bash' in each terminal to update your path.";
    out " "
fi


### DONE
"$autopackage_silent" || echo
exit 0
