#!/bin/sh

PRGNAM=$(basename $(pwd))
VERSION=${VERSION:-$(date +%Y.%m.%d_%H.%M)
ARCH=font
BUILD=${BUILD:-1}
TAG=${TAG:-cyco}

CWD=$(pwd)

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

# Create working directories:
mkdir -p $OUTPUT          # place for the package to be saved
mkdir -p $PKG             # place for the package to be built
rm -rf $PKG/*             # always erase old package's contents
rm -rf $OUTPUT/{configure,make,install,error,makepkg}-$PRGNAM.log
                          # remove old log files

FONTDIR="/usr/share/fonts/TTF"

# Move the fonts into place:
mkdir -p $PKG$FONTDIR
( cd $PKG$FONTDIR/
    [ ! -e $CWD/$PRGNAM.ttf ] && \
        wget -c http://dl.dropbox.com/u/7247980/MarainScript.ttf -O $CWD/$PRGNAM.ttf
    cp $CWD/$PRGNAM.ttf .
)

# Post-install script:
mkdir -p $PKG/install
cat <<EOT > $PKG/install/doinst.sh
# Update X font indexes and the font cache:
if [ -x .$XPREF/bin/mkfontdir ]; then
  chroot . $XPREF/bin/mkfontscale $FONTDIR
  chroot . $XPREF/bin/mkfontdir $FONTDIR
fi
if [ -x .$XPREF/bin/fc-cache ]; then
  chroot . $XPREF/bin/fc-cache -f $FONTDIR
fi
EOT

# Add a package description:
mkdir -p $PKG/install
cat <<EOF > $PKG/install/slack-desc
$PRGNAM: $PRGNAM (Marain font)
$PRGNAM: 
$PRGNAM: « [...] based on the Marain alphabet, from Iain M. Banks' "Culture" novels.
$PRGNAM: In those novels, the spacefaring Culture use the Marain alphabet because each
$PRGNAM: character is unique and can be read in any rotation. »
$PRGNAM: 
$PRGNAM: 
$PRGNAM: 
$PRGNAM: 
$PRGNAM: http://danielsolisblog.blogspot.com/2010/09/free-font-marain-script.html
$PRGNAM: 
EOF

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.tgz 2>&1 | tee $OUTPUT/makepkg-${PRGNAM}.log
cd $OUTPUT
md5sum ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.tgz > ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.tgz.md5
cd -
cat $PKG/install/slack-desc | grep "^${PRGNAM}" > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.txt