#!/bin/sh

#############################################################################
#
# This Gallery installation script has been developed by Jesper Knudsen
# at http://sme.swerts-knudsen.dk
#
# It installs libtiff and netpgm as well as Gallery
#
# Revision History:
#
# Jan 1, 2004: 	Initial revision history.
# Feb 23, 2004: Updated to support release 1.4.2 of Gallery as well as update
#		to jhead
# May 2, 2004:  Updated to support command line parameter indicating which
#		ibays to install into.
# May 3, 2004:	Updated to version 1.4.3
# July 6, 2004: Added support for download of RPMs from mirror site
#               sme.swerts-knudsen.com if primary site is not
#               responding
# Dec 30,2004:  Updated to gallery-1.4.4-pl4.tar.gz
#############################################################################

cls()
{   
for (( i=1 ; i<35 ; i++ ))
do
echo ""
done
}

cls
echo "========================================================"
echo "= Gallery Installation Script                           ="
echo "=                                                      ="
echo "= This script will install Gallery in an ibay          ="
echo "= on your system.                                      ="
echo "=                                                      ="
echo "= No express or implied warranties are provided and its="
echo "= usage is at your own risk.                           ="
echo "=                                                      ="
echo "= If you feel confortable with the above then press    ="
echo "= enter if not pres Ctrl+C to abort the installation   ="
echo "= script.                                              ="
echo "=                                                      ="
echo "========================================================"
read x
cls

if [ $1 ]
then
echo "Using install ibay as specified on command line = \"$1\""
GAL=$1
else
echo "No Install ibay specified using default \"gallery\""
GAL="gallery"
fi

if [ -d /home/e-smith/files/ibays/$GAL ]
then

PRIMARY=http://sme.swerts-knudsen.com
SECONDARY=http://sme.swerts-knudsen.dk

# determine download server....
mkdir downloadtest
cd downloadtest
wget -q --timeout=4 $PRIMARY/index.html
if [ -f index.html ]
then
# download from primary site OK
SITE=$PRIMARY
else
wget -q --timeout=4 $SECONDARY/index.html
if [ -f index.html ]
then
# download from mirror site
SITE=$SECONDARY
fi
fi
echo "Downloading RPMs and files from $SITE"
cd ..
rm -rf downloadtest

rpm -qa > rpmlist

#First install all the required RPMs

if [ `grep -c libtiff-3 rpmlist` -eq 0 ]
then 
rpm -Uvh $SITE/downloads/Gallery/libtiff-3.5.7-2.i386.rpm
fi

if [ `grep -c netpbm-9 rpmlist` -eq 0 ]
then
rpm -Uvh $SITE/downloads/Gallery/netpbm-9.24-9.73.4.legacy.i386.rpm
fi

if [ `grep -c netpbm-progs-9 rpmlist` -eq 0 ]
then
rpm -Uvh $SITE/downloads/Gallery/netpbm-progs-9.24-9.73.4.legacy.i386.rpm
fi

rm -rf rpmlist

#Then install jhead
cd /usr/bin 
wget -N $SITE/downloads/Gallery/jhead
chmod a+x /usr/bin/jhead 

#Now gallery itself
cd /home/e-smith/files/ibays/$GAL/ 
wget -N $SITE/downloads/Gallery/gallery-1.5.1.tar.gz
tar xzf gallery-1.5.1.tar.gz
rm -f gallery-*
cd gallery
mv * ../html 
cd ..
rmdir gallery 
cd html 
mkdir albums 
mkdir temp
chmod -R 777 /home/e-smith/files/ibays/$GAL/html/albums
chmod -R 777 /home/e-smith/files/ibays/$GAL/html/temp
rm -f index.html

echo "Allow gallery to use jhead from an iBay"
/sbin/e-smith/db accounts setprop $GAL PHPBaseDir /home/e-smith/files/ibays/$GAL/:/usr/bin 
/sbin/e-smith/signal-event ibay-modify $GAL 

# Now notify Swerts-Knudsen that you have installed - only so that I can enhance 
# the scripts that are most in use                 

ERR=`wget -q -t 1 -T 2 --delete-after http://sme.swerts-knudsen.dk/cgi-bin/egometer?Gallery_Script`

#enable configuration
sh configure.sh 

else
echo "Ibay called \"$GAL\" does not exist - Installation terminated!"
echo "Please use the Server-Manager and create one."
fi



