#!/bin/sh

# Disk catalog script - set the following four variables.

# This is the /dev entry for your CD or DVD ROM.  There will probably be
# several symlinks going to the same device.  To get this, try 
#
# ls -al /dev | grep dvd
DVDDEV="/dev/cdrom1"


# This is where this device is mounted in your file tree.  This is usually
# /mnt/cdrom or /mnt/dvd but may be /media/cdrom or something like it,
# especially in Ubuntu which does things all indie rock and trendy like.
DVDMOUNTPOINT="/mnt/cdrom"


# This is the pathname of the catalog file.  The CATALOGINDEX simply
# contains a list of all volumes.  Not sure if that is useful to you or
# not but figured I'd add it.
CATALOGOUT="/home/quag7/catalog.txt"
CATALOGINDEX="/home/quag7/catalogindex.txt"


# Scratch directory - place for temporary files to go
SCRATCHDIR="/tmp"


######### Start Code ###########

# The touch command merely pokes a file, updating its last accessed time,
# or it creates a file of zero bytes if it does not exist.  We're doing
# this here just to avoid the file not found message on the first run of
# the script.  It can be safely left out.

touch $CATALOGOUT


# Grab volume name - Have to use the sed to remove trailing whitespace,
# since it is (irritatingly) part of the volume name.

VOLUMENAME=`volname $DVDDEV | sed 's/[ \t]*$//'`

# Search catalog to see if volume has already been added or if there is
# a volume name with the same description:

ALREADYADDED=`grep "$VOLUMENAME" $CATALOGOUT`
GRLENGTH=`echo $ALREADYADDED | wc -m`

if [ $GRLENGTH -gt 1 ]; then
echo "There is already a volume called: $VOLUMENAME"
echo "in the catalog."
echo ""
echo -n "Do you still want to add this? [y/n]: "
read CONFIRM
  if [ "$CONFIRM" == "n" ]; then
  echo ""
  exit 0;
  fi
fi

echo ""
echo "[Adding volume]: $VOLUMENAME"

# Backquotes are used to dump the results of a command into a variable.
# Backslash escapes the newline character so we can continue to the command
# on the next line.  This is done here so the line fits neatly in 80 columns,
# which is the best practice for scripts.

# First do the listing, and output to a scratch file.

cd $DVDMOUNTPOINT
ls -AlFGgR --group-directories-first --time-style=long-iso . > \
$SCRATCHDIR/diskcatalog-raw.txt

# Munge lines to make prettier.

# Remove . from any line that starts that way.
# sed is stream editor:
# sed operation/from/to/
# s = substitute
# ^ = if occurs at beginning of string
# \. = starting with period.  backslash is necessary because in sed, 
#      period stands for "single character."  We want period to be
#      interpreted literally as a dot.
# <blank field> = rename to null
#
# substitute/^if occurs at beginning of line, dot slash/<null>/
#
# sed is easier to write than read :)
#
# append a g on if you want all instances in a line replaced, not just the
# first, such as:
#
# sed 's/from/to/g'

cat $SCRATCHDIR/diskcatalog-raw.txt | sed 's/^\.//' > \
$SCRATCHDIR/diskcatalog-munged1.txt

# Remove first two fields in any line containing more than 2 fields, which
# should be true only of file and directory listings, leaving directory
# headings and bytecounts per directory intact.  To do this, we will
# loop through each line of the file, count the fields, then decide what
# to do.
#
# cut -delimiter " " (space), display -fields 4 to end of line.
#
# I removed file size from this because it fucks up the columns, but you
# can add it back in by changing -f4 to -f3 and maybe adding a -h switch
# to the ls command above, which is for "human readable" if you want.

cat $SCRATCHDIR/diskcatalog-munged1.txt | while read LINE; do
  TOTALFIELDS=`echo $LINE | wc -w`
  if [ "$TOTALFIELDS" -gt 2 ]; then
     OUTLINE=`echo $LINE | cut -d " " -f 4-`
     echo $OUTLINE >> $SCRATCHDIR/diskcatalog-munged2.txt
  else
     echo $LINE >> $SCRATCHDIR/diskcatalog-munged2.txt
  fi
done

# Grab some data and make a header.

RIGHTNOW=`date`
TOTALSIZE=`du -chs $DVDMOUNTPOINT | tail -n 1 | cut -d \t -f 1`

echo "----------------------------------------------------------------------\
---------" >> $CATALOGOUT

echo "VOLUME: $VOLUMENAME" >> $CATALOGOUT
echo "" >> $CATALOGOUT
echo "[Size]   : $TOTALSIZE" >> $CATALOGOUT
echo "[Added]  : $RIGHTNOW" >> $CATALOGOUT
echo "----------------------------------------------------------------------\
---------" >> $CATALOGOUT
echo "" >> $CATALOGOUT
cat $SCRATCHDIR/diskcatalog-munged2.txt >> $CATALOGOUT
echo "" >> $CATALOGOUT
echo "" >> $CATALOGOUT

# Clean up

rm -f $SCRATCHDIR/diskcatalog-raw.txt
rm -f $SCRATCHDIR/diskcatalog-munged1.txt
rm -f $SCRATCHDIR/diskcatalog-munged2.txt

# Generate Index of Volumes

grep "^VOLUME:" $CATALOGOUT > $CATALOGINDEX
TOTALVOLUMES=`cat $CATALOGINDEX | wc -l`
echo ""
echo "[Total Volumes Indexed so far]: $TOTALVOLUMES"
echo ""
exit 0
