#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://ant.apache.org
# Packagers Diego @ Slacky.eu
# http://www.slacky.eu

NAME=apache-tomcat
VERSION=6.0.18
CHOST=i486
BUILD=1ld
CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/$NAME
ARCH=${ARCH:-i486}
SOURCE=http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.18/bin/$NAME-$VERSION.tar.gz

if [ ! -e $NAME-$VERSION.tar.gz ]; then
wget -c $SOURCE
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP
tar xvzf $CWD/$NAME-$VERSION.tar.gz

echo -e "\E[0;32m+---------------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+---------------------------------------+\E[0;0m"

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

USER=apache-tomcat
DIR_INST=/opt/$USER

chown -R root:root .

mkdir -p $PKG/$DIR_INST
cp -R * $PKG/$DIR_INST
cd $PKG/$DIR_INST

mkdir -p $PKG/etc/
ln -sf ..$DIR_INST/conf/ ../../etc/$USER

CONF=`find conf/ -name "*.*" -print`
for i in $CONF;
do
    mv $i $PKG/etc/$USER/`basename $i`.new;
done

mkdir -p $PKG/etc/profile.d
cat << EOFsh > $PKG/etc/profile.d/$USER.sh
#!/bin/sh
export CATALINA_HOME=$DIR_INST
EOFsh
cat << EOFcsh > $PKG/etc/profile.d/$USER.csh
#!/bin/csh
setenv CATALINA_HOME $DIR_INST
EOFcsh
chmod 755 $PKG/etc/profile.d/*

mkdir -p $PKG/etc/rc.d
cat << EOFrc > $PKG/etc/rc.d/rc.$USER.new
#!/bin/sh
# /etc/rc.d/rc.$USER
# Start/Stop script for the Apache-Tomcat Server

tomcat_start() {
  echo -n "Starting Apache Tomcat ... "
  su - $USER -c "\
  export JRE_HOME=/usr/lib/java; \
  export CATALINA_BASE=$DIR_INST; \
  export CATALINA_HOME=$DIR_INST; \
  export CATALINA_TMPDIR=$DIR_INST/temp; \
  $DIR_INST/bin/startup.sh" >> /var/log/$USER/server.log 2>&1
  [ $? -eq 0 ] && echo "OK" || echo "NO!"
}

tomcat_stop() {
  echo -n "Stopping Apache Tomcat ... "
  su - $USER -c "\
  export JRE_HOME=/usr/lib/java; \
  export CATALINA_BASE=$DIR_INST; \
  export CATALINA_HOME=$DIR_INST; \
  export CATALINA_TMPDIR=$DIR_INST/temp; \
  $DIR_INST/bin/shutdown.sh" >> /var/log/$USER/server.log 2>&1
  [ $? -eq 0 ] && echo "OK" || echo "NO!"
}

case "\$1" in
  'start')
    tomcat_start
  ;;
  'stop')
    tomcat_stop
  ;;
  'restart')
    tomcat_stop
    tomcat_start
  ;;
  *)
    echo "Usage: rc.$USER {start|stop|restart}"
  ;;
esac
EOFrc
chmod 755 $PKG/etc/rc.d/*

mkdir -p $PKG/var/log/
ln -sf ../..$DIR_INST/logs/ ../../var/log/$USER

mkdir -p $PKG/usr/doc/$NAME-$VERSION
mv $PKG/$DIR_INST/LICENSE \
   $PKG/$DIR_INST/NOTICE \
   $PKG/$DIR_INST/RELEASE-NOTES \
   $PKG/$DIR_INST/RUNNING.txt \
   $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/doinst.sh > $PKG/usr/doc/$NAME-$VERSION/doinst.sh
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

cd $PKG
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi

echo -e "\n"
echo -e "           \|||/          "
echo -e "           (o o)          "
echo -e "--------ooO-(_)-Ooo-------"
echo -e " by Diego @ www.slacky.eu "
echo -e "\n"
