#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://www.kde-apps.org/content/show.php?content=28107
# Packager Andrea Sciucca Gohanz ( gohanz at infinito.it)
# http://www.slacky.it
# Required: Korundum

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

PKG=$TMP/package-kenigma
NAME=kenigma
VERSION=1.2.1
ARCH=${ARCH:-noarch}
BUILD=1as

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

cd $TMP
tar xvzf $CWD/$NAME-$VERSION.tar.gz

echo -e "\E[0;32m+--------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+--------------------------------+\E[0;0m"

cd $NAME

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

chown -R root:root .

PREFIX=$(kde-config --prefix)
BINPREFIX="$PREFIX/bin"
SHAREPREFIX="$PREFIX/share/apps/kenigma"
DESKTOPPREFIX="$PREFIX/share/applnk/Edutainment"

RUBY=$(ls /usr/bin | grep -m 1 ^ruby)

echo "#!/bin/bash" >> kenigma
echo "cd $SHAREPREFIX && $RUBY main.rb" >> kenigma

chmod +x kenigma

mkdir -p $PKG/$BINPREFIX
mkdir -p $PKG/$SHAREPREFIX
mkdir -p $PKG/$DESKTOPPREFIX
mkdir -p $PKG/usr/share/icons/default.kde/48x48/apps


cp -pf kenigma $PKG/$BINPREFIX
cp -f $CWD/kenigma.desktop $PKG/$DESKTOPPREFIX
cp -f main.rb $PKG/$SHAREPREFIX
cp -f rotors.rb $PKG/$SHAREPREFIX
cp -f stecker.rb $PKG/$SHAREPREFIX
cp -f xmlsettings.rb $PKG/$SHAREPREFIX
cp -f options.rb $PKG/$SHAREPREFIX
cp -f textwindow.rb $PKG/$SHAREPREFIX
cp -f kenigma.dtd $PKG/$SHAREPREFIX
cp -a $CWD/kenigma.png $PKG/usr/share/icons/default.kde/48x48/apps

rm kenigma

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a  README $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION

cd $PKG
chown -R root:root .
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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