#!/bin/sh
#
# Build and package cherokee in Slackware
# by: Juan Angulo Moreno <juan@apuntale.com>

CWD=`pwd`
TMP=${TMP:-/tmp}
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
PKG=$TMP/package-cherokee

VERSION=0.5.6
ARCH=${ARCH:-i486}
BUILD=3jja
NAME=cherokee

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

cherokee_configure() {
CFLAGS="$SLKCFLAGS" \
./configure \
    --prefix=/usr \
    --sysconfdir=/etc\
    --with-wwwroot=/var/www/htdocs \
    --enable-tls=openssl
    --enable-static-module=all
}

# Clean
rm -rf $PKG
rm $TMP/$NAME-$VERSION-$ARCH-$BUILD.tgz 
rm $TMP/$NAME-$VERSION-$ARCH-$BUILD.tgz.md5
rm -rf $CWD/$NAME-$VERSION

# compile
rm -rf $CWD/cherokee-$VERSION
tar xvfz $CWD/cherokee-$VERSION.tar.gz
cd cherokee-$VERSION

# Fixup perms/owners:
chown -R root.root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -name "*.h" -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;


# Sometimes they ship a few of these:
find . -name "*.orig" -exec rm {} \;

# Compila
cherokee_configure
make
make install DESTDIR=$PKG

chown -R root:root $PKG/usr/bin
chown -R root:root $PKG/usr/sbin

sed -e 's/#User/User/g' $PKG/etc/cherokee/cherokee.conf > cherokee.conf.NEW
mv cherokee.conf.NEW $PKG/etc/cherokee/cherokee.conf
sed -e 's/#Group/Group/g' $PKG/etc/cherokee/cherokee.conf > cherokee.conf.NEW
mv cherokee.conf.NEW $PKG/etc/cherokee/cherokee.conf

mkdir -p $PKG/usr/man
mv $PKG/usr/share/man/man1 $PKG/usr/man
rm -rf $PKG/usr/share/man1
gzip -9 $PKG/usr/man/man?/*.?

mkdir -p $PKG/usr/doc/$NAME-$VERSION
mv $PKG/usr/share/doc/$NAME $PKG/usr/doc/$NAME-$VERSION
rm -rf $PKG/usr/share/doc

cd $TMP/$NAME-$VERSION
cp -a AUTHORS README TODO COPYING ChangeLog INSTALL \
  $PKG/usr/doc/$NAME-$VERSION

mv $PKG/var/www/htdocs/index.html $PKG/var/www/htdocs/index-cherokee.html

mkdir -p $PKG/etc/rc.d
cp $CWD/rc.cherokee $PKG/etc/rc.d/rc.cherokee
chmod 0644 $PKG/etc/rc.d/rc.cherokee

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

mkdir -p $PKG/usr/src/slackbuilds/$NAME-$VERSION
cp $CWD/cherokee.SlackBuild $PKG/usr/src/slackbuilds/$NAME-$VERSION
chmod 0644 $PKG/usr/src/slackbuilds/$NAME-$VERSION/cherokee.SlackBuild

# Stuffs that not need
rm -rf $PKG/usr/var
rm -rf $PKG/usr/share/doc
rm -rf $PKG/usr/share/man

cd $PKG
makepkg -l y -c n $TMP/$NAME-$VERSION-$ARCH-$BUILD.tgz

# md5sum for package 
cd $TMP
md5sum $NAME-$VERSION-$ARCH-$BUILD.tgz > $NAME-$VERSION-$ARCH-$BUILD.tgz.md5
