#!/bin/bash
# bish bash wallop - set up some autopackage stuff!

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

VERSION="0.2.6"

function lilac() { echo -en "\033[1;35m"; }
function cyan() { echo -en "\033[1;36m"; }
function normal() { echo -en "\033[m\017"; }
function red() { echo -en "\033[1;31m"; }
function green() { echo -en "\033[1;32m"; }

# if user not located in autopackage/main directory - add directories from $0 to prog_dir
dirname="${0//.\//}"
if [ `expr index "$dirname" '/'` -gt "0" ]; then
    scriptname=`basename "$dirname"`
    scriptname_length=`expr length "$scriptname"`
    temp_length=`expr length "$dirname"`
    let "end_index=$temp_length-$scriptname_length-1"
    prog_dir="${dirname:0:$end_index}"
    prog_dir="$PWD/$prog_dir"
else
    prog_dir="$PWD"
fi

echo "autopackage $VERSION setup script"
cyan; echo "- Mike Hearn (c) 2002 The autopackage project"; normal;
echo
echo "This script will:"
echo " * Check for required version of getopt"
echo " * Create directory for packages as /var/packages"
echo " * Create symlinks from /usr/local/bin to the executables"
echo "     in $prog_dir"
echo " * Create symlink from /usr/local/share/autopackage"
echo "     to $prog_dir/share"
echo " * Create configuration file as /etc/autopackage"
echo "     - By default, autopackage will update the linker cache if you install to"
echo "       a non-standard prefix. You can turn this off in the config file."
echo " * rsync the skeletons database from autopackage.org - make sure your net"
echo "   connection is active and rsync is installed"
echo
echo "If you don't want to do any of these things, just press Ctrl-C now. You can"
echo "easily edit the script to take out the actions you don't want, and this script"
echo "is not required to run autopackage, though a bit of tweaking may be required."
echo
echo "You can undo the effects of this script by running unsetup at any time."
echo
echo "To continue, just press enter";
read

if [[ `id -u` != "0" ]]; then
    red; echo "You must run this script as root"; normal;
    exit 1
fi

getopt -T
if [[ $? -ne 4 ]]; then
    red;
    echo You have an old version of the getopt program.
    echo autopackage requires the enhanced version of getopt to run correctly
    normal;
    exit 1;
fi

echo -n "Setting up packages directory ... "
rm -rf /var/packages 2>/dev/null
mkdir /var/packages
green; echo "done"; normal;

echo -n "Symlinking from /usr/local/bin to the $prog_dir executables ... "
rm /usr/local/bin/apkg-info 2>/dev/null
rm /usr/local/bin/makeinstaller 2>/dev/null
rm /usr/local/bin/apkg-verify 2>/dev/null
rm /usr/local/bin/apkg-uninstall 2>/dev/null
rm /usr/local/bin/package 2>/dev/null
ln -s "$prog_dir/makeinstaller" /usr/local/bin/makeinstaller 2>/dev/null
ln -s "$prog_dir/package" /usr/local/bin/package 2>/dev/null
green; echo "done"; normal;

echo -n "Symlinking from /usr/local/share/autopackage to $prog_dir/share ... "
rm /usr/local/share/autopackage 2>/dev/null
ln -s "$prog_dir/share" /usr/local/share/autopackage 2>/dev/null

green; echo "done"; normal;

echo -n "Editing and saving configuration file as /etc/autopackage ... "
####
#### You should UPDATE THE VERSION NUMBER when you add/remove required variables to the config file
####

cat <<EOF >/etc/autopackage

autopackage_config_version="1"

# autopackage configuration file
# this is a script that will be sourced by most of the autopackage tools.
# use with care! syntax errors here can cause autopackage to break

autopackage_prefix="%PREFIX%"
autopackage_version="%VERSION%"
autopackage_global_db_location="/var/packages"
autopackage_user_db_location="\$HOME/.packages"

# if "true" (watch the case!) then the linker cache will be updated when a package is installed to a non standard prefix
autopackage_update_linker_cache="true"
autopackage_update_manpath="true"

# restrictions on what autopackage can do (useful for when users don't have root access, ie on networks)
autopackage_deny_install="false"   # set this to "true" to prevent installations entirely
autopackage_deny_user_install="false"   # set this to "true" to prevent user (non-root) installs

# default prefix to install packages to (when the package itself does not require a specific prefix)
# Other examples include:
# autopackage_default_prefix="/opt" - all packages install to /opt by default
# autopackage_default_prefix="/apps/\\$SHORT_NAME" - all packages install to their own directory, for instance /apps/foobar

autopackage_default_prefix="/usr/local"

EOF



config=`cat /etc/autopackage`
config=`echo "$config" | sed 's|%PREFIX%|/usr/local|g'`
config=`echo "$config" | sed "s|%VERSION%|$VERSION|g"`
echo "$config" > /etc/autopackage

green; echo "done"; normal;
echo
echo "Now run ./refresh-skels as a normal user, to resync the skeletons database. You'll need rsync installed."
echo "If rsync cannot connect, join #autopackage on freenode and tell us, sometimes rsyncd dies for no apparent reason :("

## DONE
echo
