#!/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-1.10 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/e2fsprogs-1.10.tar.gz
cd e2fsprogs-1.10
configure --enable-elf-shlibs --enable-dynamic-e2fsck
make CFLAGS=-O2 LDFLAG_STATIC=-s
cd e2fsck
strip e2fsck flushb
cat e2fsck > $PKG/sbin/e2fsck
cat flushb > $PKG/sbin/flushb
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 chattr > $PKG/usr/bin/chattr
cat lsattr > $PKG/usr/bin/lsattr
cat fsck > $PKG/sbin/fsck
cat badblocks > $PKG/sbin/badblocks
cat dumpe2fs > $PKG/sbin/dumpe2fs
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
cat libss.so.2.0 > $PKG/lib/libss.so.2.0
cat libext2fs.so.2.3 > $PKG/lib/libext2fs.so.2.3
cat libe2p.so.2.3 > $PKG/lib/libe2p.so.2.3
cat libcom_err.so.2.0 > $PKG/lib/libcom_err.so.2.0
cat libuuid.so.1.1 > $PKG/lib/libuuid.so.1.1

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

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