#!/bin/sh
# This file is Copyright (C) 1995-1997 by Karsten Ballder (Ballueder@usa.net)
#
# It is part of the KBackup package, see the file COPYING for details.
# KBackup and all files included in this package are licensed and protected
# under the terms and conditions of the GNU General Public License Version 2.
#
#	If you want to contact the current maintainer of this software, please
#	send e-mail to: KBackup@usa.net

AUTODETECT_SRC_LOADED=YES

test_tape_device_II()
{
  tape="$1"

  InfoBox "Testing $tape:\nrewinding..."

  my_mt -f "$tape" rewind  2>/dev/null

  if [ $? != 0 ]
  then
    return 1
  fi
  mysleep 5
  
  my_mt -f "$tape" rewind 2>/dev/null
  if [ $? = 0 ]
  then
    mysleep 5
    InfoBox "Testing $tape:\nwriting 1st pattern..."
    ( echo "Tape device is working correctly (non-rewinding)." | \
     $DD of="$tape" obs=$syncto conv=sync ) 2>/dev/null
    if [ $? != 0 ]
      then
	return 1
    fi
    mysleep 5
    InfoBox "Testing $tape:\nwriting 2nd pattern..."
    ( echo "Tape device is incompatible with KBackup (rewinding)." | \
     $DD of="$tape" obs=$syncto conv=sync ) 2>/dev/null
    if [ $? != 0 ]
      then
	return 1
    fi
    mysleep 5
    InfoBox "Testing $tape:\nrewinding...          "
    my_mt -f "$tape" rewind 2>/dev/null
    mysleep 5
    my_mt -f "$tape" rewind 2>/dev/null
    mysleep 5
    InfoBox "Testing $tape:\nreading...            "
    cat "$tape" | $FGREP "non-rewinding" >/dev/null 2>/dev/null
    return $?
  else
    return 1	# error
  fi
}


#
#	scans system for KBackup compatible tape devices and returns
#	the list of device names in $TMP/$TMPFILE2
#
autodetect_tape_device_II()
{

  $DIALOG --title " WARNING ! " --yesno \
   "
  Please insert an empty tape into your drive.
  Furtheron, this might give unpredictable side
  effects, as your kernel might try to load
  additional kernel modules.

  USE THIS AT YOUR OWN RISK and only if you
    know what you are doing!

    This test will erase any data on the tape!
    Are you sure that you want to continue?" 16 50
    if [ $? != 0 ]
    then
      return
    fi

    echo >$TMP/$TMPFILE2
    InfoBox "Looking for devices..."
      
      devices=`ls /dev/[n]st* /dev/[n]tpqic* /dev/[r]mt* /dev/*tape /dev/n[r]ft* /dev/[r]ft* /dev/*mt* | sort | uniq`

      for i in $devices
      do
	InfoBox "Trying to reset driver..."
	if [ -r "$resetdevice" ] 
	then 
	  cat "$resetdevice" 2>/dev/null >/dev/null
	fi
	InfoBox "Looking for: $i"
	  $DD if="$i" of=/dev/null bs=0 count=0 2>$TMP/$TMPFILE >/dev/null
	    $FGREP "No such device" $TMP/$TMPFILE >/dev/null
	    if [ $? != 0 ] 
	    then
	      echo "$i" >>$TMP/$TMPFILE2
	    fi
	done

	devices=`cat $TMP/$TMPFILE2`
	count=1
	echo >$TMP/$TMPFILE2
	tape_detected=NO
	for i in $devices
	do
	  InfoBox "Trying to reset driver..."
	  if [ -r "$resetdevice" ] 
	  then 
	    cat "$resetdevice" 2>/dev/null >/dev/null
	  fi
	  test_tape_device_II "$i" 
	  
	  if [ $? = 0 ]
	  then
	    echo "$i ok" >>$TMP/$TMPFILE2		
	    tape_detected=YES
	  fi
	done
}

autodetect_tape_menu()
{
  if [ "$tape_detected" = "YES" ]
  then
    $DIALOG --title " Autodetection " --menu "
    These devices have been detected as being
    available and compatible for use with KBackup.

      Please choose one from the list.
      " 20 60 6 `cat $TMP/$TMPFILE2` 2>$TMP/$DIALOGFILE
      if [ $? = 0 ]
      then
	device=`cat $TMP/$DIALOGFILE`
	device_type=TAPE
	Configuration_changed=YES
      fi
  else
    MsgBox "
    No compatible tape device found.
    "
  fi
}

