#!/bin/bash

# infozip.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 30-May-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

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

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

############ Build zip ###########################################################

# Extract source:
tar zxvvf $CWD/zip*.tar.gz
cd zip*
slackhousekeeping

# Grab Zip version from its extracted dir:
ZIPVER=$( ls -d ../zip* | cut -d/ -f2 )

# Apply RISC OS filetype patch:
bzcat $PORTCWD/sources/acorn-fstypes.patch.bz2 | patch -p1 --verbose || failpatch

# Build:
make CFLAGS="$SLKCFLAGS -g -Wall -I. -DUNIX" -f unix/Makefile generic || failmake

# Install binaries:
mkdir -p $PKG/usr/bin
install -m755 zipnote zipsplit zipcloak zip $PKG/usr/bin

# Install man pages:
mkdir -p $PKG/usr/man/man1
install -m644 man/*.1 $PKG/usr/man/man1

# Install docs:
mkdir -p $PKG/usr/doc/$ZIPVER
cp -a BUGS COPYING Contents History.`echo $VERSION | tr -d .` INSTALL README ToDo WHERE \
      $PKG/usr/doc/$ZIPVER

############ Build unzip ###########################################################

# Extract source:
cd $TMPBUILD
tar zxvvf $CWD/unzip*.tar.gz
cd unzip*
slackhousekeeping

# Grab unzip version number from extracted dir:
UNZIPVER=$( ls -d ../unzip* | cut -d/ -f2 )

# Build:
make CFLAGS="$SLKCFLAGS -I." -f unix/Makefile LF2="" unzips || failmake

# Install binaries:
install -m755 unzip unzipsfx funzip unix/zipgrep  $PKG/usr/bin
( cd $PKG/usr/bin && ln -sf unzip zipinfo )

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

# Install docs:
mkdir -p $PKG/usr/doc/$UNZIPVER
cp -a BUGS Contents History.550 INSTALL README ToDo WHERE \
      $PKG/usr/doc/$UNZIPVER

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

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# 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
