#!/bin/sh

# Heavily based on the Slackware 13.37 SlackBuild
# Written by neongen ( neongen at libero dot it )
# Last build from Vito (Vito at slacky dot it)
#
# Slackware build script for truecrypt
# Official Site: http://www.truecrypt.org
#
# 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.

set -e

PKGNAME=truecrypt
VERSION=${VERSION:-7.0a}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}
ARCH=${ARCH:-x86_64}

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


CHOST="i486"
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$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"
  CHOST="x86_64"
fi

rm -rf $TMP
mkdir -p $TMP $PKG $OUTPUT

cd $TMP
if [ -e $CWD/TrueCrypt\ $VERSION\ Source.tar.gz ];then
   tar xfvz $CWD/TrueCrypt\ $VERSION\ Source.tar.gz
else
   echo "Source not found!"
   exit 1
fi
cd $PKGNAME-$VERSION-source

# This fixes dismount failures on Slackware
zcat $CWD/$PKGNAME.diff.gz | patch -p1 -E --backup --verbose

tar xfvz $CWD/pkcs.tar.gz

chown -R root:root .
chmod -R u+w,go+r-w,a-s .

EXTRA_CFLAGS="$SLKCFLAGS" \
EXTRA_CXXFLAGS="$SLKCFLAGS" \
PKCS11_INC=$TMP/$PKGNAME-$VERSION-source/pkcs \
make VERBOSE=1

mkdir -p $PKG/usr/doc/$PKGNAME-$VERSION

cp Release/Setup\ Files/License.txt $PKG/usr/doc/$PKGNAME-$VERSION
cp Release/Setup\ Files/TrueCrypt\ User\ Guide.pdf $PKG/usr/doc/$PKGNAME-$VERSION

mkdir -p $PKG/usr/share/applications
cp $CWD/truecrypt.desktop $PKG/usr/share/applications
mkdir -p $PKG/usr/share/pixmaps
cp $CWD/truecrypt.png $PKG/usr/share/pixmaps

mkdir -p $PKG/usr/bin
cp Main/truecrypt $PKG/usr/bin

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$PKGNAME-$VERSION/slack-desc
# cat $CWD/slack-required > $PKG/install/slack-required
# cat $CWD/slack-required > $PKG/usr/doc/$PKGNAME-$VERSION/slack-required
cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$VERSION/$PKGNAME.SlackBuild

cd $PKG
chown -R root:root $PKG
chmod 644 $PKG/usr/doc/$PKGNAME-$VERSION/*

if [ -x "$(which requiredbuilder 2>/dev/null)" ];then
  requiredbuilder -y -v -s $CWD $PKG 
fi

/sbin/makepkg -l y -c n $OUTPUT/$PKGNAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

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

