#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# Home: http://www.freecol.org/
# Packager: Davide Locatelli ( l.davide AT tiscali.it )

CWD=`pwd`
if [ "$TMP" = "" ]; then
 TMP=/tmp/tgz
fi

PKG=$TMP/package-freecol
NAME=freecol
VERSION=0.8.2
TARGET=i686
ARCH=${ARCH:-i686}
BUILD=1dk

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O2 -march=athlon-xp -mtune=athlon-xp"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP
tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd $NAME
#chown -R root.root .
#./configure --prefix=/usr \
#	--enable-animation \
#	$TARGET-slackware-linux
#make
#make install DESTDIR=$PKG

mkdir -p $PKG/usr/share/freecol
mkdir -p $PKG/usr/share/applications
mkdir -p $PKG/usr/bin

cp -a FreeCol.jar data freecol freecol.xpm jars splash.jpg \
	$PKG/usr/share/freecol
chmod 755 $PKG/usr/share/freecol/freecol

cat > $PKG/usr/bin/freecol << EOF
#!/bin/sh

(cd /usr/share/freecol && ./freecol)
EOF
chmod 755 $PKG/usr/bin/freecol

cat > $PKG/usr/bin/freecol_window << EOF
#!/bin/sh

(cd /usr/share/freecol && ./freecol --windowed)
EOF
chmod 755 $PKG/usr/bin/freecol_window

cat > $PKG/usr/share/applications/freecol.desktop << EOF
[Desktop Entry]
Encoding=UTF-8
Name=FreeCol
GenericName=Free Colonization
Comment=Colonization clone
Exec=freecol
Icon=/usr/share/freecol/freecol.xpm
StartupNotify=true
Terminal=false
Type=Application
Categories=Java;Game;
EOF

cat > $PKG/usr/share/applications/freecolw.desktop << EOF
[Desktop Entry]
Encoding=UTF-8
Name=FreeCol window mode
GenericName=Free Colonization
Comment=Colonization clone
Exec=freecol_window
Icon=/usr/share/freecol/freecol.xpm
StartupNotify=true
Terminal=false
Type=Application
Categories=Java;Game;
EOF

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a COPYING FreeCol.pdf README \
	$PKG/usr/doc/$NAME-$VERSION

#( 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
#)

#( cd $PKG/usr/man
#  find . -name "*.?" | xargs gzip -9
#)

chown -R root.bin $PKG/usr/bin
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
cd $PKG
ADD="jre >= 6u11-i586-1" \
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP
fi
