#!/bin/bash
# This is a script to build a Zenwalk package. Check the Zenwalk homepage: www.zenwalk.org
#
# SOFTWARE			: libmpd
# MAINTAINER       	: skywatcher (s.skywatcher|AT|gmail|DOT|org, simone antoniucci)
# LATEST REVISION	: 2010-07-24
# BUILD-ONLY DEPS	: -
# NOTES				: -

### Checking for unbound variables and exit on errors
set -u -e

### Repo
URL=http://zenwalk.pinguix.com/user-accounts/skywatcher

### Pkg variables
NAME="libmpd"
VERSION="0.20.0"
ARCH="i486"
CPU="i686"
BUILD="66.1"
EXT="txz"
HOMEPAGE="http://www.sarine.nl/libmpd"
SRCLNK="http://downloads.sourceforge.net/musicpd/$NAME-$VERSION.tar.gz"
DESCRIPTION="library for MPD server connection"
DOCS="AUTHORS ChangeLog COPYING NEWS README"
ADDSRC=""
INCSRC=""
ADDDEP=""
XCLDEP=""

### Script variables
CWD=`pwd`
PKG=$NAME-$VERSION-$ARCH-$BUILD
URL=$URL/$NAME/$VERSION
PKGDIR=$CWD/$NAME-install
SRCDIR=$CWD/$NAME-$VERSION
SRCPKG=`echo $SRCLNK | cut -d\  -f1 | rev | cut -d/ -f1 | rev`

### Flags and jobs
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"
NJOBS="-j4"

### Cleaning
rm -rf $PKGDIR
rm -rf $SRCDIR

### Making default package dirs
install -dv $SRCDIR $PKGDIR/{install,usr/src/$NAME-$VERSION}

### Getting sources
for src in $SRCLNK; do \
	source=`echo $src | rev | cut -d/ -f1 | rev` 
	if [ -f $source ]; then
		echo "Source file $source is already present, no need to download it";
	else
		echo "Downloading $source..."
		wget -c -nv $SRCLNK
	fi
done

### Extracting main sources
tar xfv $SRCPKG -C $SRCDIR --strip-components=1
#~ unzip $SRCPKG -d $SRCDIR ...

### Building ===========================================================
cd $SRCDIR

#~ # Repackaging
#~ install -dv $PKGDIR/usr/bin
#~ install -dv $PKGDIR/usr/share/$NAME

#~ # Modifying files
#~ sed -i s::: @@@@@  

#~ # Patching files
#~ cd $SRCDIR/@@@@@
#~ patch @@@@@ $CWD/$@@@@@ || exit 1

# Compiling and installing
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --mandir=/usr/man --docdir=/usr/doc/$NAME-$VERSION \
|| exit 1
make $NJOBS || exit 1
make install DESTDIR=$PKGDIR || exit 1
#~ 
#~ python setup.py install --prefix=/usr --root=$PKGDIR || exit 1

#~ # Exec
#~ install -dv $PKGDIR/usr/bin
#~ cat >> $PKGDIR/usr/bin/$NAME << EOF
#~ #!/bin/bash
#~ LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so /usr/libexec/camorama "\$@"
#~ EOF
#~ chmod 755 $PKGDIR/usr/bin/camorama

#~ # Gconf stuff
#~ export GCONF_CONFIG_SOURCE="xml::$PKGDIR/etc/gconf/gconf.xml.defaults"
#~ if [ -d $PKGDIR/etc/gconf/schemas ]; then
	#~ install -dv -m755 $PKGDIR/etc/gconf/gconf.xml.defaults
	#~ SCHEMAS=$PKGDIR/etc/gconf/schemas
	#~ for schema in $SCHEMAS/*.schemas; do
		#~ gconftool-2 --makefile-install-rule $schema
	#~ done
	#~ ( cd $PKGDIR/etc/gconf ; find . -type d -exec chmod 755 {} \; )
	#~ ( cd $PKGDIR/etc/gconf ; find . -type f -exec chmod 644 {} \; )
#~ fi

# Icon
#~ install -Dv $SRCDIR/@@@@@ $CWD/@@@@@ $PKGDIR/usr/share/icons/hicolor/48x48/apps/$NAME.png
#~ install -Dv $SRCDIR/@@@@@ $CWD/@@@@@ $PKGDIR/usr/share/icons/hicolor/scalable/apps/$NAME.svg
#~ install -Dv $PKGDIR/usr/share/pixmaps/@@@@@ $PKGDIR/usr/share/icons/hicolor/48x48/apps/$NAME.png
#~ install -Dv $PKGDIR/usr/share/pixmaps/@@@@@ $PKGDIR/usr/share/icons/hicolor/scalable/apps/$NAME.svg
#~ mv $PKGDIR/@@@@@ 
#~ cd $ICONDIR && convert $NAME.@@@ $NAME.png
#~ cd $ICONDIR && mogrify -resize 48x48 $NAME.png

# Desktop entry
#~ sed -i s::: $PKGDIR/usr/share/applications/$NAME.desktop
#~
#~ install -dv $PKGDIR/usr/share/applications
#~ cat > $PKGDIR/usr/share/applications/$NAME.desktop << EOF
#~ [Desktop Entry]
#~ Name=
#~ Comment=
#~ Exec=
#~ Icon=
#~ Type=Application
#~ Categories=@@@@@;@@@@@;
#~ Terminal=false
#~ StartupNotify=true
#~ Encoding=UTF-8
#~ EOF

