;	(16-Apr-92)
;+
; NAME:
;   ACOPY
; PURPOSE:
;   Copy archive tapes and log the results.
; METHOD:
;   Calls mt, rd_tapdir, and arctap2tap to read and copy the tapes.
; RESTRICTIONS:
;   Master tape must be in drive 0
;   Copy   tape must be in drive 1
; MODIFICATION HISTORY:
;   Written, 31-mar-92, J. R. Lemen, LPARL
;   modified, 16-Apr-92, to split-out log-file writing section, gal, Japan
;   mod,	26-Sep-94, handle job aborts from arctap2tap., GAL
;
;-

version = 'ACOPY  V1.1 (16-Apr-92)'
print,' ****  ',version,'  ****',$
	format=string('('+strtrim(35-6-strlen(version)/2,2)+'x'+',3a)')

indrive = 0			; Assumptions
outdrive = 1			; Assumptions
;log_path = '/0p/linford/soft/tape'
log_path = '$DIR_GEN_TAPECOPY'	; Assumptions
if strupcase(get_host()) eq 'FLARES' then log_path = ''  ;*** How to resolve?

print,' ' 
print,'* Assume that Master (Input) tape is in drive',	$
		indrive,def_tapd(indrive),format='(a,i2," (",a,")")'
print,'* Assume that Copy  (Output) tape is in drive',	$
		outdrive,def_tapd(outdrive),format='(a,i2," (",a,")")'

print,'  ----------------------------------'
print,'  Reading the master tape header ...'
print,'  ----------------------------------'
mt, def_tapd(indrive), in_unit, /open, /mount	; get a unit number
mt, in_unit, /rewind				; rewind
mt, in_unit, skpf=1				; Skip over one file
rd_tapdir, in_unit, header, drecs		; read the tape header
if !version.os ne 'vms' then mt, in_unit,/close	; close the drive (if unix)

print,' ' & print,'  Master tape name = ',string(header.tapename)

; -----------------------------------
; Get the tape type (master or copy) 
; -----------------------------------

in_gen = 0				; Assumed generation level of input
ans = 'Y'	;;;print,' ' & defans = 'Y' & input,'* Is the input tape a Master?', ans, defans
if strupcase(strmid(ans,0,1)) eq 'N' then begin
  input,'* Enter the input generation number', in_gen, 1, 1, 99
endif 
out_gen = in_gen + 1
if in_gen eq 0 then in_tape = 'M  ' else $
	in_tape = strmid('C'+strtrim(in_gen,2)+' ',0,3)
out_tape = strmid('C'+strtrim(out_gen,2)+' ',0,3)

; -----------------------------------
; Get the tape Destination
; -----------------------------------

destin_str = ['UH','SU','UCB','KH','LPARL','NSSDC','NAOJ','BCS','Other','Nolog']

if n_elements(destin) eq 0 then destin = 0 else destin = destin+1
if destin gt n_elements(destin_str)-1 then destin=0

print,' ' & print,'  The possible destination codes are: '
for i=0,n_elements(destin_str)-1 do print,'[',strtrim(i,2),']  ',destin_str(i)

destin = 6	;;;print,' '&  input,'* Enter the number of the destination', destin, destin, 0, n_elements(destin_str)-1
User = '     ' & strput,user,destin_str(destin)		; Make it 5 chars long

; -----------------------------------
;  Begin the tape copy
; -----------------------------------

print,' ' & print,'  Ready to begin the tape copy now'
ans = 'Y'	;;;defans = 'Y' & input,'* Do you want to continue?', ans, defans
if strupcase(strmid(ans,0,1)) eq 'N' then stop
start_time = strmid(!stime,0,17)

print,'-------------------------------------',format='(5x,a)'
print,'|  '+string(header.tapename),strmid(!stime,0,11),'|',	$
		format='(5x,a,4x,a,t42,a)'
print,'|  '+out_tape,strtrim(user),'|',				$
		format='(5x,a,t34,a5,t42,a)'
print,'-------------------------------------',format='(5x,a)'
print,' Start Copy Time = ',start_time,format='(5x,2a)'

if !version.os ne 'vms' then begin
  arctap2tap, indrive, outdrive, /nofault, /log , jabort=jabort
endif else begin
  vmscopytape, indrive, outdrive
endelse

if (n_elements(jabort) eq 0) then jabort = 0;	/* if undefined set to no err */

stop_time = strmid(!stime,0,17)
copy_time = addtime(anytim2ex(stop_time),diff=anytim2ex(start_time))	; (min)
copy_time = string(copy_time,format='(f5.1)')


; -----------------------------------
;  Tell the operator we are finished
; -----------------------------------

print,'  Tape copy is completed'
print,'  Start time = ',start_time
print,'  Stop  time = ',stop_time
print,'  The job took ',strtrim(copy_time,2),' min'

host = string(' ',format='(a6)')
hostname = get_host() & hostname = str2arr(hostname,delim='.')
hostname = hostname(0)
strput,host,hostname			; Force host to be 6 characters long

;fmt = '(7(a," | "),a,t100," | ",a)'	;moved to wrttcplog.pro

print,'-------------------------------------',format='(5x,a)'
print,'|  '+string(header.tapename),strmid(!stime,0,11),'|',	$
		format='(5x,a,4x,a,t42,a)'
print,'|  '+out_tape,strtrim(user),'|',				$
		format='(5x,a,t34,a5,t42,a)'
print,'-------------------------------------',format='(5x,a)'

; --------------------------
;  Write the Log file
; --------------------------
if (jabort ne 1) then begin
  wrttcplog, header.tapename, start_time, copy_time, user, host, in_tape, $
	out_tape, indrive, outdrive, log_path
endif

;	--- Following code moved to "wrttcplog.pro" ----
;if strupcase(user) ne 'NOLOG' then begin
;  filnam = strtrim(host,2) + '.log'		; log file name
;  file_log = concat_dir(log_path, filnam)
;  files = findfile(file_log) & files = files(0)
;  if strlen(files) eq 0 then begin
;    openw,lun,file_log,/get_lun
;    printf,lun,'Tape Name      ','Start Copy       ','(Min)','User ',	$
;	'Host  ','In ','Out','Drive In      ','Drive Out',format=fmt
;  endif else begin
;    openu,lun,file_log,/get_lun,/append
;  endelse
;  printf,lun,string(header.tapename),start_time,copy_time,	$
;		user,host,in_tape,out_tape,			$
;		def_tapd(indrive),def_tapd(outdrive),format=fmt
; free_lun,lun
;
;  if strlen(files) eq 0 then begin		; Change protection if new file
;    if strupcase(get_host()) eq 'SAG' then begin
;;;        file_log = log_path + '/' +  strlowcase(filnam)	; rsh to unix system
;;;        cmd = 'rsh sxt chmod 666 '+file_log		; Make world writable
;	  print,' ** Make sure you log on to sxt and do a chmod 666 to ',$
;			file_log,' **'
;          bell & bell
;     endif else begin
;        cmd = 'chmod 666 '+file_log			; Make world writable
;       spawn,cmd
;    endelse
;  endif
;  print,'  Tape copy has been logged in file: ',file_log
;endif						; strupcase(user) ne 'NOLOG'
;	---- end removed section ----

bell				; Ring the bell to wake up the operator

end
