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

VERSION=6.12.00
ARCH=i386
BUILD=2

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

cd $TMP
tar xjvf $CWD/tcsh-$VERSION.tar.bz2
cd tcsh-$VERSION
chown -R root.root .
mkdir -p $PKG/usr/doc/tcsh-$VERSION
cp -a FAQ Fixes NewThings Ported README README.imake WishList Y2K \
  $PKG/usr/doc/tcsh-$VERSION
chmod 644 $PKG/usr/doc/tcsh-$VERSION/*
CFLAGS=-O2 ./configure --prefix=  i386-slackware-linux
make
mkdir -p $PKG/etc
mkdir -p $PKG/bin
cat tcsh > $PKG/bin/tcsh
chmod 755 $PKG/bin/tcsh
chown -R root.bin $PKG/bin
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
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
( cd $PKG
  find . | xargs file | grep "ELF 32-bit LSB executable" | grep 80386 | cut -f 1 -d : | xargs strip --strip-unneeded
)

# 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
