#!/bin/sh

# Slackware build script for csh

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20170621 bkw:
# - update to latest debian patch (-2.2, needed for -current).
# - quit compiling with -Werror (also needed for -current).
# - BUILD=2.

PRGNAM=csh
VERSION=${VERSION:-20110502}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

DEBVER=2.2

# Grr.
TARNAM="${PRGNAM}_${VERSION}.orig"
DIRNAM="${PRGNAM}-${VERSION}.orig"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $DIRNAM
tar xvf $CWD/$TARNAM.tar.gz
cd $DIRNAM
tar xvf $CWD/${PRGNAM}_${VERSION}-$DEBVER.debian.tar.[xg]z
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# Apply all of Debian's patches.
for diff in debian/patches/*.diff; do
  patch -p1 < $diff
done

# My own patch, keeps csh.h from defining its own (tiny) BUFSIZ. Might
# make I/O more efficient, and allows /etc/profile.d/coreutils-dircolors.sh
# to set a giant $LS_OPTIONS value without "Word too long" error.
patch -p1 < $CWD/bufsiz.diff

# use Slackware standard flags
sed -i "1iCFLAGS=$SLKCFLAGS" Makefile

# The LIBC= isn't even used, but Slackware64's pmake is broken: it has
# /usr/lib/libc.a hard-coded, and pmake wants to build that (and can't),
# even though the csh binary is dynamic and doesn't even need libc.a!
# Also don't know why I have to make const.h separately, but it works.
pmake const.h NOGCCERROR=1
pmake LIBC=/usr/lib$LIBDIRSUFFIX/libc.a NOGCCERROR=1
cd USD.doc
pmake paper.ps paper.txt
cd -

# I think this is the first time I've ever seen 'make install' gzip the
# man pages and strip the binary! BSD FTW!
mkdir -p $PKG/usr/bin $PKG/usr/man/man1
pmake install DESTDIR=$PKG BINDIR=/usr/bin MANDIR=/usr/man

# Technically this conflicts with Slackware's etc package, but the file
# that's modified still works exactly the same with tcsh. Also it's a .new
# config file, requires manual intervention.
mkdir -p $PKG/etc
cat $CWD/csh.login > $PKG/etc/csh.login.new

DOCDIR=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $DOCDIR
cp -a USD.doc/paper.* $DOCDIR
cat $CWD/$PRGNAM.SlackBuild > $DOCDIR/$PRGNAM.SlackBuild
cat $CWD/README_Slackware.txt > $DOCDIR/README_Slackware.txt

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}