#!/bin/sh
#    
# Script name: install
#
# $Id: install,v 3.0 1999/08/20 22:04:20 ironfoot Exp $
#
# Copyright (c) 1999  Michael Merten <mikemerten@yahoo.com>
#
# License: This script is distributed under the terms of version 2 
#          of the GNU GPL. See the LICENSE file included with this package.
#
# This will install the config file, the script and the manpage
# into the appropriate directories with the proper permissions.
#
WKDIR=`pwd`
BINDIR=/usr/local/sbin
MANDIR=/usr/local/man/man8
CRONDIR=/etc/cron.weekly
#
# check for the -f flag
if [ "$1" = "-f" ]; then
    FORCE=yes
else
    FORCE=
fi
#
# test for installed packages
echo -e "\nLooking for the necessary programs..."
STOP=
for pkg in dpkg-name dpkg-scanpackages dpkg-scansources bc rsync; do
    if [ -n "`which $pkg`" ]; then
       echo "found: $pkg"
    else
       echo "MISSING: $pkg"
       STOP=yes
    fi
done
if [ "$STOP" = "yes" ]; then
    echo -e "\nYou are missing some of the necessary programs."
    echo "You cannot use apt-move without them... Aborting install."
    exit 1
fi

#
# test for files in the install locations
#
echo -e "\nLooking for existing files..."
#
# check for config file
if [ -f /etc/apt-move.conf ]; then
    echo "Found old config file... backing it up..."
    mv /etc/apt-move.conf /etc/apt-move.conf.old
    echo "Building new config file..."
    . $WKDIR/apt-move.conf
    . /etc/apt-move.conf.old
    CF=/etc/apt-move.conf
    echo '#  Configuration file for the apt-move script.' > $CF
    echo '#  Automatically generated by the apt-move install script.' >> $CF
    echo '#  Your previous apt-move.conf file was renamed to ' >> $CF
    echo '#  apt-move.conf.old.  You should modify the following ' >>$CF
    echo '#  configuration to suit your system.  See the apt-move(8) ' >>$CF
    echo '#  manpage for more information. '>>$CF
    echo '#' >> $CF
    echo "ARCH=$ARCH" >> $CF
    echo "LOCALDIR=$LOCALDIR" >> $CF
    echo "DIST=$DIST" >> $CF
    echo "PKGTYPE=$PKGTYPE" >> $CF
    echo "SECTIONS="'"'$SECTIONS'"' >> $CF
    echo "USSITE=$USSITE" >> $CF
    echo "NONUSSITE=$NONUSSITE" >> $CF
    echo "FILECACHE=$FILECACHE" >> $CF
    echo "DELETE=$DELETE" >> $CF
    echo "MAXDELETE=$MAXDELETE" >> $CF
    echo "LOGFILE=$LOGFILE" >> $CF
    echo "MONITOR=$MONITOR" >> $CF
    echo "Configuration file created... "
    echo "   remember to check /etc/apt-move.conf before using apt-move!"
else
    /usr/bin/install -m 0644 $WKDIR/apt-move.conf /etc/apt-move.conf
    echo "Installed default configuration file..."
    echo "   remember to check /etc/apt-move.conf before using apt-move!"
fi

STOP=
#
# check for old files that should be removed
for file in $BINDIR/mirror $BINDIR/runmirror $BINDIR/runsync $MANDIR/runmirror.8.gz $MANDIR/runsync.8.gz; do
    if [ -f $file ]; then
        echo "Found obsolete file: $file"
        resp=yes
        [ $FORCE ] || read -p "Do you want to delete it? [yes] :" resp
        if [ "$resp" = "no" -o "$resp" = "NO" -o "$resp" = "n" -o "$resp" = "N" ]; then
            echo -e "    Leaving $file alone.\n"
        else
            echo -e "    Removing $file\n"
            rm -f $file
        fi
    fi
done

for file in $BINDIR/apt-move $MANDIR/apt-move.8.gz $CRONDIR/apt-move; do
    if [ -f $file ]; then
        echo "Found file: $file"
        resp=yes
        [ $FORCE ] || read -p "OK to overwrite it? [yes]:" resp
        if [ "$resp" = "no" -o "$resp" = "NO" -o "$resp" = "n" -o "$resp" = "N" ]; then
            echo -e "    Leaving: $file \n"
            STOP=yes
            break
        else
            echo -e "    Overwriting: $file \n"
            rm -f $file
        fi
    fi
done

if [ $STOP ]; then
    echo -e "\nInstallation could not be completed because some of the"
    echo -e "target files already exist on your system."
    echo -e "You need to remove or rename them before running this script."
    echo -e "Aborting."
    exit 1
fi

#
echo -e "\nLooks ok, installing files..."
/usr/bin/install -d $BINDIR $MANDIR
/usr/bin/install -m 0755 $WKDIR/apt-move $BINDIR/apt-move
/usr/bin/install -m 0644 $WKDIR/apt-move.8 $MANDIR/apt-move.8
gzip -f $MANDIR/apt-move.8
/usr/bin/install -m 0755 $WKDIR/apt-move.cron $CRONDIR/apt-move
#
echo -e "\nFinished Installation!"
echo "If anything failed, fix the problem and run this script again."
echo -e "\nRemember to edit /etc/apt-move.conf before using apt-move!\n\n"
exit 0
