#!/bin/bash
# This is a script to build a Zenwalk package. Check the Zenwalk homepage: www.zenwalk.org
#
# SOFTWARE			: mpd
# MAINTAINER       	: skywatcher (s.skywatcher|AT|gmail|DOT|org, simone antoniucci)
# LATEST REVISION	: 2011-03-23
# 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="mpd"
VERSION="0.15.16"
ARCH="i486"
CPU="i686"
BUILD="70.1"
EXT="txz"
HOMEPAGE="http://mpd.wikia.com/wiki/Music_Player_Daemon_Wiki"
SRCLNK="http://downloads.sourceforge.net/musicpd/$NAME-$VERSION.tar.bz2"
DESCRIPTION="The Music Player Daemon"
DOCS="AUTHORS COPYING INSTALL NEWS README UPGRADING"
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

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

# Compiling and installing
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --mandir=/usr/man --docdir=/usr/doc/$NAME-$VERSION \
			--enable-ipv6 \
			--enable-oss \
			--enable-alsa \
			--enable-mvp \
			--enable-oggflac \
			--enable-flac \
			--enable-aac \
			--enable-audiofile \
			--enable-mpc \
			--enable-id3 \
			--enable-lsr \
			--enable-sqlite \
			--enable-curl \
			--enable-mms \
			--enable-ffmpeg \
			--enable-vorbis \
			--enable-wavpack \
			--enable-vorbis-encoder \
			--enable-lame-encoder \
			--enable-shout \
			--enable-lastfm \
			--enable-jack \
			--disable-pulse \
			|| exit 1
		
make $NJOBS || exit 1
make install DESTDIR=$PKGDIR || exit 1

### provide mpd initialization script
cat > $PKGDIR/usr/bin/mpd-initial.sh << EOF
#!/bin/bash
# This script will set up the .mpd folder and the .mpdconf
# configuration file in the home directory of the user who launches it.

echo ""
echo "Checking for existence of ~/.mpd ..."
if [[ -d ~/.mpd ]]; then
	echo "~/.mpd found"
	echo ""
	echo "Checking for existence of ~/.mpd/playlists"
	if [[ -d ~/.mpd/playlists ]]; then
		echo "~/.mpd/playlists found"
	else
		echo "~/.mpd/playlists not found"
		echo "creating ~/.mpd/playlists"
		mkdir -pv ~/.mpd/playlists
	fi
else
	echo "~/.mpd not found"
	echo "creating ~/.mpd and ~/.mpd/playlists"
	mkdir -pv ~/.mpd/playlists
fi

echo ""
echo "Checking for existence of ~/.mpdconf ..."
if [[ -f ~/.mpdconf ]]; then
	echo "~/.mpdconf found"
else
	echo "~/.mpdconf not found"
	echo "creating ~/.mpdconf"
	cp -v /usr/doc/mpd-$VERSION/mpdconf.example ~/.mpdconf
	sed -i 's|~/music|~/Music|' ~/.mpdconf
	echo "Music directory set to ~/Music"
fi

echo ""
echo "MPD was set up for the current user. Please run:"
echo "mpd --create-db"
echo "to create a database of music files. You can change the paths"
echo "used by MPD and other settings by editing your ~/.mpdconf file."
echo ""
EOF
chmod a+x $PKGDIR/usr/bin/mpd-initial.sh

# Icon

# Desktop entry

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

# Unnedeed stuff

### Slack-desc 
#    |-----handy-ruler------------------------------------------------------|
cat > $PKGDIR/install/slack-desc << EOF
$NAME: $NAME - $DESCRIPTION
$NAME:  
$NAME: Music Player Daemon (MPD) allows remote access for playing music
$NAME: (MP3, Ogg Vorbis, FLAC, AAC, Mod, and wave files) and managing 
$NAME: playlists. MPD is designed for integrating a computer into a stereo
$NAME: system that provides control for music playback over a local network.
$NAME: It also makes a great desktop music player, especially if you are a 
$NAME: console junkie, like frontend options, or restart X often.
$NAME: 
$NAME: 
$NAME: $HOMEPAGE
EOF

### Doinstall
cat > $PKGDIR/install/doinst.sh << EOF
echo "################################################################"
echo "NOTE: The users who have not yet configured MPD can run the"
echo "mpd_intial.sh script to set up the required configuration files"
echo "################################################################"
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

### 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

### 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!
