#!/bin/sh
set -- `getopt l:e:x $*`
if [ $? != 0 ];then
    exit 1
fi
enabled_libs='""'
exclude_items='""'
while [ $1 != -- ]; do
    case $1 in
    -x)
        set -xv;;
    -l)
        enabled_libs=$2
        shift;;
    -e)
        exclude_items=$2
        shift;;
    -h)
        echo "usage: $0 [-h] [-e excluded_items] [-l enabled_libs] [cutoff date]"
    esac
    shift
done
shift

all_tapes="/tmp/all_tapes"
tapes_to_flip="/tmp/tapes_to_flip"
flipped_tapes="$HOME/write_tabs/flipped_tapes"
work_dir=$HOME/write_tabs
if [ ! -d $work_dir ]; then
    mkdir $work_dir > /dev/null
    if [ $? -ne 0 ]; then
        echo can not create working directory $work_dir
        exit 1
    fi
fi

. /usr/local/etc/setups.sh
setup enstore
if [ -z "${1-}" ]; then
        cutoff_date="2003-04-01"
else
        cutoff_date=$1
fi
if [ -z $enabled_libs_list ]; then
    enabled_libs="9940|cdf|mezsilo|D0-9940B|CDF-9940B|CD-9940B|dlt"
fi
if [ -z $exclude_items_list ]; then
    excluded_items="migrated"
fi

# get list of all non writable volumes
python $ENSTORE_DIR/src/readonly_volumes.py | egrep $enabled_libs | egrep -v $excluded_items >$all_tapes
# get list of volumes after the cutoff date
python $ENSTORE_DIR/src/readonly_volumes.py $cutoff_date | egrep $enabled_libs | egrep -v $excluded_items >$tapes_to_flip
diff $all_tapes $tapes_to_flip | awk '{if (NF==9) print $2,$3,$4,$5,$6,$7,$8,$9}'>$flipped_tapes
