#!/bin/sh
# roxterm slackbuild - Dani - dani@slacky.it
# Heavily based on the Slackware 12.2 SlackBuild

NICE=10

function set_perm {
	echo -e -n "\nAdjusting permission in $1...";
	chown -R root:root $1 &> /dev/null
	if [ $? != 0 ]; then
	 echo -e "Failed.\n";
	 exit 1
	fi
  	 
	find $1 -perm 664 -exec chmod 644 {} \;
	find $1 -perm 600 -exec chmod 644 {} \;
	find $1 -perm 444 -exec chmod 644 {} \;
	find $1 -perm 400 -exec chmod 644 {} \;
	find $1 -perm 440 -exec chmod 644 {} \;
	find $1 -perm 777 -exec chmod 755 {} \;
	find $1 -perm 775 -exec chmod 755 {} \;
	find $1 -perm 511 -exec chmod 755 {} \;
	find $1 -perm 711 -exec chmod 755 {} \;
	find $1 -perm 555 -exec chmod 755 {} \;
 
 	echo "Done.";
}
 
function strip_files {
	
	echo -n "Stripping files in $1...";
	
	find $1 | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	find $1 | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	find $1 | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null

	echo "Done.";
}

NAME=roxterm
VERSION=1.14.2
ARCH=${ARCH:-i486}
CHOST=${CHOST:-i486}
BUILD=1dd

CWD=`pwd`
PKG=/tmp/$NAME-build
TGZ=$PKG/TGZ

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -e $CWD/$NAME-$VERSION.tar.gz ]; then
 wget http://downloads.sourceforge.net/roxterm/$NAME-$VERSION.tar.gz || exit 1
fi

echo -n "Removing old files..."
rm -rf $PKG 
if [ $? != 0 ]; then
	echo -n "Failed.\n";
	exit 1;
fi
mkdir -p $TGZ
echo -e "Done.\n";

cd $PKG
tar xzvf $CWD/$NAME-$VERSION.tar.gz || exit 1
cd $NAME-$VERSION || exit 1
 
set_perm `pwd`;

mkdir -p $TGZ/usr/doc/$NAME-$VERSION
cp -av AUTHORS COPYING ChangeLog INSTALL NEWS \
       README TODO                            \
       $TGZ/usr/doc/$NAME-$VERSION

CFLAGS="$SLKCFLAGS"          \
CXXFLAGS="$SLKCFLAGS"        \
./configure                  \
  --prefix=/usr              \
  --sysconfdir=/etc          \
  --docdir=/usr/doc/$NAME-$VERSION \
  --program-prefix=          \
  --program-suffix=          \
  --build=$CHOST-slackware-linux || exit 1
echo -e "\nPress Enter.\n";
read
nice -n$NICE make -j2 || exit 1
make install DESTDIR=$TGZ

(
cd $TGZ
cp -af usr/share/doc/$NAME/* usr/doc/$NAME-$VERSION
rm -rf usr/share/doc/$NAME/
 (
  cd usr/doc
  ln -sv $NAME-$VERSION $NAME
 )
)

set_perm $TGZ;
strip_files $TGZ;

rm -rf $TGZ/usr/share/man
rm -rf $TGZ/usr/share/doc

mkdir -p $TGZ/install
cat $CWD/slack-desc > $TGZ/install/slack-desc
cat $CWD/slack-desc > $TGZ/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $TGZ/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

cd $TGZ
echo "Making slack-required...";
requiredbuilder -v -y -s $CWD $TGZ
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "-clean" ]; then
 echo -n "Cleaning..."
 rm -rf $PKG
 echo -e "Done.\n";
fi

exit 0;
