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

VERSION=${VERSION:-1.6.0}
INSTWAT=${INSTWAT:-0.7.0beta4}
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

cd $TMP
rm -rf checkinstall-$VERSION
tar xjvf $CWD/checkinstall-$VERSION.tar.bz2
cd checkinstall-$VERSION
find . -name "*.rej" -exec rm -f {} \;
find . -name "*.orig" -exec rm -f {} \;

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 640 -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 {} \;

# This patch causes checkinstall to use the native Slackware package
# building tool, though, and allows us to meet the "good enough"
# standard of excellence.  ;-)
zcat $CWD/checkinstall.followspecsbetter.diff.gz | patch -p1 --verbose --backup --suffix=.orig

# If there's a checkinstallrc, move it:
if [ -r /etc/checkinstallrc ]; then
  mv /etc/checkinstallrc /etc/checkinstallrc.bak
fi

make || exit 1
make install

mkdir -p $PKG/usr/sbin
cat /usr/sbin/checkinstall > $PKG/usr/sbin/checkinstall
chmod 755 $PKG/usr/sbin/checkinstall
# Note that we DO NOT install "makepak".  Don't use that -- it's broken.

# Our patch also places the config file in /etc since Linux standards say config
# files must not go under /usr:
mkdir -p $PKG/etc/checkinstall
cat /etc/checkinstall/checkinstallrc > $PKG/etc/checkinstall/checkinstallrc.new

# Install NLS files:
mkdir -p $PKG/usr/share/locale/es/LC_MESSAGES
cp /usr/share/locale/es/LC_MESSAGES/checkinstall.mo $PKG/usr/share/locale/es/LC_MESSAGES/checkinstall.mo

mkdir -p $PKG/usr/doc/checkinstall-$VERSION
cp -a \
  BUGS COPYING CREDITS Changelog FAQ INSTALL NLS_SUPPORT README RELNOTES TODO \
  $PKG/usr/doc/checkinstall-$VERSION

# Now install the installwatch components:
cd installwatch-$INSTWAT
  mkdir -p $PKG/usr/bin
  cat /usr/bin/installwatch > $PKG/usr/bin/installwatch
  chmod 755 $PKG/usr/bin/installwatch
  mkdir -p $PKG/usr/lib
  cat installwatch.so > $PKG/usr/lib/installwatch.so
  chmod 755 $PKG/usr/lib/installwatch.so
  mkdir -p $PKG/usr/doc/checkinstall-$VERSION/installwatch-$INSTWAT
  cp -a \
    BUGS CHANGELOG COPYING INSTALL README TODO VERSION \
    $PKG/usr/doc/checkinstall-$VERSION/installwatch-$INSTWAT
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

# Add install script for checkinstallrc:
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/checkinstall/checkinstallrc.new
EOF

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/checkinstall-$VERSION-$ARCH-$BUILD.tgz

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

