###########
#get device name and add it to the $etcmtools file as drive @.
#add a drive % that points to the temporary file `floppy'.

#mode= read or write, influences creation of % floppy.

export mdisk=@:

grep /dev/fd$mnr /etc/tbackup/Floppy | grep "=$mdens=" | \
 gawk '{ if(substr($1,9)=="xdens") $1=substr($1,1,8); print; }' >pux

#check if all is well.
if [ `cat pux |wc -l` != 1 ]; then
 echo "Error in Floppy file!"
 doabort
fi

export mdev=`gawk '{ print $1; }' pux`
export disksizek=`gawk '{ print $NF; }' pux`

if [ ! -e $mdev ]; then
 echo "Fatal error: floppy device $mdev does not exist."
 doabort
fi

export disksize=$[$disksizek * 1024]

#add entries to $etcmtools file.

rm -f $etcmtools~~
mv $etcmtools $etcmtools~~
grep -v "^[@%]" $etcmtools~~ >$etcmtools

printf "@ %s (added by tbackup)\n" "`cat pux`" >>$etcmtools

if [ $mode = write ]; then
 gawk '{ $1 = "/tmp/tbup/bulk/floppy"; print }' pux >bla
 printf "%% %s (added by tbackup)\n" "`cat bla`" >>$etcmtools
fi

rm -f pux bla

#get setfdprm command from database if extended density, else set it to a nop.
export setfdprm="echo -n"
if [ $mdens = xd -o $mdens = xh ]; then
 gawk -v fid=$disksizek '$1==fid { $1=""; print }' $bupbin/Floppyprm >pux
 setfdprm="setfdprm -p $mdev `cat pux`"
 rm -f pux
#executed on user abort, resets floppy to autodetect mode.
#not really necessary since changing floppies will also reset things.
 resetfloppy="setfdprm -c $mdev"
fi


#get parameters from Floppydb
gawk -v fid=$disksizek -v eccmethod=$eccmethod ' ($1==fid)&&($2==eccmethod) {
print "export doffset="$3;
print "export descrdown="$4;
print "export availk="$5;
printf "export eccargs=\"";
 for(i=6; i<=NF; i++) printf "%s ",$i;
 print "\"";
}' $bupbin/Floppydb >pux

if [ ! -s pux ]; then
 echo "Internal error in table Floppydb. (trestore version too old?)"
 doabort
fi

source /tmp/tbup/pux
rm pux

#calculate part size.

if [ $eccmethod = none ]; then
 export psizek=$availk
else
 export psizek=`ecor $eccargs -a$availk -D`
 export eccsizek=`ecor $eccargs -a$availk -E`
fi

export psize=$[$psizek * 1024]

