#!/bin/sh
#
# Slackware build script for HighMoon - duel in space.
#
# Heavily based on the Slackware 13.0 SlackBuild
# Written by Luigi "Reilly" Cozzolino
# URL: http://highmoon.gerdsmeier.net/
# Packager: Luigi "Reilly" Cozzolino

# Exit on most errors:
set -e

########  SOURCE PACKAGE SECTION  ###########
SRCNAME="highmoon"
SRCVER="1.2.4"
SRCTYPE="tar.gz"
SRCPKG="${SRCNAME}-${SRCVER}.${SRCTYPE}"
SOURCEDIR="HighMoon"
########  END SOURCE PACKAGE SECTION  #######

########  SLACKWARE PACKAGE SECTION  ########
SLKNAM="${SRCNAME}"
SLKVER="${SRCVER}"
ARCH="${ARCH:-i486}"
BUILD="2lc"
PKGTYPE="${PKGTYPE:-txz}"
########  END SLACKWARE PACKAGE SECTION  ####

CWD=$(pwd)
TMP=${TMP:-$HOME/PKGS/${SLKNAM}}

if [ "${ARCH}" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "${ARCH}" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
fi

# Number of jobs to compile at once:
if [ -z "$NUMJOBS" ]; then
  NUMJOBS=-j4
fi

PKG=$TMP/package-${SLKNAM}
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf ${SOURCEDIR}

clear
sleep 1
echo ; echo ; echo
echo "+==========================================================="
echo -e "|\033[01;32m Building\033[00m \033[01;31m${SRCPKG}\033[00m"
echo "+==========================================================="
sleep 5
# Checking source package type...
[ "${SRCTYPE}" = "tar.gz"  -o "${SRCTYPE}" = "tgz" ] && EXTRACT_SRCPKG="tar -xvzf"
[ "${SRCTYPE}" = "tar.bz2" -o "${SRCTYPE}" = "tbz" ] && EXTRACT_SRCPKG="tar -xvjf"
[ "${SRCTYPE}" = "zip"     -o "${SRCTYPE}" = "ZIP" ] && EXTRACT_SRCPKG="unzip"
[ "${SRCTYPE}" = "rar"     -o "${SRCTYPE}" = "RAR" ] && EXTRACT_SRCPKG="rar x"
[ "${SRCTYPE}" = "7z"      -o "${SRCTYPE}" = "7Z" ]  && EXTRACT_SRCPKG="7z x"

# and finally extract it:
${EXTRACT_SRCPKG} $CWD/${SRCPKG} || exit 1
cd ${SOURCEDIR}

# Make sure ownerships and permissions are sane:
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
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 {} \;

# Apply a patch from Mandriva(many thanks guys!):
bzcat $CWD/patches/highmoon-1.2.3-mdkconf.patch.bz2 | patch -p0 --verbose || exit 1

# Build and install:
make $NUMJOBS \
     INSTALLPATH="${PKG}/usr/share/games/${SLKNAM}" \
     INSTALLBIN="${PKG}/usr/games"	|| exit 1

make INSTALLPATH="${PKG}/usr/share/games/${SLKNAM}" \
     INSTALLBIN="${PKG}/usr/games" \
     install				|| exit 1

# Strip binaries and libraries:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Add some shell script for Highmoon:
mkdir -vp $PKG/usr/games
cat > $PKG/usr/games/${SLKNAM} << "EOF"
#!/bin/sh

# Set HighMoon directory:
HIGHMOON_DIR="/usr/share/games/highmoon"

cd ${HIGHMOON_DIR}
./ufo $1
EOF

# Change perms:
chmod -v 755 $PKG/usr/games/${SLKNAM}

# Add some image icon:
mkdir -p $PKG/usr/share/pixmaps
install -m 644 icon.png $PKG/usr/share/pixmaps/${SLKNAM}.png

# Add a desktop file:
mkdir -p $PKG/usr/share/applications
cat << "EOF" > $PKG/usr/share/applications/${SLKNAM}.desktop
[Desktop Entry]
Encoding=UTF-8
Type=Application
Categories=Application;Game;ArcadeGame;
Name=HighMoon
Comment=HighMoon - Duel in Space.
Exec=highmoon
Icon=/usr/share/pixmaps/highmoon.png
StartupNotify=true
Terminal=false
EOF

# Perform this action necessary for create a `slack-required':
install -m 755 ufo $PKG/usr/games
requiredbuilder -v -y -s $CWD $PKG
sleep 1
rm -v $PKG/usr/games/ufo

# Add a documentation directory:
mkdir -p $PKG/usr/doc/${SLKNAM}-${SLKVER}
cp -a \
  AUTHORS* COPYING* INSTALL* NEWS* README* VERSION* \
  $PKG/usr/doc/${SLKNAM}-${SLKVER}
cat $CWD/${SLKNAM}.SlackBuild > $PKG/usr/doc/${SLKNAM}-${SLKVER}/${SLKNAM}.SlackBuild

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 $TMP/${SLKNAM}-${SLKVER}-${ARCH}-${BUILD}.${PKGTYPE}

# Checksum `md5' file creation:
( cd $TMP ; md5sum ${SLKNAM}-${SLKVER}-${ARCH}-$BUILD.${PKGTYPE} > ${SLKNAM}-${SLKVER}-${ARCH}-${BUILD}.${PKGTYPE}.md5 )

# Final touches:
( cd $TMP ; cat $PKG/install/slack-desc | grep "^${SLKNAM}:" > ${SLKNAM}-${SLKVER}-${ARCH}-${BUILD}.txt )

# Clean up the extra stuff:
if [ "$1" = "--cleanup" -o "$1" = "-c" ]; then
  rm -rf $TMP/${SOURCEDIR}
  rm -rf $PKG
fi
