#!/bin/sh
#
# Copyright (c) 2007, 2008 Marco A. Frias B. <marcofrias@linuxpackages.net>
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this script for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
#
# Based on Template SlackBuild written by Eric Hameleers <alien@slackware.com>
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild Script
# ===========================
#
# By:    Marco A. Frias B. <marcofrias@linuxpackages.net>
# For:   apache-tomcat
# Descr: Servlet/JSP Container
# URL:   http://tomcat.apache.org/
# Needs: jre | jdk
#
# Changelog:
#
# -- Slackware 12.1
#
# 6.0.18-3: 13/Sep/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#           Fix bugs.
#
# 6.0.18-2: 01/Sep/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#           Fix permission bugs.
#
# 6.0.18-1: 01/Aug/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#           Release new version 6.0.18.
#
# 6.0.16-2: 02/Jul/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#           Rewrite SlackBuild and rewrite rc.tomcat (include pid file).
#
# -- Slackware 12.0
#
# 6.0.16-1: 13/Feb/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#           Release new version 6.0.16 and update SlackBuild.
#
# 6.0.14-2: 14/Dec/2007 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#           Rewrite SlackBuild and add status option in rc.tomcat.
#
# 6.0.14-1: 24/Nov/2007 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#           * Initial build.
#
# NOTE:
#      Run 'sh apache-tomcat.SlackBuild --cleanup' to build a Slackware package.
# -----------------------------------------------------------------------------

## Name and version of the package
PRGNAME=apache-tomcat
VERSION=6.0.18

## The arch we will build for
ARCH=${ARCH:-noarch}

## Build tag
RELEASE=3
BUILDTAG=mfb

## The documentation files:
DOCFILES="LICENSE NOTICE RELEASE-NOTES RUNNING.txt"

## Where do we look for sources?
CWD=$(pwd)
SRCDIR=$(dirname $0)
[ "${SRCDIR:0:1}" == "." ] && SRCDIR=$CWD/${SRCDIR}

## Place to build (TMP) package (PKG) and output (OUTPUT) the program:
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-${PRGNAME}
OUTPUT=${OUTPUT:-/usr/src/slackpackages}

## Full path of source:
SOURCE="$SRCDIR/${PRGNAME}-${VERSION}.tar.gz"

##
## --- with a little luck, you won't have to edit below this point ---
##

## Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line ${LINENO}" | tee $TMP/error-${PRGNAME}.log' ERR

## Catch unitialized variables:
set -u
P1=${1:-1}

## Create working directories:
mkdir -p $OUTPUT             # place for the package to be saved
mkdir -p $TMP/tmp-${PRGNAME} # location to build the source
mkdir -p $PKG                # place for the package to be built

