#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-e2fsbn
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

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

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+================+"
echo "| e2fsprogs-0.5a |"
echo "+================+"
cd $TMP
tar xzvf $CWD/e2fsprogs-0.5b.tar.gz
cd e2fsprogs-0.5b
zcat $CWD/e2fsprogs-0.5b.diff.gz | patch
make world
strip lib/e2p/libe2p.so.1.0 lib/et/libet.so.1.0 lib/ext2fs/libe2fs.so.1.0 lib/ss/libss.so.1.0
cd e2fsck
strip e2fsck
cat e2fsck > $PKG/sbin/e2fsck
cat e2fsck.8 | gzip -9c > $PKG/usr/man/man8/e2fsck.8.gz
cd ../misc
strip badblocks chattr dumpe2fs lsattr mke2fs mklost+found tune2fs fsck
cat fsck > $PKG/sbin/fsck
cat badblocks > $PKG/sbin/badblocks
cat chattr > $PKG/sbin/chattr
cat dumpe2fs > $PKG/sbin/dumpe2fs
cat lsattr > $PKG/sbin/lsattr
cat mke2fs > $PKG/sbin/mke2fs
cat mklost+found > $PKG/sbin/mklost+found
cat tune2fs > $PKG/sbin/tune2fs
for page in badblocks.8 dumpe2fs.8 fsck.8 mke2fs.8 mklost+found.8 tune2fs.8 ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/$page.gz
done
for page in chattr.1 lsattr.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cd ../debugfs
strip debugfs
cat debugfs > $PKG/sbin/debugfs
cat debugfs.8 | gzip -9c > $PKG/usr/man/man8/debugfs.8.gz
cd ../lib/ss
cat libss.so.1.0 > $PKG/lib/libss.so.1.0
cd ../et
cat libet.so.1.0 > $PKG/lib/libet.so.1.0
cd ../ext2fs
cat libe2fs.so.1.0 > $PKG/lib/libe2fs.so.1.0
cd ../e2p
cat libe2p.so.1.0 > $PKG/lib/libe2p.so.1.0

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

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