#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-ash
VERSION=0.4.0
ARCH=alpha
BUILD=3

rm -rf $PKG
mkdir -p $PKG
( cd $TMP
rm -rf ash-$VERSION
tar xzf $CWD/ash-$VERSION.tar.gz
cd ash-$VERSION
chown -R root.root .
( cd $CWD/patches
  for file in ash-builtin.patch.gz ash-echo.patch.gz ash-getcwd.patch.gz ash-getopt.patch.gz ash-glob.patch.gz ash-jobs.patch.gz ash-kill.patch.gz ash-makefile.patch.gz ash-manpage.patch.gz ash-hetio.patch.gz ash-memout.patch.gz ash-misc.patch.gz ash-redir.patch.gz ash-setmode.patch.gz ash-syntax.patch.gz ash-test.patch.gz ash-times.patch.gz ash-debian.patch.gz ash-ppid.patch.gz ash-freebsd.patch.gz ash-sighup.patch.gz ash-arithy.patch.gz ; do
    zcat $file
  done ) | patch -p1 || exit
zcat $CWD/ash-1.6-gcc4.diff.gz | patch -p0 || exit
make || exit
mkdir -p $PKG/{bin,usr/{man/man1,doc/ash-$VERSION}}
cat sh > $PKG/bin/ash
chmod 755 $PKG/bin/ash
chown -R root.bin $PKG/bin
cat sh.1 | gzip -9c > $PKG/usr/man/man1/ash.1.gz
chmod 644 TOUR
cp -a TOUR $PKG/usr/doc/ash-$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
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat << EOF > $PKG/install/doinst.sh
if grep "bin/ash" etc/shells 1> /dev/null 2> /dev/null; then
  true
else
  echo "/bin/ash" >> etc/shells
fi
EOF
cd $PKG
makepkg -l y -c n $TMP/ash-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/ash.build.log
