#!/bin/bash

#
# Simple script to copy a dump file to DVD media
#  forrmat: copyDumpToDVD <list of dump filenames>
#
exit_cleanup() {
    exit $1
}

copyToDVD() {
    # mount the DVD media first
    DVD_MOUNTPOINT=/media/cdrom
    rc=0
    
    # Mount the DVD and check for write-protect.  This might not work
    # for the DVD-RAM media, but this check won't hurt anything.
    # Note, the mount command fails if no DVD cartridge in drive or
    # if the medium is write protected.
#    mount -w $DVD_MOUNTPOINT > /dev/null
    # Specifically use UDF, do not rely on filesystem "guessing" or /etc/fstab content.
    mount -t udf -w /dev/cdrom /mnt/cdrom > /dev/null
    if [ $? -eq 0 ] ; then
        # The mount command was successful and the cartridge is not
        # write protected.

        # Go to target directory
        cdir=`pwd`
        cd $DVD_MOUNTPOINT

        # Copy the data to DVD - assume it's been formatted
        for i in $*
        do
            cp -f /dump/$i $DVD_MOUNTPOINT/$i
            if [ $? -eq 0 ] ; then
                # The file was copied successfully.
                cd $cdir
                umount $DVD_MOUNTPOINT
                if [ $? -eq 0 ] ; then
                    # The DVD cartridge was successfully unmounted.
                    rc=0
                else
                    # The DVD cartridge was not successfully unmounted.
                    rc=7
                fi
            else
                # The file was not copied successfully.
                cd $cdir
                umount $DVD_MOUNTPOINT
                if [ $? -eq 0 ] ; then
                    # The DVD cartridge was successfully unmounted.
                    rc=3
                else
                    # The DVD cartridge was not successfully unmounted.
                    rc=4
                fi
            fi
        done
    else
        # The mount command failed.
        grep $DVD_MOUNTPOINT /etc/mtab
        if [ $? -eq 0 ] ; then
            # The DVD is already mounted.
            rc=5
        else
            # The DVD is not already mounted.
            rc=6
        fi
    fi

    return $rc
}

#--------------------Main Program--------------------------

#
# if no filename specified, list choices, then exit
#
if [ "$*" = "" ]; then
    echo "No input filename specified. Files available to copy:"
    echo
    a=`ls /dump/SYSDUMP* 2>/dev/null | wc | awk '{print $1}'`
    b=`ls /dump/FSPDUMP* 2>/dev/null | wc | awk '{print $1}'`
    c=`ls /dump/SMADUMP* 2>/dev/null | wc | awk '{print $1}'`
    d=`ls /dump/PWRDUMP* 2>/dev/null | wc | awk '{print $1}'`
    if [ $a -ne 0 ]; then
        cdir=`pwd`
        cd /dump
        ls SYSDUMP* 
        cd $cdir
    fi
    if [ $b -ne 0 ]; then
        cdir=`pwd`
        cd /dump
        ls FSPDUMP* 
        cd $cdir
    fi
    if [ $c -ne 0 ]; then
        cdir=`pwd`
        cd /dump
        ls SMADUMP* 
        cd $cdir
    fi
    if [ $d -ne 0 ]; then
        cdir=`pwd`
        cd /dump
        ls PWRDUMP* 
        cd $cdir
    fi
    exit_cleanup 1
else
    # Ok. arguements were passed in, hopefully all are exitsing dump file(s).
    # Look for them and begin the copy process...
    for i in $*
    do
        if [ ! -f /dump/$i ]; then
            #input arguement file not found
            echo "File not found: $1"
            exit_cleanup 2
        fi
    done

    # if here, all arguements must be valid dump files
    copyToDVD $*
    shellExit=$?
    
    exit_cleanup $shellExit
fi
