#!/bin/bash
#
# iso-read-test.sh
#
# takes zero, one, or two optional parameters in any order
#
# ONE optional parameter:
# an integer number for approximate size of .iso file in megabytes,
# default is 1, for 1 megabyte,
# to test approximately 5 gigabytes pass 5000
#
# OTHER optional parameter:
# the iso-read command you want to test,
# default is iso-read,
# has to be named iso-read to be recognized,
# could be for example src/iso-read

# in $1 or $2 accept integer number as total .iso size in megabytes
if [[ $1 =~ ^[0-9]+$ ]]
then
    size="$1"
elif [[ $2 =~ ^[0-9]+$ ]]
then
    size="$2"
else
    size="1" # default
fi

# in $1 or $2 accept a different location for iso-read,
# e.g. ~/libcdio/src/iso-read
if [[ $1 =~ iso-read ]]
then
    isoread="$1"
elif [[ $2 =~ iso-read ]]
then
    isoread="$2"
else
    isoread="iso-read" # default
fi
# make absolute before changing directory
if [ `which "$isoread"` ]
then
    isoreadpath=`which "$isoread"`
else
    isoreadpath="$isoread"
fi
isoreaddir=`dirname "$isoreadpath"`
isoreaddir=`cd "$isoreaddir" && pwd`
isoread="$isoreaddir"/`basename "$isoreadpath"`

echo "Going to use $isoread ."
"$isoread" --version

# this says to make test files in directory where this script is,
# and just so you know, those files will be megabytes large,
# but they will be reused on subsequent runs to be much faster.
# like a hundred times faster or more
datadir=`dirname "$0"`
datadir=`cd "$datadir" && pwd`
cd "$datadir"

# pick for your system,
# on a Mac mkisofs, otherwise people use newer genisoimage
isomakingcommand=""
which mkisofs && isomakingcommand="mkisofs"
which genisoimage && isomakingcommand="genisoimage"
if [ -z "$isomakingcommand" ]
then
    echo "NEITHER mkisofs NOR genisoimage FOUND !"
    exit 1
fi

echo "Working with a $size megabyte large .iso file in $datadir ."

isoname="exampleIso"$size"M.iso"

prepdir="prep"

if [ ! -f "$isoname" ]
then

if [ ! -f "$isoname".tar.gz ]
then

    echo "Going to use " `which "$isomakingcommand"` " to prepare $isoname in $isoname.$prepdir ."

    mkdir "$isoname.$prepdir"
    mkdir "$isoname.$prepdir"/mixed

    # a large file with many of the same characters that will compress well
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\060' > "$isoname.$prepdir/mixed/file0l.txt"
    # a small file we will want to iso-read
    echo "content of short text file #0 we are looking for" > "$isoname.$prepdir/mixed/file0s.txt"
    # a few times over now
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\061' > "$isoname.$prepdir/mixed/file1l.txt"
    echo "content of short text file #1 we are looking for" > "$isoname.$prepdir/mixed/fileOneSmall.txt"
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\062' > "$isoname.$prepdir/mixed/file2l.txt"
    echo "content of short text file #2 we are looking for" > "$isoname.$prepdir/mixed/file2small.txt"
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\063' > "$isoname.$prepdir/mixed/file3l.txt"
    echo "content of short text file #3 we are looking for" > "$isoname.$prepdir/mixed/file3ThreeSmall.txt"
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\064' > "$isoname.$prepdir/mixed/file4l.txt"
    echo "content of short text file #4 we are looking for" > "$isoname.$prepdir/mixed/fileFourS.txt"
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\065' > "$isoname.$prepdir/mixed/file5l.txt"
    echo "content of short text file #5 we are looking for" > "$isoname.$prepdir/mixed/file5small.txt"
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\066' > "$isoname.$prepdir/mixed/file6l.txt"
    echo "content of short text file #6 we are looking for" > "$isoname.$prepdir/mixed/file6s.txt"
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\067' > "$isoname.$prepdir/mixed/file7l.txt"
    echo "content of short text file #7 we are looking for" > "$isoname.$prepdir/mixed/fileSevenSmall.txt"
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\068' > "$isoname.$prepdir/mixed/file8l.txt"
    echo "content of short text file #8 we are looking for" > "$isoname.$prepdir/mixed/fileEightWithARatherUnusuallyLongFilenameButWithSmallishContent.txt"
    dd if=/dev/zero bs=1024 count=$size"00" | tr '\0' '\069' > "$isoname.$prepdir/mixed/file9l.txt"
    echo "content of short text file #9 we are looking for" > "$isoname.$prepdir/mixed/file9s.txt"

    # a potentially huge .iso file, whatever you wrote above
    "$isomakingcommand" -J -r -T -f -V "EXAMPLEISO"$size"M" -o "$isoname" "$isoname.$prepdir"

    # if you want to archive a test file, this compressed one is much smaller,
    # seen 4KB instead of 1MB, seen 5MB instead of 5GB
    tar -cvzf "$isoname.tar.gz" "$isoname"