rm -rf $PKG/*                # always erase old package's contents
rm -rf $TMP/tmp-${PRGNAME}/* # remove the remnants of previous build

## Source file availability:
if ! [ -f ${SOURCE} ]; then
  echo -e "\E[1;31m"
  echo -ne "File '$(basename ${SOURCE})' not available... aborting the build."
  echo -e "\E[0m\n"
  exit 1
fi

## --- PACKAGE BUILDING ---
cd $TMP/tmp-${PRGNAME}

echo -e "\E[1;32m"
echo -ne "Extracting the source archive(s) for $PRGNAME..."
echo -e "\E[0m\n"
sleep 1

if $(file ${SOURCE} | grep -q ": bzip2"); then
  tar -xjvf ${SOURCE}
elif $(file ${SOURCE} | grep -q ": gzip"); then
  tar -xzvf ${SOURCE}
fi

mv ${PRGNAME}-${VERSION} ${PRGNAME}
(cd ${PRGNAME}
find . \( -perm 777 -o -perm 775 -o -perm 711 \) -exec chmod 755 {} \;
find . \( -perm 700 -o -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
chown -R root.root .
)

echo -e "\E[1;32m"
echo -ne "Installing $PRGNAME..."
echo -e "\E[0m\n"
sleep 1

mkdir -p $PKG/usr/share
cp -a ${PRGNAME} $PKG/usr/share
cd $PKG/usr/share/${PRGNAME}

mkdir -p $PKG/etc/${PRGNAME}
mv conf $PKG/etc/${PRGNAME}
ln -sf /etc/${PRGNAME} conf
(cd $PKG/etc/${PRGNAME}/conf
for configfile in *; do mv $configfile ../$configfile.new; done
)

mkdir -p $PKG/var/log/${PRGNAME}
mv logs $PKG/var/log/${PRGNAME}
ln -sf /var/log/${PRGNAME} logs

mkdir -p -m 0700 $PKG/var/tmp/${PRGNAME}
mv temp $PKG/var/tmp/${PRGNAME}
ln -sf /var/tmp/${PRGNAME} temp

mkdir -p $PKG/var/cache/${PRGNAME}
mv work $PKG/var/cache/${PRGNAME}
ln -sf /var/cache/${PRGNAME} work

mkdir -p $PKG/var/lib/${PRGNAME}
mv webapps $PKG/var/lib/${PRGNAME}
ln -sf /var/lib/${PRGNAME} webapps
(cd $PKG/var/lib/${PRGNAME}/webapps
for appfile in *; do mv $appfile ../$appfile; done
)

mkdir -p $PKG/etc/{rc.d,profile.d}
install -m 644 ${SRCDIR}/rc.tomcat    $PKG/etc/rc.d/rc.tomcat.new
install -m 755 ${SRCDIR}/catalina.csh $PKG/etc/profile.d
install -m 755 ${SRCDIR}/catalina.sh  $PKG/etc/profile.d

rm -rf $PKG/etc/${PRGNAME}/conf
rm -rf $PKG/usr/share/${PRGNAME}/bin/*.{bat,exe}
rm -rf $PKG/var/cache/${PRGNAME}/work
rm -rf $PKG/var/log/${PRGNAME}/logs
rm -rf $PKG/var/lib/${PRGNAME}/webapps
rm -rf $PKG/var/tmp/${PRGNAME}/temp

## Add documentation:
mkdir -p $PKG/usr/doc/${PRGNAME}-${VERSION}
cp -a $DOCFILES $PKG/usr/doc/${PRGNAME}-${VERSION} || true
find $PKG/usr/doc/${PRGNAME}-${VERSION} -type d -exec chmod 755 {} \;
find $PKG/usr/doc/${PRGNAME}-${VERSION} -type f -exec chmod 644 {} \;
chown -R root.root $PKG/usr/doc/${PRGNAME}-${VERSION}

echo -e "\E[1;32m"
echo -ne "Building package..."
echo -e "\E[0m\n"
sleep 1

## Add slackbuild files:
mkdir -p $PKG/usr/src/slackbuilds/${PRGNAME}-${VERSION}

(cd $PKG/usr/src/slackbuilds/${PRGNAME}-${VERSION}
install -m 644 $SRCDIR/slack-desc .
install -m 644 $SRCDIR/slack-required .
install -m 644 $SRCDIR/doinst.sh .
install -m 644 $SRCDIR/${PRGNAME}.info .
install -m 644 $SRCDIR/${PRGNAME}.SlackBuild .
install -m 644 $SRCDIR/rc.tomcat .
install -m 644 $SRCDIR/catalina.csh .
install -m 644 $SRCDIR/catalina.sh .
)

# Add a package description:
mkdir -p $PKG/install
install -m 644 $SRCDIR/slack-desc $PKG/install
install -m 644 $SRCDIR/doinst.sh $PKG/install
install -m 644 $SRCDIR/slack-required $PKG/install

## Build the package:
cd $PKG
makepkg -l y -c n $OUTPUT/${PRGNAME}-${VERSION}-${ARCH}-${RELEASE}${BUILDTAG}.tgz \
  2>&1 | tee $TMP/makepkg-${PRGNAME}.log

echo -e "\E[1;32m"
echo -ne "Your $PRGNAME package is complete. It is located in $OUTPUT"
echo -e "\E[0m\n"

if [ "$P1" = "--cleanup" ]; then
  rm -rf $TMP/tmp-${PRGNAME}
  rm -rf $PKG
fi
