#!/bin/sh
#
# SlackBuild for E-UAE
# http://www.rcdrummond.net/uae/
# By SukkoPera <enjoy.the.silence@iol.it>
#
# Thanks a lot to CAT for his Slackware package cration
# guide (http://www.slacky.it/misto/tutorial/spunleashed.txt)
#
# Check out
# - http://www.sukkopera.tk
# - http://www.slacky.it
# - http://www.nyft.org
#
# Notes:
# - Thanks to Giulio Canevari for the icons and to rob and MOgWai46 for
#   the package testing


# Get the current and temporary directories
CWD=`pwd`
if [ "$TMP" = "" ]; then
 TMP=/tmp/pkg
fi

# Set up working directories
if [ ! -d $TMP ]
then
	mkdir -p $TMP
fi

# Some useful variables about the package
NAME=e_uae
PKG=$TMP/package-$NAME
VERSION=0.8.29-WIP4
ARCH=i486
BUILD=1suk
TNAME=e-uae
SOURCEARCH=$TNAME-$VERSION

# Print a welcome screen
echo "+--------------+"
echo "| $NAME-$VERSION |"
echo "+--------------+"


if [ -d $PKG ]
then
	# Clean up a previous build
	#rm -rf $PKG
	#mkdir -p $PKG
	echo "Previous package build directory found, please remove it and"
        echo "restart the SlackBuild script: $PKG"
	exit 1
else
	mkdir -p $PKG
fi

for i in usr/share/applications usr/share/pixmaps/$NAME
do
	echo "--- Creating directory $PKG/$i"
	mkdir -p $PKG/$i
done

# Decompress
echo "------------------------- Uncompressing source -------------------------"
cd $TMP
tar jxvf $CWD/$SOURCEARCH.tar.bz2
mv $SOURCEARCH $NAME-$VERSION
cd $NAME-$VERSION

# Install the libscg headers. To do this we need an uncompressed and
# compiled cdrtools source tree
CDRTOOLS=/tmp/cdrtools-2.01.01/
if [ -d $CDRTOOLS ]
then
	echo "--- cdrtools found, trying to install libscg headers"
	./src/install_libscg $CDRTOOLS
	SCSI="--enable-scsi-device --enable-cdtv --enable-cd32"
else
	echo "--- cdrtools NOT found, this build will not include SCSI (and therefore CDTV/CD32) support"
fi

# Build
echo "------------------------------ Configuring -----------------------------"
#  --enable-aga            Enable AGA chipset emulation (default yes)
#  --enable-cdtv           Enable CDTV emulation (default no)
#  --enable-cd32           Enable CD32 emulation (default no)
#  --enable-cycle-exact-cpu  Enable cycle-exact CPU emulation (default yes)
#  --enable-compatible-cpu Enable compatible CPU emulation (default yes)
#  --enable-jit            Enable JIT compiler (currently x86 only)
#  --enable-natmem         Enable JIT direct memory support (default auto)
#  --enable-noflags        Enable noflags support in JIT (default no)
#  --enable-x86assembly    Enable x86 assembly optimizations (default auto)
#  --enable-threads        Enable thread support (default auto)
#  --enable-autoconfig     Enable emulaton of autoconfig devices (default auto)
#  --enable-scsi-device    Enable emulaton of SCSI devices (default no)
#  --enable-bsdsock        Enable bsdsocket.library emulation
#  --enable-bsdsock-new    Enable new bsdsocket.library emulation
#  --enable-enforcer       Enable ersatz Enforcer support (default auto)
#  --enable-dga            X11 version: Use the DGA extension
#  --enable-vidmode        X11 version: Use the XF86VidMode extension
#  --enable-ui             Use a user interface if possible (default on)
#  --enable-audio          Enable audio output (default auto)
#  --enable-fdi            Enable FDI support (default yes)
#  --with-x                use the X Window System
#  --with-sdl              Use SDL library for low-level functions
#  --with-sdl-sound        Use SDL library for sound
#  --with-sdl-gfx          Use SDL library for graphics

# All things autodetected should work without putting them in configure
CFLAGS="-O2 -march=i486 -mtune=i686" \
CXXFLAGS="-O2 -march=i486 -mtune=i686" \
	./configure \
	--prefix=/usr \
	--sysconfdir=/etc \
	--localstatedir=/var \
	--with-x \
	--with-sdl \
	--with-sdl-sound \
	--with-sdl-gfx \
	$SCSI \
	--enable-bsdsock-new

#echo "------------------------------- Patching -------------------------------"

echo "------------------------------ Compiling -------------------------------"
make
res=$?
if [ $res -ne 0 ]
then
	# make failed, we cannot continue
	exit $res
fi

echo "------------------------------ Installing ------------------------------"
make install DESTDIR=$PKG
res=$?
if [ $res -ne 0 ]
then
	# make install failed, we cannot continue
	exit 1
fi

# Doc
echo "----------------- Copying documentation and other files ----------------"
mkdir -p $PKG/usr/doc/$NAME-$VERSION
for i in ChangeLog CHANGES COPYING README docs/*
do
#        cat $i | gzip > $PKG/usr/doc/$NAME-$VERSION/$i.gz
        cp -a $i $PKG/usr/doc/$NAME-$VERSION/
done
#find $PKG/usr/doc/$NAME-$VERSION -type d -name "CVS" -exec rm -rf {} \; 2> /dev/null

# Amiga binaries - Maybe there's a better place to put these in?
mkdir $PKG/usr/doc/$NAME-$VERSION/amiga-bin
#cp amiga/{bsdsocket.library,mousehack,timehack,transrom,uae-control.info,uae_rcli,transdisk,uae-control,uaectrl} $PKG/usr/doc/$NAME-$VERSION/amiga-bin
find amiga -type f -maxdepth 1 -exec cp {} $PKG/usr/doc/$NAME-$VERSION/amiga-bin \;

# Icons & window manager menu integration
cp $CWD/*.png $PKG/usr/share/pixmaps/$NAME
cp $CWD/e_uae.desktop $PKG/usr/share/applications

# Gzip man pages
#find $PKG/usr/man -name "*.[123456789]" -exec gzip -9 {} \;

# SlackBuild stuff
mkdir -p $PKG/usr/doc/$NAME-$VERSION/slackbuild
cp $CWD/$0 $CWD/slack-desc $PKG/usr/doc/$NAME-$VERSION/slackbuild


# Strip binaries
echo "--------------------------- Stripping binaries -------------------------"
find $PKG -type f | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded

# Set permissions
echo "--------------------------- Setting permissions ------------------------"

chown -R root:root $PKG/usr/doc
chmod -R 755 $PKG/usr/doc/*
find $PKG/usr/doc -type f -exec chmod 644 {} \;

if [ -d $PKG/usr/share ]
then
	chown -R root:root $PKG/usr/share
	chmod -R 755 $PKG/usr/share/*
	find $PKG/usr/share -type f -exec chmod 644 {} \;
fi

# Copy Slackware package files
echo "--------------------- Copying Slackware package files ------------------"
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Create package
echo "---------------------------- Creating package --------------------------"
echo "Creating package"
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-0.8.29.wip4-$ARCH-$BUILD.tgz

# Clean up
if [ "$1" = "--cleanup" ]; then
	echo "---------------------- Cleaning up working directory -------------------"
	rm -rf $TMP/$NAME-$VERSION
	rm -rf $PKG
fi

# Package created
echo "Package creation finished!"
