#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
PKG=$TMP/package-tcsh

VERSION=6.15.00
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

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

rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf tcsh-$VERSION
tar xjvf $CWD/tcsh-$VERSION.tar.bz2 || exit 1
cd tcsh-$VERSION

# The LS_COLORS variable shared by tcsh has new options in recent
# versions of coreutils that cause tcsh to exit, so disable the
# built-in color ls:
zcat $CWD/tcsh.nobuiltincolorls.diff.gz | patch -p1 --verbose || exit 1

chown -R root:root .
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 {} \;
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=  \
  $ARCH-slackware-linux
make -j4 || exit 1
mkdir -p $PKG/etc
mkdir -p $PKG/bin
cat tcsh > $PKG/bin/tcsh
chmod 755 $PKG/bin/tcsh
mkdir -p $PKG/usr/man/man1
cat tcsh.man | gzip -9c > $PKG/usr/man/man1/tcsh.1.gz
echo '.so man1/tcsh.1' | gzip -9c > $PKG/usr/man/man1/csh.1.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
)
mkdir -p $PKG/usr/doc/tcsh-$VERSION
cp -a \
  BUGS FAQ Fixes NewThings Ported README README.imake WishList Y2K \
  $PKG/usr/doc/tcsh-$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/tcsh-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/tcsh-${VERSION}
  rm -rf $PKG
fi