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

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


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

getopt -T
if [[ $? -ne 4 ]]; then
	out "$intl_SETUP_UPGRADE_GETOPT"
	echo
	exit 1
fi

_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="$1"
		shift
		;;

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

	# Unrecognized parameters
	*)
		shift 1
		;;

	esac
done

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

_removeAutopackage

[ ! "$autopackage_silent" ] && out "$intl_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/config"
cp -f "$apkg_dir/share/apkg-bashlib"		"$prefix/share/autopackage/apkg-bashlib"
cp -f "$apkg_dir/share/apkg-db"			"$prefix/share/autopackage/apkg-db"
cp -f "$apkg_dir/share/apkg-defs"		"$prefix/share/autopackage/apkg-defs"
cp -f "$apkg_dir/share/apkg-dep"		"$prefix/share/autopackage/apkg-dep"
cp -f "$apkg_dir/share/apkg-failsafelib"	"$prefix/share/autopackage/apkg-failsafelib"
cp -f "$apkg_dir/share/apkg-funclib"		"$prefix/share/autopackage/apkg-funclib"
cp -f "$apkg_dir/share/apkg-io"			"$prefix/share/autopackage/apkg-io"
cp -f "$apkg_dir/share/apkg-script-utils"	"$prefix/share/autopackage/apkg-script-utils"
cp -f "$apkg_dir/share/apkg-ttyfe"		"$prefix/share/autopackage/apkg-ttyfe"
[ -d "$apkg_dir/share/locale" ] && cp -fr "$apkg_dir/share/locale" "$prefix/share/"
cp -f "$apkg_dir/share/remove"			"$prefix/share/autopackage/remove"
cp -f "$apkg_dir/libexec/autosu-gtk"		"$prefix/libexec/autopackage/autosu-gtk"
cp -f "$apkg_dir/libexec/autosu-helper"		"$prefix/libexec/autopackage/autosu-helper"
cp -f "$apkg_dir/libexec/autosu-tui"		"$prefix/libexec/autopackage/autosu-tui"
cp -f "$apkg_dir/libexec/dirisempty"		"$prefix/libexec/autopackage/dirisempty"
cp -f "$apkg_dir/libexec/dumpverdefs"		"$prefix/libexec/autopackage/dumpverdefs"
cp -f "$apkg_dir/libexec/freespace"		"$prefix/libexec/autopackage/freespace"
cp -f "$apkg_dir/libexec/libcurl.so.2.0.2"	"$prefix/libexec/autopackage/libcurl.so.2.0.2"
cp -f "$apkg_dir/libexec/libuau.so.3.0.0"	"$prefix/libexec/autopackage/libuau.so.3.0.0"
cp -f "$apkg_dir/libexec/luau-downloader"	"$prefix/libexec/autopackage/luau-downloader"
cp -f "$apkg_dir/libexec/luau-downloader.bin"	"$prefix/libexec/autopackage/luau-downloader.bin"
cp -f "$apkg_dir/libexec/vfolder-magic"		"$prefix/libexec/autopackage/vfolder-magic"
cp -f "$apkg_dir/libexec/readlink"		"$prefix/libexec/autopackage/readlink"
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

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

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

[ ! "$autopackage_silent" ] && out "$intl_SETUP_FINISH"


# link to autopackage functions and environment
[ -e /etc/autopackage/config ] && source /etc/autopackage/config;
[ -e ${XDG_CONFIG_HOME:-~/.config}/autopackage/config ] && source ${XDG_CONFIG_HOME:-~/.config}/autopackage/config;

# 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

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


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