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

VERSION=5.52
ZIP=2.32
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}

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/_infozip.tar.gz

cd $TMP
rm -rf zip-$ZIP
tar xjvf $CWD/zip$(echo $ZIP | tr -d .).tar.bz2 || exit 1
cd zip-$ZIP
chown -R root:root .
mkdir -p $PKG/usr/doc/zip-$ZIP
cp -a BUGS CHANGES INSTALL MANUAL README TODO WHATSNEW WHERE \
      $PKG/usr/doc/zip-$ZIP
chmod 644 $PKG/usr/doc/zip-$ZIP/*
make -f unix/Makefile generic_gcc || exit 1
mkdir -p $PKG/usr/bin
cat zip > $PKG/usr/bin/zip
cat zipnote > $PKG/usr/bin/zipnote
cat zipsplit > $PKG/usr/bin/zipsplit
cat zipcloak > $PKG/usr/bin/zipcloak
mkdir -p $PKG/usr/man/man1
for page in man/zip.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/`basename $page`.gz
done

cd $TMP
rm -rf unzip-$VERSION
tar xjvf $CWD/unzip`echo $VERSION | tr -d .`.tar.bz2 || exit 1
cd unzip-$VERSION
chown -R root:root .
mkdir -p $PKG/usr/doc/unzip-$VERSION
cp -a BUGS COPYING* Contents History.$(echo $VERSION | tr -d .) INSTALL README ToDo WHERE \
      $PKG/usr/doc/unzip-$VERSION
chmod 644 $PKG/usr/doc/unzip-$VERSION/*
make -f unix/Makefile linux || exit 1
cat unzip > $PKG/usr/bin/unzip
cat unzipsfx > $PKG/usr/bin/unzipsfx
cat funzip > $PKG/usr/bin/funzip
cat unix/zipgrep > $PKG/usr/bin/zipgrep
( cd $PKG/usr/bin ; ln -sf unzip zipinfo )
chmod 755 $PKG/usr/bin/*
cd man
for page in funzip.1 unzip.1 unzipsfx.1 zipgrep.1 zipinfo.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cd ..
( 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/infozip-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/zip-$ZIP
  rm -rf $TMP/unzip-$VERSION
  rm -rf $PKG
fi