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

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

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

echo "+===============+"
echo "| AfterStep-1.0 |"
echo "+===============+"
#
# we use the afterstep binary from 1.1beta2, but everything else from 1.0
#
cd $TMP
tar xvzf $CWD/AfterStepClassic-1.1beta2.tar.gz
cd AfterStepClassic-1.1beta2
zcat $CWD/configure.h.diff.gz | patch -p0
./MakeMakefiles
make all
strip afterstep/afterstep
cat afterstep/afterstep > $PKG/usr/X11R6/bin/afterstep
#
# now build the old one to get the rest of everything
#
cd $TMP
tar xzvf $CWD/AfterStep-1.0.tar.gz
cd AfterStep-1.0
# build program
zcat $CWD/configure.h.diff.gz | patch -p0
./MakeMakefiles
make all
# remove debugging symbols
for modname in Animate Audio Auto Banner Pager Wharf asclock
do
   strip modules/$modname/$modname
done
# write binaries to package
cat modules/asclock/asclock > $PKG/usr/X11R6/bin/asclock
for modname in Animate Audio Auto Banner Pager Wharf
do
   cat modules/$modname/$modname > $PKG/usr/X11R6/lib/X11/afterstep/$modname
done
# copy icon and background files to necessary locations
cp icons/*.xpm $PKG/usr/X11R6/include/X11/pixmaps
cp backgrounds/* $PKG/usr/X11R6/lib/X11/afterstep/backgrounds
# do the man pages
cat afterstep/afterstep.man | gzip -9c > $PKG/usr/X11R6/man/man1/afterstep.1.gz
for manname in Animate Audio Auto Banner Pager Wharf asclock
do
   cat modules/$manname/$manname.man | gzip -9c > \
      $PKG/usr/X11R6/man/man1/$manname.1.gz
done
# write out default configuration file
cat sample.steprc > $PKG/usr/X11R6/lib/X11/afterstep/system.steprc
# copy docs to the doc directory
cp CHANGES $PKG/usr/doc/AfterStep-1.0
cp CREDITS $PKG/usr/doc/AfterStep-1.0
cp FAQ $PKG/usr/doc/AfterStep-1.0
cp INSTALL $PKG/usr/doc/AfterStep-1.0
cp README $PKG/usr/doc/AfterStep-1.0
cp README.8bit $PKG/usr/doc/AfterStep-1.0
# add the xinitrc file
mkdir -p $PKG/var/X11R6/lib/xinit
chown -R root.root $PKG/var
chmod -R 755 $PKG/var
cat $CWD/xinitrc.afterstep > $PKG/var/X11R6/lib/xinit/xinitrc.afterstep
chown root.root $PKG/var/X11R6/lib/xinit/xinitrc.afterstep
chmod 755 $PKG/var/X11R6/lib/xinit/xinitrc.afterstep

# Build the package:
cd $PKG
tar czvf $TMP/afterstep.tgz .

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/AfterStepClassic-1.1beta2
  rm -rf $TMP/AfterStep-1.0
  rm -rf $PKG
fi
