#!/bin/bash
#
# maintainer       : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# former           : George Vlahavas (vlahavas~AT~gmail~DOT~com, gapan@zenwalk forums)
# project web page : http://x.mame.net/
# source           : http://www.sourcefiles.org/Emulators/Videogames/xmame-0.106.tar.bz2

# Package name
NAME="xmame"
# Package version
VERSION="0.106"
# Source code URL
SOURCE="http://www.sourcefiles.org/Emulators/Videogames/xmame-$VERSION.tar.bz2"
# Files that should be placed in /usr/doc/package-version/
DOCS="README todo doc/multiplayer-readme.txt doc/changes.unix doc/changes.opengl doc/dga2.txt doc/mame/*"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="i486"
CPU="i686"
BUILD="64.2"

rm -rf $PKG
mkdir -p $PKG

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << END
$NAME: xmame is a port of MAME, the Multiple Arcade Machine Emulator
$NAME:
$NAME: MAME stands for Multi Arcade Machine Emulator. As the name
$NAME: suggests, it is a program that emulates the hardware (and low-level
$NAME: firmware) of a massive variety of arcade machines.
$NAME: 
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Check to see if source is present. If it's not, download it
rm -rf $NAME-$VERSION
if [ -f $NAME-$VERSION.tar.bz2 ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c $SOURCE
fi

# Unpack source
tar xvjf $NAME-$VERSION.tar.bz2

#
# building-computer dependent: the number of parallel 'jobs' of 'make'
#
# 'heavy' computer: 6 build-jobs at once
#
#numjobs='-j6'
#
# 'light' computer: 2 build-jobs at once
#
numjobs='-j2'

# log
(

# Copy Docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cd $CWD/$NAME-$VERSION
cp -a $DOCS $PKG/usr/doc/$NAME-$VERSION

# Patch the makefile and make
cd $CWD/$NAME-$VERSION
sed -i -e 's,CFLAGS = -O2,CFLAGS = -O2 -march=i486 -mtune=i686,' Makefile
sed -i -e 's,INSTALL_GROUP =        bin,INSTALL_GROUP = root,' Makefile
sed -i -e 's,PREFIX = /usr/local,PREFIX = /usr,' Makefile
sed -i -e 's,BINDIR = ,BINDIR = $(DESTDIR),' Makefile
sed -i -e 's,MANDIR = ,MANDIR = $(DESTDIR),' Makefile
sed -i -e 's,# JOY_STANDARD = 1,JOY_STANDARD = 1,' Makefile
sed -i -e 's,# JOY_SDL = 1,JOY_SDL = 1,' Makefile
sed -i -e 's,# SOUND_ALSA = 1,SOUND_ALSA = 1,' Makefile
sed -i -e 's,# SOUND_SDL = 1,SOUND_SDL = 1,' Makefile
sed -i -e 's,# X11_OPENGL = 1,X11_OPENGL = 1,' Makefile
sed -i -e 's,# XINPUT_DEVICES = 1,XINPUT_DEVICES = 1,' Makefile
sed -i -e 's,# X11_XINERAMA = 1,X11_XINERAMA = 1,' Makefile
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"
make $numjobs

# Install on temporary directory
make install DESTDIR=$PKG

# Create the shared directories for roms etc
mkdir -p $PKG/usr/share/xmame/roms
# Copy over the xmamerc file
cp -a $CWD/$NAME-$VERSION/doc/xmamerc.dist $PKG/usr/share/xmame/xmamerc
sed -i -e 's,/local,,' $PKG/usr/share/xmame/xmamerc
# Copy controller conf files
cp -a $CWD/$NAME-$VERSION/ctrlr $PKG/usr/share/xmame

cd $CWD
unzip catver.zip
cp -a Catver.ini $PKG/usr/share/xmame/

unzip mamehistory136.zip
cp -a history.dat $PKG/usr/share/xmame/
cp -a readhist.txt $PKG/usr/doc/xmame-0.106/

unzip nplayers0136.zip
cp -a nplayers.ini $PKG/usr/share/xmame/
cp -a docs/*.* $PKG/usr/doc/xmame-0.106/


# Make a symlink for the executable
cd $PKG/usr/bin
ln -sf xmame.x11 xmame

# Strip binaries
cd $PKG
find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# Compress the man page(s)
find $PKG/usr/man -type f -exec gzip -9 {} \;

# Add the build-file in the package
mkdir -p $PKG/usr/src/$NAME-$VERSION
cp "$CWD/build-$NAME.sh" "$PKG/usr/src/$NAME-$VERSION/build-$NAME.sh"
chmod a+x "$PKG/usr/src/$NAME-$VERSION/build-$NAME.sh"

# Make sure ownerships and permissions are sane
cd $PKG
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 {} \;

# Make the package
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

# Calculate md5sum
cd $CWD/
md5sum $NAME-$VERSION-$ARCH-$BUILD.txz > $NAME-$VERSION-$ARCH-$BUILD.md5

# Remove source-code and temporary install directories
# since they are not needed anymore
rm -rf $CWD/$NAME-$VERSION
rm -rf $PKG

# log
) 2>&1 | tee "$NAME-$VERSION.log"
