#!/bin/sh
# Set initial variables:
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-e2fsprogs
VERSION=${VERSION:-1.41.11}
ARCH=${ARCH:-alpha}
BUILD=${BUILD:-1}
TARGET=$ARCH-alphaslack-linux

rm -rf $PKG
mkdir -p $PKG
( cd $TMP
rm -rf e2fsprogs-$VERSION
tar xf $CWD/e2fsprogs-$VERSION.tar.gz
cd e2fsprogs-$VERSION

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

# Disable --uuidd builds the same functionality into libuuid.*.  It's better than way.
# Disable libblkid and libuuid, as we'll be using the ones in u-l-ng
./configure \
 --prefix= \
 --libdir=/lib \
 --bindir=/usr/bin \
 --includedir=/usr/include \
 --mandir=/usr/man \
 --infodir=/usr/info \
 --datadir=/usr/share \
 --docdir=/usr/doc/e2fsprogs-$VERSION \
 --enable-elf-shlibs \
 --enable-jbd-debug \
 --enable-tls \
 --disable-uuidd \
 --disable-libblkid \
 --disable-libuuid \
 --build=$TARGET

make -j2 || exit 1
make install DESTDIR=$PKG
make install-libs DESTDIR=$PKG
# Fix up package:
rm -rf $PKG/etc/init.d
mkdir -p $PKG/usr/lib
mv $PKG/lib/pkgconfig $PKG/usr/lib

( 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
)

# Don't clobber an existing config file
#mv $PKG/etc/mke2fs.conf $PKG/etc/mke2fs.conf.new

mkdir -p $PKG/usr
( cd $PKG/usr
  #rm -f man/man1/compile_et.1 man/man3/uuid_generate_random.3 man/man3/uuid_generate_time.3 man/man8/fsck.ext2.8 man/man8/fsck.ext3.8 man/man8/mkfs.ext2.8 man/man8/mkfs.ext3.8
  rm -rf man/man3/uuid_generate_random.3 man/man3/uuid_generate_time.3 man/man8/fsck.ext2.8 man/man8/fsck.ext3.8 man/man8/mkfs.ext2.8 man/man8/mkfs.ext3.8 man/man8/mkfs.ext4.8 man/man8/mkfs.ext4dev.8
  ( cd man/man3
    ln -sf uuid_generate.3.gz uuid_generate_random.3.gz
    ln -sf uuid_generate.3.gz uuid_generate_time.3.gz 
  )
  ( cd man/man8
    ln -sf e2fsck.8.gz fsck.ext2.8.gz
    ln -sf e2fsck.8.gz fsck.ext3.8.gz
    ln -sf e2fsck.8.gz fsck.ext4.8.gz
    ln -sf e2fsck.8.gz fsck.ext4dev.8.gz
    ln -sf mke2fs.8.gz mkfs.ext2.8.gz
    ln -sf mke2fs.8.gz mkfs.ext3.8.gz
    ln -sf mke2fs.8.gz mkfs.ext4.8.gz
    ln -sf mke2fs.8.gz mkfs.ext4dev.8.gz
  )
  cd ..
  mv bin include info man usr 2> /dev/null
  mkdir -p usr/lib
  mv lib/*.so lib/*.a usr/lib
)
# Junk removal
rm -rf $PKG/usr/man/man8/fsck.ext2.8 $PKG/usr/man/man8/fsck.ext3.8 $PKG/usr/man/man8/fsck.ext4.8 $PKG/usr/man/man8/fsck.ext4dev.8 $PKG/usr/man/man8/mkfs.ext2.8 $PKG/usr/man/man8/mkfs.ext3.8 $PKG/usr/man/man8/mkfs.ext4.8 $PKG/usr/man/man8/mkfs.ext4dev.8
# Compress man pages:
gzip -9 $PKG/usr/man/man?/*
## Anyone care for these?  I don't. [NOTE:  XFS utils link with libuuid.a]
##rm -f $PKG/usr/lib/*.a
( cd $PKG/sbin
  rm -rf mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev fsck.ext2 fsck.ext3 fsck.ext4 e2label findfs
  ln -sf mke2fs mkfs.ext2
  ln -sf mke2fs mkfs.ext3
  ln -sf mke2fs mkfs.ext4
  ln -sf mke2fs mkfs.ext4dev
  ln -sf tune2fs e2label
  ln -sf tune2fs findfs
  cat << EOF > fsck.ext2
#!/bin/sh
exec /sbin/e2fsck -C 0 \$*
EOF
  chmod 755 fsck.ext2
  cp -a fsck.ext2 fsck.ext3
  cp -a fsck.ext2 fsck.ext4
  cp -a fsck.ext2 fsck.ext4dev
)
mkdir -p $PKG/usr/doc/e2fsprogs-$VERSION
cp -a COPYING INSTALL INSTALL.elfbin README RELEASE-NOTES SHLIBS $PKG/usr/doc/e2fsprogs-$VERSION
chmod 644 $PKG/usr/doc/e2fsprogs-$VERSION/*
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# I guess Ted would rather not have this included, so we won't.
# ( cd misc 
#   make findsuper
#   cat findsuper > $PKG/sbin/findsuper
#   chmod 755 $PKG/sbin/findsuper )

mv $PKG/etc/mke2fs.conf $PKG/etc/mke2fs.conf.new

cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/mke2fs.conf.new
if [ -x /usr/bin/install-info ]; then
  install-info --info-dir=/usr/info /usr/info/libext2fs.info.gz 2> /dev/null
fi
EOF

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/e2fsprogs-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/e2fsprogs.build.log
