#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://www.kernel.org/pub/linux/utils/kernel/cpufreq/cpufrequtils.html

CWD=`pwd`
if ["$TMP" = ""]; then
 TMP=/tmp
fi
PKG=$TMP/package-cpufrequtils
NAME=cpufrequtils
VERSION=005
ARCH=${ARCH:-i686}
BUILD=1pg

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

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 {} \;

make
make install DESTDIR=$PKG 
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
AUTHORS COPYING README \
 $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/*/*
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
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.tgz
mv $TMP/$NAME-$VERSION-$ARCH-$BUILD.tgz $CWD

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