# Docs, man, info
cd $SRCDIR
install -dv $PKGDIR/usr/doc/$NAME-$VERSION
install -v $DOCS $PKGDIR/usr/doc/$NAME-$VERSION
#~ install -dv $PKGDIR/usr/man/man#?
#~ install -v $PKGDIR/usr/share/man#/ $PKGDIR/usr/man/man#

#~ mandir=`find $PKGDIR -type d -name man`
#~ new_mandir=`echo $mandir | sed s:"share":"":`
#~ mv -v $mandir $new_mandir 

#~ infodir=`find $PKGDIR -type d -name info`
#~ new_infodir=`echo $infodir | sed s:"share":"":`
#~ mv -v $infodir $new_infodir 

# Unnedeed stuff
#~ rm -fr $PKGDIR/

### Slack-desc 
#    |-----handy-ruler------------------------------------------------------|
cat > $PKGDIR/install/slack-desc << EOF
$NAME: $NAME - $DESCRIPTION
$NAME:  
$NAME: Libmpd is a library to easily connect to a mpd server. It's wraps
$NAME: around libmpdclient and provides a higher level api.
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: 
$NAME: $HOMEPAGE
EOF

### Doinstall
#~ cat > $PKGDIR/install/doinst.sh << EOF
#~ EOF

#~ # dotnew
#~ mv $PKGDIR/etc/@@@ $PKGDIR/etc/@@@.new
#~ find $PKGDIR/etc -type f -exec mv {} {}.new \; 
#~ 
#~ cat > $PKGDIR/install/doinst.sh << "EOF"
#~ config(){
#~ NEW="$1.new"
#~ OLD="$1"
#~ if [ ! -r $OLD ]; then
	#~ mv $NEW $OLD
#~ elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then
	#~ rm $NEW
#~ fi
	 	#~ }
#~ config etc/@@@
#~ EOF
#~ cd $PKGDIR && find etc -type f -name "*.new" -exec echo config {} \; | sed s:".new":: >> $PKGDIR/install/doinst.sh

#~ # infodir 
#~ # first remove any /usr/info/dir file from the package!
#~ cat > $PKGDIR/install/doinst.sh << EOF
#~ if [ -x usr/bin/install-info ]
#~ then
   #~ usr/bin/install-info --info-dir=usr/info usr/info/XXXXXXX.info.gz 2>/dev/null
#~ fi
#~ EOF

#~ # mime & desktop 
#~ cat > $PKGDIR/install/doinst.sh << EOF
#~ # Update mime and desktop database
#~ if [ -x /usr/bin/update-desktop-database ]; then
  #~ /usr/bin/update-desktop-database -q /usr/share/applications >/dev/null 2>&1
#~ fi
#~ 
#~ if [ -x /usr/bin/update-mime-database ]; then
  #~ /usr/bin/update-mime-database /usr/share/mime >/dev/null 2>&1
#~ fi
#~ EOF

#~ # scrollkeeper
#~ # use disable scrollkeeper in configure
#~ cat > $PKGDIR/install/doinst.sh << EOF
#~ # Update scrollkeeper database
#~ if [ -x usr/bin/scrollkeeper-update ]; then
        #~ usr/bin/scrollkeeper-update > /dev/null 2>&1
#~ fi
#~ EOF

# ======================================================================

### Managing and zipping man and info files
if [ -d $PKGDIR/usr/man ]; then
	find $PKGDIR/usr/man -type f -exec gzip -9 -v {} \;
fi
if [ -d $PKGDIR/usr/info ]; then
	find $PKGDIR/usr/info -type f -exec gzip -9 -v {} \;
fi

### Adding build-file and included stuff to the package
for file in $0 $INCSRC
	do cp -av $CWD/$file $PKGDIR/usr/src/$NAME-$VERSION
done
chmod a+x $PKGDIR/usr/src/$NAME-$VERSION/build-$NAME.sh

### Stripping binaries
cd $PKGDIR
set +e
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

### Ownerships and permissions
cd $PKGDIR
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 {} \;
set -e

#~ ### Special permissions
#~ chmod @@@@@

### Making the package
makepkg -l y -c n $CWD/$PKG.$EXT
cd $CWD

### md5
md5sum $PKG.$EXT > $PKG.md5

### dep
ADD=$ADDDEP EXCLUDE=$XCLDEP requiredbuilder -z $PKG.$EXT > $PKG.dep
#~ ADD=$ADDDEP EXCLUDE=$XCLDEP depfinder $PKG.$EXT > $PKG.dep

### slackdesc
cat $PKGDIR/install/slack-desc > $PKG.txt

### src
echo $SRCLNK > $PKG.src
for link in $0 $ADDSRC 
	do echo $URL/$link >> $PKG.src
done

### Cleaning
DEL_BLDDIR=${DEL_BLDDIR:-1}
if [ "$DEL_BLDDIR" -eq "1" ]; then
	rm -rf $SRCDIR
	rm -rf $PKGDIR
fi

### Done. Ciao!
