#!/bin/bash

# pkgtools.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 06-Jun-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# *** UPDATE THESE WITH EACH BUILD:
DIALOG=1.0-20060126

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     SLKCFLAGS="-O -march=armv3 -mtune=xscale" ;;
   powerpc) SLKCFLAGS="-O" ;;
   sparc)   SLKCFLAGS="-O" ;;
   *)       SLKCFLAGS="-O" ;;
esac

############ dialog ##############################################

# Extract source:
tar zxvvf $CWD/dialog-$DIALOG.tar.gz
cd dialog-*
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --enable-nls || failconfig

# Build:
make || failmake

# Start fakeroot server if required:
start_fakeroot

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

# Install binaries:
install -m755 dialog $PKG/bin

# Install man pages:
install -m644 dialog.1 $PKG/usr/man/man1

# Install etc data:
mkdir -p $PKG/etc
install -m644 samples/slackware.rc $PKG/etc/dialogrc

# Build locale data:
( cd po
  make || failmake
  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 )

# Install dialog docs:
mkdir -p $PKG/usr/doc/dialog-$DIALOG
cp -favv CHANGES COPYING README VERSION dialog.lsm \
         $PKG/usr/doc/dialog-$DIALOG

######## Slackware pkgtools #############################################

( cd $CWD/manpages
  install -m644 explodepkg.8 installpkg.8 makepkg.8 upgradepkg.8 pkgtool.8 \
              removepkg.8 \
              $PKG/usr/man/man8
  mkdir -p $PKG/usr/X11R6/man/man1
  install -m644 xwmconfig.1 $PKG/usr/X11R6/man/man1 )

# 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.*)

########################################################################

# Stuff specific to ARMedslack
if [ $PORTARCH = arm ]; then
   # This isn't required for ARM machines.  ARM boxes require 
   # arch-specific boot loaders.
   rm -f $PKG/sbin/makebootdisk
   # And this setup script is called by the installer but it's
   # useless for RISC OS based machines where we need to modify
   # the RISC OS !GRUB config file:
   rm -f $PKG/var/log/setup/*.make-bootdisk
   rm -f $PKG/var/log/setup/*.install-kernel
   ( cd $PKG 
     # Apply speedup fixes for installpkg:
     patch -p0 --verbose < $PORTCWD/sources/installpkg.speedup || failpatch 
     # A makepkg update:
     cd sbin
     patch -p0 < $PORTCWD/makepkg.diff || failpatch
   ) || failpatch
fi

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
