#!/bin/bash

##########################################################################
# Script : filelist.initrd.generate
# Purpose: Generate a file list of the Slackware initrd so that
#          we can compare it to the latest release in order to determine
#          whether we need to update anything for our port's initrd.
#          This is what I do for the SlackBuild & .build scripts
# Author : Stuart Winter <stuart@armedslack.org>
# Date...: 20-Aug-2005
##########################################################################

# Bring in the package building library:
source /usr/share/slackdev/buildkit.sh

# Let's assume that the initrd is already mounted on /mnt/floppy
# I'll probably rewrite this script at some point to be more automated
# and take filenames and stuff like that but for now it'll suffice 
# as the groundwork to ensure I don't forget to do it 
# as Katja would say to me "blah blah blah" ;)

# Eventually it will:
# Have a copy of the original color.gz (named color.gz.orig or whatever)
# Know the location of the newer version
# mount the original (probably located in same dir as this script)
#  - copy the contents to a temp directory
# umount the original initrd
# 
# mount the newer (either hard coded path or user specified, or default to hardcoded if none spec)
#  - copy contents to another temp directory
# umount the newer one
#
#  - generate a file listing diff between the two temp directories
#    - if a file displayable (a shell script or ascii file, use 'file' to determine type)
#      then 
#          - display the file info differences (size, date stamp etc)
#          - display the diff output between original and latest(new initrd)
#      otherwise:
#          - just display the file info differences (size, date stamp etc)
# - if there are no differences between the two then delete the temp directories
#   otherwise leave them in place for human perusal.
# 
# This script does none of these things yet .. but when I get some time it will.

CWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/initrd-worker  # where we'll extract stuff
export PKG=$TMP/initrd-changes      # where we'll place any files requiring manual inspection
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Location of the initrd in Slackware-current:
NEW_INITRD=$SLACKSOURCE/../isolinux/initrd.img

# Location of previous initrd (taken from Slackware-current)
ORIG_INITRD=$CWD/initrd.img.orig

# Ensure both images exist:
if [ ! -f $NEW_INITRD ]; then
   echo "Slackware-current initrd.img ($NEW_INITRD) is missing"
   exit 1
  else
   echo "Status of Slackware-current initrd:"
   file $NEW_INITRD
   stat $NEW_INITRD
fi
if [ ! -f $ORIG_INITRD ]; then
   echo "Original initrd.img ($ORIG_INITRD) is missing"
   exit 1
  else
   echo "Status of previous initrd:"
   file $ORIG_INITRD
   stat $ORIG_INITRD
fi

# Before doing anything complicated, let's do the most basic test of all:
if [ "$( md5sum $NEW_INITRD | awk '{print $1}' )" = "$( md5sum $ORIG_INITRD | awk '{print $1}' )" ]; then
   echo "MD5 sums are identical: no further tests required"  
   exit
fi

# The Slackware initrd images are compressed, so let's unpack them into 
# the temporary directory so we may mount them:
zcat $NEW_INITRD  > $TMPBUILD/new-initrd.img
zcat $ORIG_INITRD > $TMPBUILD/previous-initrd.img

# mount the two initrds:
( set -x
  cd $TMPBUILD/
  mkdir -pm755 mountpoints/{new,previous}
  cd mountpoints
  mount -oloop ../new-initrd.img new
  mount -oloop ../previous-initrd.img previous )

# Generate file list of the new initrd:
( cd $TMPBUILD/mountpoints/new
  find . -not -type d | xargs /bin/ls -ld --full-time | tr -s ' ' ) > $TMPBUILD/new-initrd.filelist
# dir list:
( cd $TMPBUILD/mountpoints/new
  find . -type d | xargs /bin/ls -ld --full-time | tr -s ' ' ) > $TMPBUILD/new-initrd.dirlist

# Generate file list of the previous initrd:
( cd $TMPBUILD/mountpoints/previous
  find . -not -type d | xargs /bin/ls -ld --full-time | tr -s ' ' ) > $TMPBUILD/previous-initrd.filelist
# dir list:
( cd $TMPBUILD/mountpoints/previous
  find . -type d | xargs /bin/ls -ld --full-time | tr -s ' ' ) > $TMPBUILD/previous-initrd.dirlist

# Generate a diff of the file lists:
diff -BbC0 $TMPBUILD/previous-initrd.filelist \
          $TMPBUILD/new-initrd.filelist \
          | egrep '^\- |^\+ ' > $TMPBUILD/filelist.diff

# Generate a diff of the dir lists:
diff -BbC0 $TMPBUILD/previous-initrd.dirlist \
           $TMPBUILD/new-initrd.dirlist \
           | egrep '^\- |^\+ ' > $TMPBUILD/dirlist.diff

# We split the file and dir lists up to make it easier for a human
# (me) to determine the changes:
echo
echo "********* Directory list changes *********"
cat $TMPBUILD/dirlist.diff
echo
echo "********* File list changes **************"
cat $TMPBUILD/filelist.diff

# umount the initrd files:
umount $TMPBUILD/mountpoints/{previous,new}