else
    echo "Extracting $isoname from existing $isoname.tar.gz ."
    tar -xvzf "$isoname.tar.gz"
fi

else
    echo "Using existing $isoname ."
fi

# remind of disk space use
ls -al
du -h .

echo "
####################################################
#
# Size $size megabyte .iso file read back:
#
"
for i in {0..9}
do
    echo "didn't read file #$i" > $size"Mfile"$i"s-read.txt"
done
#
"$isoread" --version
"$isoread" -i "$isoname" -e "mixed/file0s.txt" -o $size"Mfile0s-read.txt"
"$isoread" -i "$isoname" -e "mixed/fileOneSmall.txt" -o $size"Mfile1s-read.txt"
"$isoread" -i "$isoname" -e "mixed/file2small.txt" -o $size"Mfile2s-read.txt"
"$isoread" -i "$isoname" -e "mixed/file3ThreeSmall.txt" -o $size"Mfile3s-read.txt"
"$isoread" -i "$isoname" -e "mixed/fileFourS.txt" -o $size"Mfile4s-read.txt"
"$isoread" -i "$isoname" -e "mixed/file5small.txt" -o $size"Mfile5s-read.txt"
"$isoread" -i "$isoname" -e "mixed/file6s.txt" -o $size"Mfile6s-read.txt"
"$isoread" -i "$isoname" -e "mixed/fileSevenSmall.txt" -o $size"Mfile7s-read.txt"
"$isoread" -i "$isoname" -e "mixed/fileEightWithARatherUnusuallyLongFilenameButWithSmallishContent.txt" -o $size"Mfile8s-read.txt"
"$isoread" -i "$isoname" -e "mixed/file9s.txt" -o $size"Mfile9s-read.txt"
#
anybad=0
for i in {0..9}
do
    readback=`cat $size"Mfile"$i"s-read.txt"`
    if [ "$readback" = "content of short text file #$i we are looking for" ]
    then
        echo "file #$i read back OK: $readback"
    else
        anybad=1
        echo "file #$i read DEFECT : $readback"
    fi
done
if [ $anybad -eq 0 ]
then
    echo "Overall success of $0 :)"
else
    echo "Overall FAILURE of $0 :("
fi
exit $anybad

# if it worked correctly you should see the lines we wrote into the short files:
#
# content of short text file #i we are looking for
#
# IF however defect https://savannah.gnu.org/bugs/?39373 is still
# in effect you might see lines such as:
# 111111111111111111111111111111111111
#
# furthermore, IF however defect https://savannah.gnu.org/bugs/?39354 is still
# in effect you might see lines such as:
#
# iso-read: Could not get ISO-9660 file information out of $isoname for file mixed/file2small.txt.
# iso-read: iso-info may be able to show the contents of $isoname.

