#!/bin/bash
# packager          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# package			: libcdio
# homepage			: http://www.gnu.org/software/libcdio/
# source location	: http://ftp.gnu.org/gnu/libcdio/libcdio-0.82.tar.gz
# dependency        : glibc,vcdimager,libcddb 

softname='libcdio'
softversion='0.82'
packageversion='64.2'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
src="$buildir/$softname-$softversion"
srcpkg="$buildir/$softname-$softversion.tar.gz"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://ftp.gnu.org/gnu/$softname/$softname-$softversion.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/doc
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc 
       |-----handy-ruler------------------------------------------------------|
libcdio: libcdio - a library for CD-ROM and CD image access
libcdio:
libcdio: Applications wishing to be oblivious of the OS- and 
libcdio: device-dependent properties of a CD-ROM or of the specific 
libcdio: details of various CD-image formats may benefit from using 
libcdio: this library.
libcdio: 
libcdio:
libcdio:
libcdio:
libcdio:
EOF

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

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

# build
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"
    
./configure \
	--program-prefix="" \
	--program-suffix="" \
	--prefix="/usr" \
	--sysconfdir="/etc" \
	--localstatedir="/var" \
	--mandir="/usr/man" \
	--infodir="/usr/info" \
	--enable-vcd-info="yes" \
	--enable-static="no" \
	--enable-shared="yes" \
	--build=$arch-slackware-linux || exit

make 
make install DESTDIR=$dest

cp $src/libcdio_paranoia.pc $dest/usr/lib/pkgconfig
cp $src/libcdio_cdda.pc $dest/usr/lib/pkgconfig

# add 'default' files
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      AUTHORS COPYING ChangeLog INSTALL NEWS README README.libcdio THANKS TODO\
      $dest/usr/doc/$softname-$softversion

# strip executables
( 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
)

# compress info pages
( cd $dest/$prefix/info
  find . -name "*.info" | xargs gzip -9
)

# ZenBuild
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 "cxxlibs,gcc,libcddb,libxml2,ncurses" > $package.dep

# .src
echo http://users.zenwalk.org/user-accounts/bip/ISO/l/libcdio/source/build-libcdio.sh > $package.src
echo $source >>$package.src

# erase src
rm -rf $src
rm -rf $dest

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