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

# *** UPDATE THESE WITH EACH BUILD:
VERSION=11.0.0
DIALOG=1.0-20060126
ARCH=${ARCH:-i486}
BUILD=4

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

# Explode the package framework:
cd $PKG
explodepkg $CWD/_pkgtools.tar.gz

cd $TMP
rm -rf dialog-$DIALOG
tar xzvf $CWD/dialog-$DIALOG.tar.gz
cd dialog-$DIALOG
find . -perm 444 -exec chmod 644 {} \;
chown -R root:root .
## Disable broken arrows in textbox widget:
#zcat $CWD/dialog.textbox.noarrows.diff.gz | patch -p1 || exit
## Up arrow should have (-) next to it, not (+):
#zcat $CWD/dialog.arrows.diff.gz | patch -p1 || exit
CFLAGS="-O" \
  ./configure \
  --prefix=/usr \
  --enable-nls
make || exit 1
cat dialog > $PKG/bin/dialog
cat dialog.1 | gzip -9c > $PKG/usr/man/man1/dialog.1.gz
cat samples/slackware.rc > $PKG/etc/dialogrc
cd po
make
for file in *.gmo ; do
  mkdir -p $PKG/usr/share/locale/`basename $file .gmo`/LC_MESSAGES
  cat $file > $PKG/usr/share/locale/`basename $file .gmo`/LC_MESSAGES/dialog.mo
done
cd ..
# Install some docs:
mkdir -p $PKG/usr/doc/dialog-$DIALOG
cp -a \
  CHANGES COPYING README VERSION dialog.lsm \
  $PKG/usr/doc/dialog-$DIALOG

# Install Slackware script manpages:
( cd $CWD/manpages
  cat xwmconfig.1 | gzip -9c > $PKG/usr/man/man1/xwmconfig.1.gz
  for page in explodepkg.8 installpkg.8 makepkg.8 upgradepkg.8 pkgtool.8 \
    removepkg.8 ; do
    cat $page | gzip -9c > $PKG/usr/man/man8/$page.gz
  done
)

# Install Slackware scripts:
( cd $CWD/scripts
  cp -a xorgsetup $PKG/usr/bin
  cp -a xwmconfig $PKG/usr/bin
  chown root:root $PKG/usr/bin/*
  chmod 755 $PKG/usr/bin/*
  # OK, these two aren't really "scripts".  Sorry.  ;-)
  cp -a xorg.conf-fbdev $PKG/etc/X11
  cp -a xorg.conf-vesa $PKG/etc/X11
  chown root:root $PKG/etc/X11/*
  chmod 644 $PKG/etc/X11/*
  # Install the core Slackware package tools:
  for file in explodepkg installpkg makebootdisk makepkg pkgtool removepkg upgradepkg ; do
    cp -a $CWD/scripts/$file $PKG/sbin
  done
  chown root:root $PKG/sbin/*
  chmod 755 $PKG/sbin/*
  # These scripts are used during the installation:
  for file in setup.* ; do
    cp -a $file $PKG/var/log/setup
  done
  chown root:root $PKG/var/log/setup/setup.*
  chmod 755 $PKG/var/log/setup/setup.*
)

# Strip binaries:
( 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

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

echo
echo "HEY -- did you remember to update the version numbers in the setup scripts?"
echo

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/dialog-$DIALOG
  rm -rf $TMP/ipmask
  rm -rf $PKG
fi
