#!/bin/sh

# Slackware build script for mirrorbrain

# Copyright 2011, 2012  Robby Workman, Northport, Alabama, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS 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 AUTHOR 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.

PRGNAM=mirrorbrain
VERSION=${VERSION:-2.19.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

MBPASS=${MBPASS:-"$(mkpasswd -l 12)"}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz

# Copy mod_form stuff 
cp -r $CWD/mod_form $PRGNAM-$VERSION

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

# Use Slackware's rc.httpd 
patch -p1 < $CWD/patches/fixup_path_to_rc.httpd.diff

# We don't care about most of this:
#patch -p1 < $CWD/patches/remove_meta_stuff.diff

# Use https for google maps
patch -p1 < $CWD/patches/use-https-for-googlemaps.diff

# Don't check >2GB files to make sure they can be downloaded;
# something in new httpd seems to dislike the range requests and
# they fail, which mb interprets as the file being unsuitable for download
patch -p1 < $CWD/patches/scanner-dont-check-http-ranges.diff

# Allow https mirrors too
patch -p1 < $CWD/patches/allow-https-mirrors.diff

# Apply some optional Slackware branding  :-)
[ x$BRANDING = xyes ] && patch -p1 < $CWD/patches/optional_slackware_branding.diff

mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/httpd/modules $PKG/usr/bin $PKG/etc/httpd/extra

cd mod_form	# mod_form is needed for mod_mirrorbrain
  patch < mod_form.c.diff
  CFLAGS="$SLKCFLAGS" apxs -c mod_form.c
  cp -a .libs/mod_form.so $PKG/usr/lib${LIBDIRSUFFIX}/httpd/modules
  sed "s%@baselibdir@%lib${LIBDIRSUFFIX}%" $CWD/config/mod_form.conf > \
    $PKG/etc/httpd/extra/mod_form.conf.new
cd ../mod_mirrorbrain
  CFLAGS="$SLKCFLAGS" apxs -I../mod_form -c mod_mirrorbrain.c
  cp -a .libs/mod_mirrorbrain.so $PKG/usr/lib${LIBDIRSUFFIX}/httpd/modules
  # Yes, this *is* the desired behavior
  sed "s%@baselibdir@%lib${LIBDIRSUFFIX}%" $CWD/config/mod_mirrorbrain.conf \
      mod_mirrorbrain.conf > $PKG/etc/httpd/extra/mod_mirrorbrain.conf.new
cd ../mod_autoindex_mb
  CFLAGS="$SLKCFLAGS" apxs -c mod_autoindex_mb.c
  cp -a .libs/mod_autoindex_mb.so $PKG/usr/lib${LIBDIRSUFFIX}/httpd/modules
  sed "s%@baselibdir@%lib${LIBDIRSUFFIX}%" $CWD/config/mod_autoindex_mb.conf > \
    $PKG/etc/httpd/extra/mod_autoindex_mb.conf.new
cd ..

cd tools
  gcc $SLKCFLAGS -lGeoIP -L/usr/include/GeoIP geoiplookup_continent.c -o geoiplookup_continent
  gcc $SLKCFLAGS -lGeoIP -L/usr/include/GeoIP geoiplookup_city.c -o geoiplookup_city
  cp -a geoiplookup_{city,continent} geoip-lite-update null-rsync $PKG/usr/bin
  cp -a scanner.pl $PKG/usr/bin/scanner
  cp -a ../mirrorprobe/mirrorprobe.py $PKG/usr/bin/mirrorprobe
cd ..

cd mb
  python setup.py install --root=$PKG
cd ..

# Install a mod_dbd.conf for the httpd module
sed "s,@password@,$MBPASS," $CWD/config/mod_dbd.conf \
    > $PKG/etc/httpd/extra/mod_dbd.conf.new

# Put the sql table structure and initial data in the package
# No, this isn't needed, but it makes config instructions easier :-)
mkdir -p $PKG/usr/share/mirrorbrain/sql/
cp -a sql/*.sql $PKG/usr/share/mirrorbrain/sql/
  
# Install some configs...
mkdir -p $PKG/etc/logrotate.d/
sed -e "s,@password@,$MBPASS," -e "s,@mail@,$EMAIL," \
    $CWD/config/mirrorbrain.conf > $PKG/etc/mirrorbrain.conf.new
cat $CWD/config/mirrorbrain.logrotate > $PKG/etc/logrotate.d/mirrorbrain.new
chown root:mirrorbrain $PKG/etc/mirrorbrain.conf.new
chmod 0640 $PKG/etc/mirrorbrain.conf.new

# Install a log dir writable by mirrorbrain
mkdir -p $PKG/var/log/mirrorbrain
chown mirrorbrain:mirrorbrain $PKG/var/log/mirrorbrain

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a COPYING FAQ TODO $PKG/usr/doc/$PRGNAM-$VERSION
cp $PKG/etc/httpd/extra/mod_mirrorbrain.conf.new \
   $PKG/usr/doc/$PRGNAM-$VERSION/mod_mirrorbrain.conf.example
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README.SLACKWARE > $PKG/usr/doc/$PRGNAM-$VERSION/README.SLACKWARE

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

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
