#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild

PKGNAM=partimage
VERSION=${VERSION:-0.6.7}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2lu}
#TARGET=i486
EXT=tar.bz2
SOURCE=http://heanet.dl.sourceforge.net/sourceforge/${PKGNAM}/${PKGNAM}-${VERSION}.$EXT

CWD=`pwd`
if [ ! -e ${PKGNAM}-${VERSION}.$EXT ]; then
wget -c $SOURCE
fi

if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-${PKGNAM}
rm -rf $PKG
mkdir -p $PKG

/usr/sbin/useradd -g root -d "/sbin" -s "/bin/false" partimag 2> /dev/null

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
  elif ["$ARCH" = "athlon" ]; then
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
fi

if [ "$EXT" = "tar.gz" ]; then
   TARCMD="tar xvzf"
elif [ "$EXT" = "tgz" ]; then
   TARCMD="tar xvzf"
elif [ "$EXT" = "tar.bz2" ]; then
   TARCMD="tar xvjf"
elif ["$EXT" = "gz" ]; then
   TARCMD="gunzip"
elif ["$EXT" = "zip" ]; then
   TARCMD="gzip"
fi

echo "+======================+"
echo "| ${PKGNAM}-${VERSION} |"
echo "+======================+"

cd $TMP
rm -rf ${PKGNAM}-${VERSION}
$TARCMD $CWD/${PKGNAM}-$VERSION.$EXT || exit 1
cd ${PKGNAM}-$VERSION

# Make sure ownerships and permissions are sane:
#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 {} \;;

# Configure:
CFLAGS="$SLKCFLAGS" \

./configure \
--prefix=/usr \
--build=$ARCH-slackware-linux

# Build and install:
make || exit 1
make prefix=$PKG/usr install

# Strip binaries:
( 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
)

# Compress and link manpages, if any:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.*
      )
    done
  )
fi

# Compress info files, if any:
if [ -d $PKG/usr/info ]; then
  ( cd $PKG/usr/info
    rm -f dir
    gzip -9 *
  )
fi

# Add a documentation directory:
mkdir -p $PKG/usr/doc/${PKGNAM}-$VERSION
mv $PKG/usr/share/doc/partimage/* $PKG/usr/doc/${PKGNAM}-$VERSION
rm -rf $PKG/usr/share/doc/partimage/

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/${PKGNAM}-${VERSION}/slack-desc
cat $CWD/${PKGNAM}.SlackBuild > $PKG/usr/doc/${PKGNAM}-$VERSION/${PKGNAM}.SlackBuild
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/${PKGNAM}-${VERSION}-$ARCH-$BUILD.tgz