#!/bin/bash
## Version: 1.6 GSB Package SlackBuild.
## Copyright (c) 2007 Darren 'Tadgy' Austin <darren (at) gnomeslackbuild.org>
## Copyright (c) 2007 Steve Kennedy <sk238 (at) exeter.ac.uk>
##
## Licenced under the terms of the GNU General Public Licence version 3.
##
## Parts of this scripts are based on Eric Hameleers <alien@slackware.com>

## Package variables.
BUILD=1gsb
VERSION=2.0

## List any packages here that are required to build this package.
## The list is space seperated and case sensitive.
REQUIRED_PACKAGES="cabextract"

## Build variables.
ARCH=noarch
TUNE=${TUNE:-i586}
DISTRO=${DISTRO:-slackware}
TMP=${TMP:-/tmp}
PKGDEST=${PKGDEST:-$TMP}

## Script variables.
PKGNAME=$(basename $0 .SlackBuild)
PKG=$TMP/package-$PKGNAME
CWD=$(pwd)
NOCLEANUP=0
FORCEBUILD=0

## Usage.
function usage() {
  cat << EOF
Usage: ${0##*/} [options]

Options:  --force       The package will not be built if a package of the same
                        name is already installed, or any of the packages
                        required to build are missing.  This option over-rides
                        these checks and attempts a build anyway.
          --no-cleanup  By default any temporary source, build and package
                        directories will be deleted once the package is built.
                        This option prevents those files from being removed.
          --help        Show this help screen.
EOF
}

## Parse command line arguments.
while [ $# -gt 0 ]; do
  if [ "$1" = "-force" ] || [ "$1" = "--force" ]; then
    FORCEBUILD=1
    shift
  elif [ "$1" = "-no-cleanup" ] || [ "$1" = "--no-cleanup" ]; then
    NOCLEANUP=1
    shift
  elif [ "$1" = "-help" ] || [ "$1" = "--help" ]; then
    usage
    exit 0
  else
    echo "${0##*/}: Unknown option: $1"
    exit 1
  fi
done

## Package requirements and installation checks.
[ "$FORCEBUILD" = "0" ] && {
  function check_installed() {
    ls -1 /var/log/packages | grep "^${1}-[^-]*-[^-]*-[^-]*$" >/dev/null 2>&1
    return $?
  }

  check_installed "$PKGNAME" && {
    echo "${0##*/}: Remove installed '$PKGNAME' package before build."
    exit 1
  }

  for REQ in $REQUIRED_PACKAGES; do
    check_installed "$REQ" || {
      echo "${0##*/}: Required package '$REQ' not installed."
      exit 1
    }
  done
}

## Temporary space and package storage.
mkdir -p $TMP
rm -rf $PKG
mkdir -p $PKG
mkdir -p $PKG/install
mkdir -p $PKGDEST

## Extract and Install

# Font directory location depends on the X build prefix:
# Determine what X we're running (the modular X returns the prefix
# in the next command, while older versions stay silent):
XPREF=`pkg-config --variable=prefix x11` || true
if [ "$XPREF" == "" ]; then
  XPREF='/usr/X11R6'
  FONTDIR="$XPREF/lib/X11/fonts/TTF"
else
  FONTDIR="/usr/share/fonts/TTF"
fi
mkdir -p $PKG/$FONTDIR
FONTS="andale32 arial32 arialb32 comic32 courie32 georgi32 impact32 times32 trebuc32 verdan32 webdin32"
cd $TMP
mkdir -p $TMP/cab-contents ;
for i in $FONTS ; do
  cabextract --lowercase --directory=cab-contents $CWD/$i.exe 2>/dev/null 1>/dev/null || exit 1
done

## for vista fonts
cabextract -L -F ppviewer.cab --directory=$TMP $CWD/PowerPointViewer.exe &&
cabextract -L -F '*.TT[FC]' --directory=cab-contents $TMP/ppviewer.cab || exit 1
( cd cab-contents && mv cambria.ttc cambria.ttf && chmod 600 \
    calibri{,b,i,z}.ttf cambria{,b,i,z}.ttf candara{,b,i,z}.ttf \
    consola{,b,i,z}.ttf constan{,b,i,z}.ttf corbel{,b,i,z}.ttf )

# Install the fonts into the package directory.
cp -v cab-contents/*.ttf $PKG/$FONTDIR
cd $PKG/$FONTDIR
chown -R root:root *
chmod -R u+w,go+r-w,a-s *

# Add fontconfig info
#mkdir -p $PKG/etc/fonts
#for i in alias local misc ;
#do
  #cp $CWD/gsb/$i.conf $PKG/etc/fonts/$i.conf.new || exit 1;
#done;
#chown root:root $PKG/etc/fonts/*.conf.new || exit 1;

## Package meta files
for FILE in doinst.sh slack-desc slack-required slack-conflicts slack-suggests
do
  [ -e $CWD/$FILE ] && {
    cat $CWD/$FILE >>$PKG/install/$FILE
  }
done

## Create the package
cd $PKG
makepkg -p -l y -c n $PKGDEST/$PKGNAME-${VERSION//-/_}-$ARCH-$BUILD.tgz

## Cleanup
[ "$NOCLEANUP" = "0" ] && {
  rm -rf $PKG $TMP/cab-contents
}
