#!/bin/sh
#
# Slackware build script for Powermanga - Arcade 2D shoot'em up game
#
# Heavily based on the Slackware 13.0 SlackBuild
# Written by Luigi "Reilly" Cozzolino
# URL: http://linux.tlk.fr/games/Powermanga/
# Packager: Luigi "Reilly" Cozzolino

# Exit on most errors:
set -e

########  SOURCE PACKAGE SECTION  ###########
SRCNAME="powermanga"
SRCVER=""
SRCTYPE="tgz"
SRCPKG="${SRCNAME}.${SRCTYPE}"
SOURCEDIR="${SRCNAME}-0.90"
########  END SOURCE PACKAGE SECTION  #######

########  SLACKWARE PACKAGE SECTION  ########
SLKNAM="${SRCNAME}"
SLKVER="0.90"
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 from Debian(thank you boys):
patch -p1 --verbose < $CWD/patches/005_link_libm.diff || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --mandir=/usr/man/man6 \
  --build=${ARCH}-slackware-linux || exit 1

# Build and install:
make $NUMJOBS	scoredir="/var/games/${SLKNAM}"				|| exit 1
make install	scoredir="/var/games/${SLKNAM}"		DESTDIR=$PKG	|| 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
)

# Default to English:
echo "Lang=en" > $PKG/usr/share/games/powermanga/texts/config.ini

# Add a desktop file:
mkdir -vp $PKG/usr/share/applications
cat > $PKG/usr/share/applications/${SLKNAM}.desktop << "EOF"
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=Powermanga
GenericName=Shoot'em up Game
Comment=Play a vertical shoot'em up game
Icon=/usr/share/pixmaps/powermanga.xpm
Exec=powermanga
Terminal=false
Categories=Game;ArcadeGame;
EOF

# Add a image icon:
mkdir -vp						$PKG/usr/share/pixmaps
install -m 0644 $CWD/${SLKNAM}.xpm			$PKG/usr/share/pixmaps

# Keep old records in safe and chenge perms:
mv -v $PKG/var/games/powermanga/powermanga.hi		$PKG/var/games/powermanga/powermanga.hi.new
mv -v $PKG/var/games/powermanga/powermanga.hi-easy	$PKG/var/games/powermanga/powermanga.hi-easy.new
mv -v $PKG/var/games/powermanga/powermanga.hi-hard	$PKG/var/games/powermanga/powermanga.hi-hard.new
chmod -v 0664 $PKG/var/games/powermanga/*

# Compress and link manpages, if any:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.*
      )
    done
  )
fi

# Add a documentation directory:
mkdir -p $PKG/usr/doc/${SLKNAM}-${SLKVER}
cp -a \
  AUTHORS* CHANGES* COPYING* README* \
  $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
requiredbuilder -v -y -s $CWD $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
