#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
# http://cpufreqd.sourceforge.net/
# Rewritten by alphamanitine for www.slacky.eu
CWD=`pwd`
if ["$TMP" = ""]; then
 TMP=/tmp
fi
PKG=$TMP/package-cpufreqd
NAME=cpufreqd
VERSION=2.3.4
ARCH=${ARCH:-i486}
BUILD=2pg

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2"
fi

[ -d $TMP/$NAME-$VERSION ]&& rm -rf $TMP/$NAME-$VERSION
[ -d $PKG ]&& rm -rf $PKG
mkdir -p $TMP
mkdir -p $PKG


cd $TMP
tar xjvf $CWD/$NAME-$VERSION.tar.bz2

cd $NAME-$VERSION

chown -R root:root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
            --sysconfdir=/etc \
	    --mandir=/usr/man \
	    --infodir=\${prefix}/share/info \
            --localstatedir=/var/run \
            --libdir=/usr/lib/cpufreqd \
	    --enable-apm=disabled \
	    --enable-pmu=disabled \
	    --enable-tau=disabled \
	    --disable-sensors \
	    --enable-nforce2=disabled
make
make install DESTDIR=$PKG 
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
AUTHORS COPYING ChangeLog INSTALL NEWS README TODO \
 $PKG/usr/doc/$NAME-$VERSION

( cd $PKG
  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
)

(
gzip -9 $PKG/usr/man/*/*
)
mv $PKG/etc/cpufreqd.conf $PKG/etc/cpufreqd.conf.new
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n ../$NAME-$VERSION-$ARCH-$BUILD.txz
mv $TMP/$NAME-$VERSION-$ARCH-$BUILD.txz $CWD

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP/$NAME-$VERSION
 rm -rf $PKG
fi
