#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage           : http://ubuntrucchi.wordpress.com/progetti-projects/gwakeonlan/
# source location    : http://dl.box.net/dl/331376320/ab0e3774f190bc69cd2fbe5d14aa3bd4?a=92012a92d159bec8bbf8b28863472c1e&m=168566327,11211&d=1&x=1&c=deb92237a7f61c9a54e2136b99b28840
# last revised       : 7.10.2009
# dep                : 

softname='gwakeonlan'
softversion='0.2'
packageversion='1fb'

arch='noarch'
cpu='i686'

prefix=/usr

# log ;)
(

# parameters needed for the build process
buildir=$(pwd)
src="$buildir"
srcpkg="$buildir/"$softname"_"$softversion"_all.tar.gz"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=/"$softname"_"$softversion"_all.tar.gz

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/$prefix/bin
mkdir -p $dest/usr/share/applications
mkdir -p $dest/usr/share/gwakeonlan/
mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc 
          |-----handy-ruler------------------------------------------------------|
gwakeonlan: gwakeonlan (Turn on your machines on the net using the WOL)
gwakeonlan:
gwakeonlan: With gWakeOnLan you can turn on your turned off machines using the 
gwakeonlan: wake on lan feature.
gwakeonlan: Just a click to turn them all simultaneously.
gwakeonlan: 
gwakeonlan: 
gwakeonlan:
gwakeonlan:
gwakeonlan:
gwakeonlan:
EOF

# check if source is present
if [ -f "$softname"_"$softversion"_all.tar.gz ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

# extract the source code
cd $buildir
tar xzvf $srcpkg
cd $src	

# build
mv -v gwakeonlan $dest/usr/share/gwakeonlan/
mv -v ui $dest/usr/share/gwakeonlan/ui
mv -v locale $dest/usr/share/locale
mv -v doc $dest/usr/share/gwakeonlan/doc
mv -v gfx $dest/usr/share/gwakeonlan/gfx
rm -v $dest/usr/share/locale/{*.pot,*.sh}

cat <<EOF > $dest/usr/bin/gwakeonlan
#!/bin/sh
exec /usr/share/gwakeonlan/gwakeonlan
EOF

chmod 755 $dest/usr/bin/gwakeonlan

# make icon
convert $dest/usr/share/gwakeonlan/gfx/$softname.svg -resize 48x48 $dest/usr/share/icons/hicolor/48x48/apps/$softname.png 

# make the .desktop file
cat << "EOF" > $dest/usr/share/applications/$softname.desktop
[Desktop Entry]
Encoding=UTF-8
Name=gwakeonlan
Comment=Gwakeonlan
Exec=gwakeonlan
Terminal=false
Type=Application
Categories=Application;Network;
Icon=gwakeonlan
EOF

# add 'default' files
	mkdir -p $dest/usr/doc/$softname-$softversion
    mv -v \
      $dest/usr/share/gwakeonlan/doc/changelog\
      $dest/usr/doc/$softname-$softversion
	
#  strip
( 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
( cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)

# SalixBuild
cd $buildir
cp build-$softname.sh $dest/$prefix/src/$softname-$softversion

# set target permissions
chown -R root.root $dest

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

# real packaging work
cd $dest
makepkg -l y -c n $dest.txz

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# .dep
cd $buildir
echo -e "python,pygtk,pygobject,pycairo" > $package.dep

# .src
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/xap/gwakeonlan/build-gwakonlan.sh > $package.src
echo $source >> $package.src

# erase
rm -rf doc
rm -rf gfx
rm -rf ui
rm -rf $dest

# log ;)
) 2>&1 | tee "$softname-$softversion-build.log"
