#!/bin/bash
# packager          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage			: http://www.guzu.net/linux/hddtemp.php
# source location	: http://mirrors.aixtools.net/sv/hddtemp/hddtemp-0.3-beta15.tar.bz2
# dependency        : 
# last revised      : 1.10.2009

softname='hddtemp'
softversion='0.3beta15'
softversionsrc='0.3-beta15'
packageversion='1fb'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversionsrc.tar.bz2"
src="$buildir/$softname-$softversionsrc"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://mirrors.aixtools.net/sv/hddtemp/$softname-$softversionsrc.tar.bz2

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/etc/hddtemp
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc
       |-----handy-ruler------------------------------------------------------|
hddtemp: hddtemp (Reads hard disk S.M.A.R.T. info and reports temperature)
hddtemp:
hddtemp: hddtemp is a small utility (daemonizable) that gives you the  
hddtemp: temperature of your hard drive by reading S.M.A.R.T. informations 
hddtemp: (for drives that support this feature). 
hddtemp:
hddtemp:
hddtemp:
hddtemp:
hddtemp:
hddtemp:
EOF

# check if source is present
if [ -f $softname-$softversionsrc.tar.bz2 ]; 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 xvjf $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" \
	--with-db-path=/etc/hddtemp/hddtemp.db \
	--enable-static="no" \
	--enable-shared="yes" \
	--build=$arch-slackware-linux || exit

make
make install DESTDIR=$dest

cp $buildir/hddtemp.db $dest/etc/hddtemp/hddtemp.db.new

# replace the new file if needed
cat << "EOF" > $dest/install/doinst.sh
config() {
  NEW="$1"
  OLD="`dirname $NEW`/`basename $NEW .new`"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/hddtemp/hddtemp.db.new
EOF

# add 'default' files
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      ABOUT-NLS AUTHORS COPYING INSTALL LICENSE NEWS README TODO  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
)

# ompress 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
/sbin/makepkg -l y -c n $dest.txz

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

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

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