#!/bin/sh
: ; exec klone $0 "$@"
; The above line finds the klone executable in the $PATH

;; diskchange: wait for floppy to be changed
;; mdir ==> return failure (and nothing in stdout) if no disk

(setq args (getopts "C [options] [drive]
drive can be A or B"
  ("-m" 1 message "text to be printed when prompting for disk")
))

(if (match "[bB]" (get args 0 ""))
  (setq disk-name "b:")
  (setq disk-name "a:")
)

(setq mdir (list "mdir" disk-name))
(if (not message) (setq message ""))

(print-format () "\x07*** Go change floppy disk on %0! (%1) waiting...\n"
  *hostname* message)
(system (+ "rsh capa \"echo \\\"\x0d\x07 *** CHANGE DISK ON "
    *hostname*
    " (" message ")*** `date`\\\">/dev/ttya\""
))

;; first get disk contents

(wait (system mdir :output 'fd :error 'err))

(setq original-disk-contents (coerce fd String))
(setq disk-contents original-disk-contents)

;; now wait for this to change

(while (= disk-contents original-disk-contents)
  (wait (system '("sleep" "5")))		; sleep 5 seconds
  (wait (system mdir :output 'fd :error 'err))
  (setq disk-contents (coerce fd String))
  (if (= "" disk-contents)
    (setq disk-contents original-disk-contents)
  )
)
  
(system (+ "rsh capa \"echo \\\"\x0d DISK CHANGED ON "
    *hostname*
    " ********* `date`\\\">/dev/ttya\""
))
