#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-sudo

VERSION=1.6.8p12
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

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" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

cd $TMP
rm -rf sudo-$VERSION
tar xzvf $CWD/sudo-$VERSION.tar.gz
cd sudo-$VERSION
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr --sbindir=/usr/sbin --with-getpass --with-C2
make
make install DESTDIR=$PKG
( cd $PKG/usr/bin
  chmod 4711 sudo
  ln -sf sudo sudoedit
)
chmod 755 $PKG/usr/sbin/visudo
rm -f $PKG/usr/man/man8/sudoedit.8
gzip -9 $PKG/usr/man/man?/*.?
( cd $PKG/usr/man/man8 ; ln -sf sudo.8.gz sudoedit.8.gz )
( 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
)
mv $PKG/etc/sudoers $PKG/etc/sudoers.new
mkdir -p $PKG/usr/doc/sudo-$VERSION
cp -a \
  BUGS HISTORY INSTALL LICENSE PORTING README README.LDAP RUNSON TODO TROUBLESHOOTING UPGRADE \
  $PKG/usr/doc/sudo-$VERSION
chmod 644 $PKG/usr/doc/sudo-$VERSION/*
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/sudo-$VERSION-$ARCH-$BUILD.tgz