#!/bin/sh
# Heavily based on the Slackware 11.0 SlackBuild
# Requirements: freepascal
# http://www.lazarus.freepascal.org/

# definiamo alcune variabili contenenti la directory corrente e quella temporanea di lavoro:
CWD=`pwd`
NAME=lazarus
VERSION=0.9.22
ARCH=i386
BUILD=1ld
if ["$TMP" = ""]; then
TMP=/tmp
fi
PKG=$TMP/packages-lazarus
SRC=$TMP/$NAME-$VERSION
# Settiamo qualche altra variabile per velocizzare il lavoro:
if [ ! -d $SRC ]; then
mkdir -p $SRC # posizione dove scompattiamo i binari
fi
if [ ! -d $PKG ]; then
mkdir -p $PKG # posizione dove creiamo il package
fi
echo "+------------------+"
echo "|  $NAME-$VERSION  |"
echo "+------------------+"
cd $SRC
echo
# get installed fpc version
echo "Verifico l'installazione del freePascal ..."
if ! which ppc386  ; then
	echo ERRORE: FreePascal non installato
	exit
fi
FPCVER=`ppc386 -l | head -n 1 | awk '{print $5}'`
echo "Trovato FreePascal versione: $FPCVER"
echo "Proseguo con l'installazione"
# scompattiamo i sorgenti
echo "Scompattiamo i Sorgenti di Lazarus (Fase 1) ..."
tar zxf $CWD/$NAME-$VERSION-0.tar.gz
echo "... e i Sorgenti del FreePascal (Fase 2)"
tar xf $CWD/fpc-src-2.0.4.source.tgz
mv $SRC/fpc $SRC/lazarus/fpcsrc
echo "Compiliamo Lazarus"
cd $SRC/lazarus
FPCCfg="/etc/fpc.cfg"
if [ -n "$FPCCfg" ]; then
  MAKEOPTS=" -n @$FPCCfg"
fi
make
make bigide OPT="$MAKEOPTS" USESVN2REVISIONINC=0
make tools OPT="$MAKEOPTS"
make lazbuilder OPT="$MAKEOPTS"
# build gtk2 .ppu
export LCL_PLATFORM=gtk2
make lcl ideintf packager/registration bigidecomponents OPT="$MAKEOPTS"
export LCL_PLATFORM=
strip lazarus
strip startlazarus
strip lazbuild
# Muovo i binari nel directory del package
echo "Costruiamo la struttura del pacchetto ...."
mkdir -p $PKG/usr/lib/
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/share/pixmaps
mkdir -p $PKG/usr/share/applications
mkdir -p $PKG/usr/man/man1
mkdir -p $PKG/usr/doc/
cd $SRC
mv lazarus $PKG/usr/lib/
cd $PKG/usr/lib/lazarus/
cp images/ide_icon48x48.png $PKG/usr/share/pixmaps/lazarus.png
sed -i -e 's/Exec=\/usr\/share\/lazarus\/startlazarus %f/Exec=\/usr\/bin\/startlazarus %f/g' install/lazarus.desktop
cp install/lazarus.desktop $PKG/usr/share/applications/lazarus.desktop
ln -sf /usr/lib/lazarus/lazarus $PKG/usr/bin/lazarus
ln -sf /usr/lib/lazarus/startlazarus $PKG/usr/bin/startlazarus
ln -sf /usr/lib/lazarus/lazbuild $PKG/usr/bin/lazbuild
cat docs/lazbuild.1 | gzip > $PKG/usr/man/man1/lazbuild.1.gz
ln -sf $PKG/usr/lib/lazarus/docs/ $PKG/usr/doc/$NAME-$VERSION
mv COPYING* README.txt docs/
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
rm -rf $TMP/$NAME-$VERSION

# sistelo l'icona del file desktop
( cd $PKG/usr/share/applications
  sed -i "s|Icon=lazarus.png|Icon=/usr/share/pixmaps/lazarus.png|g" lazarus.desktop
)

# impostiamo i permessi
echo "Impostaimo i permessi ...."
chown -R root:root $PKG
chmod -R +x $PKG/usr/bin/*
chmod -R 644 $PKG/usr/doc/$NAME-$VERSION
chmod -R 666 $PKG/usr/lib/$NAME/examples # Premetto il build degli esempi
# Permetto l'accesso alle directory 
cd $PKG/usr/lib/$NAME/
FILE_DIR=`find ./ -type d -print`
for dir in $FILE_DIR
    do
	chmod +x $dir
    done
# copiamo slack-desc in install
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
#cat $CWD/doinst.sh > $PKG/install/doinst.sh
# Creiamo il pacchetto con makepkg
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz
# eliminiamo le dir di lavoro qualora avessimo eseguito lo SlackBuild con l'opzione --cleanup
if [ "$1" = "--cleanup" ]; then
    rm -rf $TMP/$NAME-$VERSION
    rm -rf $PKG
fi



