#!/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.
##

## Package variables.
BUILD=1gsb
VERSION=1.0rc1

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

## 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

## Decompress archive.
mkdir -p $TMP/mac4lin &&
cd $TMP/mac4lin &&
tar xfz $CWD/Mac4Lin_v1.0_RC1.tar.gz &&
cd Mac4Lin_v1.0_RC1 || exit 1

# Apply ARCH specific patches.
if [ "$ARCH" = "i386" ]; then
  echo "${0##*/}: Applying any patches for $ARCH"
  #cat $CWD/patches/foo.patch | patch -p0 --verbose
elif [ "$ARCH" = "i486" ]; then
  echo "${0##*/}: Applying any patches for $ARCH"
  #cat $CWD/patches/foo.patch | patch -p0 --verbose
elif [ "$ARCH" = "i586" ]; then
  echo "${0##*/}: Applying any patches for $ARCH"
  #cat $CWD/patches/foo.patch | patch -p0 --verbose
elif [ "$ARCH" = "i686" ]; then
  echo "${0##*/}: Applying any patches for $ARCH"
  #cat $CWD/patches/foo.patch | patch -p0 --verbose
elif [ "$ARCH" = "x86_64" ]; then
  echo "${0##*/}: Applying any patches for $ARCH"
  #cat $CWD/patches/foo.patch | patch -p0 --verbose
elif [ "$ARCH" = "powerpc" ]; then
  echo "${0##*/}: Applying any patches for $ARCH"
  #cat $CWD/patches/foo.patch | patch -p0 --verbose
fi

## Fix permissions for building.
chown -R root.root .
find . -perm 664 -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 {} \;

## Install
mkdir -p $PKG/usr/share/icons
mkdir -p $PKG/usr/share/themes
mkdir -p $PKG/usr/share/emerald/themes
mkdir -p $PKG/usr/share/backgrounds
mkdir -p $PKG/usr/share/gdm/themes
mkdir -p $PKG/usr/share/splash
mkdir -p $PKG/usr/share/sounds
mkdir -p $PKG/usr/share/fonts/TTF

# Unpack GTK Theme
tar -C $PKG/usr/share/themes -xzf GTK/Mac4Lin_GTK_v1.0_RC.tar.gz &&
tar -C $PKG/usr/share/themes -xzf GTK/Mac4Lin_GTK_Graphite_v1.0_RC.tar.gz &&
tar -C $PKG/usr/share/themes -xzf GTK/Mac4Lin_Meta_v1.0_RC.tar.gz || exit 1

# Unpack Icon Theme
tar -C $PKG/usr/share/icons -xzf Icons/Mac4Lin_Icons_v1.0_RC.tar.gz &&
tar -C $PKG/usr/share/icons -xzf Cursors/Mac4Lin_Cursors_v1.0_RC.tar.gz || exit 1

# Unpack Emerald Theme
tar -C $PKG/usr/share/emerald/themes -xzf Emerald/Mac4Lin_Emerald_v1.0_RC.tar.gz &&
tar -C $PKG/usr/share/emerald/themes -xzf Emerald/Mac4Lin_Graphite_Emerald_v1.0_RC.tar.gz &&
tar -C $PKG/usr/share/emerald/themes -xzf Emerald/Mac4Lin_Emerald_v1.0_RC.tar.gz || exit 1

# Unpack GDM Theme
tar -C $PKG/usr/share/gdm/themes -xzf GDM/Mac4Lin_GDM_v1.0_RC.tar.gz || exit 1

# Unpack sounds
tar -C $PKG/usr/share/sounds -xzf Sounds/Mac4Lin_Sounds_v1.0_RC.tar.gz &&
tar -C $PKG/usr/share/sounds -xzf Sounds/Mac4Lin_Pidgin-Sounds_v1.0_RC.tar.gz || exit 1

# Wallpapers
cp Wallpapers/* $PKG/usr/share/backgrounds &&
tar xfz $CWD/Leopard_Wallpapers.tar.gz &&
cp Leopard_Wallpapers/* $PKG/usr/share/backgrounds || exit 1

(
cd $PKG/usr/share/backgrounds ;
for filename in * ;
do
     echo "$filename" | grep -q " "  ;
     if [ $? -eq 0 ]                  
     then
       fname=$filename ;
       n=$(echo $fname | sed -e "s/ /_/g") ;
       mv "$fname" "$n" || exit 1
       chmod 644 $n
     fi
done
)

# Fonts
# 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;
tar -C $PKG/$FONTDIR -xzf Fonts/fonts.tar.gz || exit 1
## These msfonts are in mscorefont package
rm $PKG/$FONTDIR/[a-z]*ttf

## Set permissions
find $PKG/usr -type f -exec chmod 644 {} \;

## 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/mac4lin
}
