#!/bin/sh
# Installation script for Serendipity products for the Unix platforms
#

cd `dirname "$0"`
CURRDIR=`pwd`
TESTPRINTS="$CURRDIR/../testprn"
ISUPGRADE=0
INSTALLDIR=''

# Try a Serendipity Blackmagic product
ARCHIVE=`find "$CURRDIR" -name 'blackmagic-*.tar.*'`
PRODUCTNAME="Serendipity Blackmagic"

# Try a Serendipity Megarip product
if [ "$ARCHIVE" = '' ];
then {
    ARCHIVE=`find "$CURRDIR" -name 'megarip-*.tar.*'`
    PRODUCTNAME="Serendipity Megarip"
}
fi

# Try a Serendipity Republish product
if [ "$ARCHIVE" = '' ];
then {
    ARCHIVE=`find "$CURRDIR" -name 'republish-*.tar.*'`
    PRODUCTNAME="Serendipity Republish"
}
fi

if [ "$ARCHIVE" = '' ];
then {
    echo "Could not find installation archive"
    exit 10
}
fi

DEFAULTINSTALLDIR="$HOME/$PRODUCTNAME"

echo "Welcome to the Serendipity Unix installer"
echo "When prompted for an installation directory:"
echo ""
echo "       either enter an exsting installation directory to perform an upgrade"
echo "       or enter a new directory to install a fresh version."
echo "       If the directory does not exist it will be automatically created for you."
echo ""
echo "For example entering /apps/$PRODUCTNAME would perform an upgrade if the product existed"
echo "or would create the directory /apps/$PRODUCTNAME if no product was previosuly installed"
echo ""

OK=1
while [ $OK = 1 ]
do {
    echo "Enter Installation folder (default is $DEFAULTINSTALLDIR) : "
    read INSTALLDIR
    if [ "$INSTALLDIR" = '' ];
    then {
	INSTALLDIR="$DEFAULTINSTALLDIR"
    }
    fi
    if [ "$INSTALLDIR" != '' ];
    then {
	if [ -d "$INSTALLDIR" ];
	then {
	    OK=0
	}
	else {
	    mkdir -p "$INSTALLDIR"
	    if [ $? = 0 ];
	    then {
		OK=0
	    }
	    else {
		echo " "
	    }
	    fi
	}
	fi
    }
    fi
}
done

if [ -d "$INSTALLDIR/lib/ifilters" ];
then {
    ISUPGRADE=1
}
fi

if [ $ISUPGRADE = 1 ] ;
then {
    echo "Found and existing product, an upgrade will be performed"
}
fi

echo "Performing installation of $PRODUCTNAME"

cd "$INSTALLDIR"
if [ $? != 0 ];
then {
    echo "Could not change to $INSTALLDIR Has it been deleted ?"
    exit 10
}
fi

zcat "$ARCHIVE" | tar xfp -
if [ $? != 0 ];
then {
    echo "Installation of $PRODUCTNAME failed or was interrupted by the user"
    exit 10
}
fi

if [ -d "$TESTPRINTS" ];
then {
    cd "$TESTPRINTS"
    echo "Copying testprints"
    cp -pf * "$INSTALLDIR/lib/testprints"
    if [ $? != 0 ];
    then {
	echo ""
	echo "warning: Some of the testprints failed to copy across"
    }
    fi
}
fi

echo ""
echo "Installation was successful"
echo "$PRODUCTNAME has been installed in $INSTALLDIR"


