#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
#
# Slackware build script for Nestopia - NES/Famicom emulator
#
# Written by Luigi "Reilly" Cozzolino
# URL: http://nestopia.sourceforge.net/
# Packager: Luigi "Reilly" Cozzolino

# Exit on most errors:
set -e

########  SOURCE ZIP PACKAGE SECTION  ##########
SOURCE_ZIP1="Nestopia140src"
SOURCE_ZIP2="nst140_lnx_release_h"
SOURCEDIR="nestopia"
########  END SOURCE ZIP PACKAGE SECTION  ######

########  SLACKWARE PACKAGE SECTION  ###########
PKGNAM="nestopia"
VERSION="140"
ARCH=${ARCH:-i486}
BUILD="1lc"
PKGTYPE="${PKGTYPE:-txz}"
########  END SLACKWARE PACKAGE SECTION  #######

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

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

PKG=$TMP/package-${PKGNAM}
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${PKGNAM}\033[00m"
echo "+==========================================================="
sleep 5

# Add a source directory:
mkdir ${SOURCEDIR}
cd ${SOURCEDIR}

# Extract source zip:
unzip $CWD/${SOURCE_ZIP1}.zip
unzip $CWD/${SOURCE_ZIP2}.zip

# 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 {} \;

# Build:
make $NUMJOBS || exit 1

# Add base installation:
mkdir -p $PKG/usr/share/games/nestopia
install -m 755 nst $PKG/usr/share/games/nestopia/nestopia.bin
install -m 644 nstcontrols NstDatabase.xml $PKG/usr/share/games/nestopia

# Add some icon images file:
mkdir -p $PKG/usr/share/games/nestopia
install -m 644 source/win32/resource/{application.ico,nes.ico} $PKG/usr/share/games/nestopia

# Add a shell script:
mkdir -p $PKG/usr/games
cat > $PKG/usr/games/nestopia.sh << "EOF"
#!/bin/sh
# Shell script for Nestopia
# Author: Luigi "Reilly" Cozzolino - Jul 2009

# Set some variables:
NESTOPIA_DIR="/usr/share/games/nestopia"
NESTOPIA_BIN="nestopia.bin"

# If `home-gamedir' environment does not exist then create it:
if [ ! -d $HOME/.nestopia ]; then
  mkdir -p $HOME/.nestopia
  ( cd $HOME/.nestopia
    cp -a ${NESTOPIA_DIR}/nstcontrols .
    cp -a ${NESTOPIA_DIR}/NstDatabase.xml .
  )
fi

cd ${NESTOPIA_DIR}
./${NESTOPIA_BIN}
EOF

# change perms:
chmod 755 $PKG/usr/games/nestopia.sh

# 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 a desktop file:
mkdir -p $PKG/usr/share/applications
cat > $PKG/usr/share/applications/nestopia.desktop << "EOF"
[Desktop Entry]
Encoding=UTF-8
Name=Nestopia
GenericName=NES/Famicom emulator
Exec=nestopia.sh
Terminal=false
Type=Application
Categories=Application;Game;Emulator;
Icon=/usr/share/games/nestopia/nes.ico
StartupNotify=true
EOF

# Add a simulation binary for requiredbuilder:
mkdir -p $PKG/usr/bin
install -m 755 nst	$PKG/usr/bin/${PKGNAM}
requiredbuilder -v -y -s $CWD $PKG
# Now remove redundances:
rm -rf $PKG/usr/bin

# Add a documentation directory:
mkdir -p $PKG/usr/doc/${PKGNAM}-$VERSION
cp -a \
  *.txt COPYING* README* readme.html \
  $PKG/usr/doc/${PKGNAM}-$VERSION
cat $CWD/${PKGNAM}.SlackBuild > $PKG/usr/doc/${PKGNAM}-$VERSION/${PKGNAM}.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/${PKGNAM}-$VERSION-${ARCH}-${BUILD}.${PKGTYPE}

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

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

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