#! /bin/sh
# lastdump: find out when a file was last dumped
# Usage: lastdump regexp
# The regular expression must be in grep syntax, not in shell syntax
root=u:
PATH=/bin:/usr/bin
program=`basename $0`

infofile=$root/etc/dumpinfo

if test $# -eq 0 ; then
  echo usage: $program file-regexp \(grep syntax, not shell\)
  exit 1
fi

if test ! -f $infofile ; then
  echo $program: Cannot find dump information file $infofile
  exit 1
fi

for regexp in $* ; do
  gawk '
    BEGIN {
      found = 0;
    }
    $0 ~ /^Level [0-9]+ dump of [^ ]+ started on [^ ]+ [^ ]+$/ {
      found_here = 0; level = $2; device = $5; startdate = $8; starttime = $9;
    }
    $0 ~ /^Level [0-9]+ dump of [^ ]+ ended on [^ ]+ [^ ]+ \(written on [^ ]+\)$/ {
      if (found_here)
        printf "Dump file: %s\n", substr ($12, 1, length ($12) - 1);
    }
    $8 ~ regexp {
      found = 1; found_here = 1;
      printf "%s: Dumped %s %s, level %d dump of %s\n", $8, startdate,
              starttime, level, device;
    }
    END {
      if (!found)
        printf "Nothing matching \"%s\" ever dumped\n", regexp;
    }
  ' regexp=$regexp $infofile
done
