#!/bin/bash

#---( Build Script for: Zenwalk 
#---( Package name: milkytracker
#---( Home Page: http://www.milkytracker.net
#---( Source URL: http://www.milkytracker.net/files/
#---( Packager: sebastian <zenwalkuser> seb@seb2.dyndns.org



#------------------( Set Package Name, Version and Source URL )


soft='milkytracker'
softversion='0.90.80'
packageversion='64.1'
srcname='milkytracker'

sourceurl="http://www.milkytracker.net/files/$srcname-$softversion.tar.bz2"

#------------------( Check for source files )


if [ -f $srcname-$softversion.tar.bz2  ]; then
   echo "Source file .. ok"
else
   echo "Source file does not exit. Downloading it ..."
   wget -c $sourceurl
fi


#------------------( Set Package CPU Architecture Optimizations )


arch='i486'
cpu='i686'


#------------------( Set Build Location Variables )


buildir=$(pwd)
srcpkg="$buildir/$srcname-$softversion.tar.bz2"
src="$buildir/$soft-$softversion"
package="$soft-$softversion-$arch-$packageversion"
dest="$buildir/$package"


#------------------( Clean, prepare the work area and extract the source code )


cd $buildir
rm -rf $src
tar -xvf $srcpkg
rm -f $dest.tgz
rm -rf $dest
mkdir -p $dest


#------------------( Set Build Ownerships and CFLAGS and CXXFLAGS )


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 {} \;


export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"


#------------------( Configure, Make And Install The Software )


cd $src

./configure --prefix=/usr \
--sysconfdir=/etc \
--localstatedir=/var \
--mandir=/usr/man \
--with-alsa \
--without-jack

make
make install DESTDIR=$dest


#------------------( Strip Executables (Comment out these lines if debugging info is needed) )


( cd $dest
  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
)


#------------------( Compress Man Pages and Info Pages (if exist) and move to the Correct Location )


if [ -d $dest/usr/man ]; then
( cd $dest/usr/man
  find . -name "*.?" | xargs gzip -9
)
fi


if [ -d $dest/usr/info ]; then
( cd $dest/usr/info
  find . -name "*.info" | xargs gzip -9
  find . -name "*.info-?" | xargs gzip -9
  rm -f dir
)
fi


#------------------( Move Documentation To Correct Location )


mkdir -p $dest/usr/doc/$soft-$softversion
cp -a \
AUTHORS COPYING INSTALL NEWS README ChangeLog docs/readme_unix docs/MilkyTracker.html \
  $dest/usr/doc/$soft-$softversion


#------------------( Copy The Icon To The Correct Location )


mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
cd $dest/usr/share/icons/hicolor/48x48/apps/ 
cp -a $buildir/$soft-$softversion/resources/pictures/carton.png $soft.png
cd $dest


#------------------( Copy Build The Script )


mkdir -p $dest/usr/src/$soft-$softversion
cp $buildir/build-$soft.sh $dest/usr/src/$soft-$softversion/build-$soft.sh


#------------------( Create The Slackdesc )


mkdir -p $dest/install
cat <<EOF > $dest/install/slack-desc 
     |-----handy-ruler------------------------------------------------------|
$soft: $soft an open source, multi-platform music tracker
$soft: 
$soft: MilkyTracker is an open source, multi-platform music application 
$soft: for creating .MOD and .XM module files.
$soft: It attempts to recreate the module replay and user experience
$soft: of the popular DOS program Fasttracker II, with special playback
$soft: modes available for improved Amiga ProTracker 2/3 compatibility.
$soft: 
$soft: 
$soft: 
$soft: 
EOF


#------------------( Create a proper desktop file )


mkdir -p $dest/usr/share/applications/
cat <<EOF > $dest/usr/share/applications/$soft.desktop
[Desktop Entry]
Encoding=UTF-8
Name=milkytracker
Name[de]=milkytracker
Exec=milkytracker
Icon=milkytracker
Terminal=false
Type=Application
StartupNotify=true
Categories=Application;AudioVideo;
EOF


#------------------( Compress The Package And Clean Up The Work Area )


cd $dest
makepkg -l y -c n ../$package.txz
rm -rf $src
rm -rf $dest


#------------------( Generate the MD5 Check Sum and create dependency file)


cd $buildir
md5sum $package.txz > $package.md5

requiredbuilder -z $package.txz > $package.dep
