#!/bin/bash
# read part $1 from floppy.

pnr=`i2split -i -ppart $1`
pnr=`expr $pnr + 1`
shortname=`basename $1`
it="part $pnr"
dosname=`printf "%s.%03d\n" $shortname $[$pnr % 1000]`


echo "--Insert the disk with $it in drive $mnr"
enter -f
if [ $readmethod = "floppypart" ]; then
 printf "  and press enter. (Type o for options, o and e! to end the partial read)\007 "
else
 printf "  and press enter. (Type o for options.)\007 "
fi
while true; do
 read
 if [ "$REPLY" = "" ]; then err=no; break; fi
 if [ "$REPLY" = "o" ]; then err=yes; break; fi
 printf "-->  "
done

beep=no

while true; do
#attempt to read part loop

#this if handles user interaction if an error occures.
if [ $err != no ]; then

 echo
 echo " Enter one of the following commands:"
 echo "                           (You may want to insert a new disk first.)"
 echo " r  : retry reading $it from drive $mnr"
if [ $eccmethod = none ]; then
 echo " s  : salvage readable data of $it from drive $mnr"
else
 echo " s  : salvage readable data of $it from drive $mnr and try to correct errors"
fi
 echo " q! : quit trying to read this part, continue with the next part"
 echo " e! : indicate end of archive"
# echo " e  : escape to shell, exit with exit. (DEBUG CODE)"
 if [ $beep = yes ]; then printf "\007"; sleep 1;printf "\007"; fi
 beep=yes

 enter -f
 REPLY=""
 while [ "$REPLY" = "" ]; do printf "-->"; read; done;
 
   if [ "$REPLY" = r ]; then
    echo;
 elif [ "$REPLY" = "s" ]; then
    rm -f /tmp/tbup/bulk/$1
    floppysalv /tmp/tbup/bulk/$1
    if [ -e /tmp/tbup/bulk/$1 ]; then 
       break
    fi
    echo "Salvage aborted."
    continue; 
 elif [ "$REPLY" = "q!" ]; then
    echo "Without $it the archive will be only be partially restored."
    echo "Watch the restore log for archiver errors."
    if question "Really give up reading $it"; then 
       echo "Attempt to read $it to disk aborted."
       echo "Feeding dummy data to the archiver......"
       yes "This is dummy data..." | head -$psize\c >/tmp/tbup/bulk/$1 2>/dev/null
       echo
       break
    fi
    beep=no
    continue;
 elif [ "$REPLY" = "e!" ]; then
    if question "Indicate end of archive"; then 
#bypass internal error check in floppyre
       cat /dev/null >/tmp/tbup/bulk/$1
#indicate end of archive
       echo "end" >/tmp/tbup/bulk/part}       
       echo End of archive indicated.
       echo "Watch the restore log for possible archiver errors."
       break
    fi
    beep=no
    continue; 
# elif [ "$REPLY" = "e" ]; then 
#      bash
#      continue; 
 else echo "Invalid command, try again."; beep=no; continue;
 fi

fi
#end of handle user interaction on error.

beep=yes
err=yes

echo "Reading $it from drive $mnr.........."


#empty kernel message log
printf "" >kernmeslog

#set the floppy parameters for xdens formats.
eval $setfdprm
#flush to let them take effect
fdflush $mdev

if ! head -c9 $mdev >dhead; then 
#1st sector is not formatted in the right way
  echo
  echo "**ERROR**: no disk inserted, disk is not formatted, or disk is not"
  echo "           formatted with the right density."
  continue
fi

#disk is formatted.

rm diskdesc 2>/dev/null
mcopy $mdisk\descr2k diskdesc &>/dev/null
if [ ! -e diskdesc ]; then 
  echo
  echo "**ERROR**: This is not a floppy backup disk."
  echo "Contents of the disk:  (only up to 7 files are listed)"
  mdir $mdisk |head -10
  continue
fi

if ! cmp -s descr2k diskdesc; then 
#descr2k on disk is not equal to current desc.
  echo
  echo "**ERROR**: This disk is not part of the current backup set."
  echo "Backup description on this disk (first 5 lines)"
  head -5 diskdesc | cat -n
  continue
fi

#check if right part

mdir $mdisk >diskdir

grep -i "^$1[. ]" diskdir >partdir

if [ ! -s partdir ]; then
#not the right part
  echo
  echo "**ERROR**: This disk does not contain $it."
  echo "Contents of the disk:  (only up to 7 files are listed)"
  mdir $mdisk |head -10
  continue
fi


if ! mcopy -n $mdisk$dosname /tmp/tbup/bulk/$1; then
  echo
  echo "**ERROR**: read failed."
  continue
fi

if [ ! -e /tmp/tbup/bulk/$1 ]; then
#for compatibility with version 0.2
 echo Seems to be a V0.2 archive...
 if ! mcopy -n $mdisk$1 /tmp/tbup/bulk/$1; then
  echo
  echo "**ERROR**: read failed."
  continue
 fi
fi

if [ ! -e /tmp/tbup/bulk/$1 ]; then
  echo
  echo "**ERROR**: This disk does not contain $it."
  echo "Contents of the disk:  (only up to 7 files are listed)"
  mdir $mdisk |head -10
  continue
fi

#last disk?

grep -i "^part} " diskdir >partdir

if [ -s partdir ]; then
 rm -f /tmp/tbup/bulk/part}
 if ! mcopy $mdisk\part} /tmp/tbup/bulk/part} ; then
  echo
  echo "**ERROR**: mcopy of the \`last disk indicator' file \`part}' failed."
  continue
 fi
fi

if [ ! -e /tmp/tbup/bulk/$1 ]; then
  echo
  echo "**ERROR**: Internal error?  $it should have been read by now..."
  continue
fi

echo "Finished reading $it.  [you can remove the floppy]"
echo

break

done

