! 8 Feb 87
!
! Terry Lambert, Century Software
! file specs are now:			remcp [system:]file1 file2
!		  or:			remcp file1 [system:]file2
!
! remcp.cmd	- copy a file from this system to this system,
!		  copy a file from this system to remote unix system
!		  copy a file from remote unix system to this system

! LIMITATIONS:
! Assumes UNIX-format for login, running of TERM in -x mode
! remote to remote is not allowed, as we are not sure if it's the same
! without a comparison, and anyways, if it isn't to or from here,
! 1) they can do it themselves, and 2) how are we supposed to know about them
! in the first place?
! This program will only work if  the other system must be contacted first, as
! we may be in something important like a database that will take unkindly to
! having wierd characters spat at it.

! IMPORTANT: Text file conversion is not automatic (as it would exclude the
!	     transfer of binary images for download by a system of the type
!	     which uploaded the file), so [c] option must be specified after
!	     the destination file name, if a remote system is used as the
!	     source or target system... Otherwise, text file compatability
!	     may be lost.

! Name:		termx
! Parameters:	configuration for desired machine
! Purpose:	"invisible" connect, login, term -x on a remote system
proc termx
	iferror connect $1
		? "Unable to connect to: $1"
		return
	endif
	xmit "term -x\r"
	wait "OK",15

	if _timeout
		setv d cdate(time())+"."+ctime(time())
		? "failure of term -x on remote"
		repeat
			xmit "^c^c\177\177^d^d^d"
			wait "ogin:",5
		until !_timeout
		quit
	endif
endproc

! Name:		detach
! Parameters:	none
! Purpose:	stop whatever is going on at the remote system and get back
!		to the login prompt
proc detach
	repeat
		xmit "^c"
		wait "\$",5
	until !_timeout
	repeat
		xmit "^c^c\177\177^d^d^d"
		wait "ogin:",5
	until !_timeout
endproc

! must have both parameters
if "$1"="" or "$2"=""
	run copy $1 $2
	quit
endif

! find system names, if any
setv p1 pos(":","$1",1)
setv p2 pos(":","$2",1)
setv f1 right("$1",len("$1")-p1)
setv f2 right("$2",len("$2")-p2)
if p1=0
	setv s1 ""
else
	setv s1 left("$1",p1-1)
endif
if p2=0
	setv s2 ""
else
	setv s2 left("$2",p2-1)
endif

! diagnostics:
! ? "position #1: $(p1)"
! ? "position #2: $(p2)"
! ? "file #1 is   $(f1)"
! ? "file #2 is   $(f2)"
! ? "system #1 is $(s1)"
! ? "system #2 is $(s2)"

if p1=0
	if p2=0						! here to here
		run copy $1 $2
		quit
	else						! here to remote
		do @termx $s2
		iferror xfer $1 $(f2)
			? "File copy error: $1 $(f2)"
		endif
		do @detach
		quit
	endif
else
	if p2=0						! remote to here
		do @termx $s1
		iferror get $(f1) $2
			? "File copy error: $(f1) $2"
		endif
		do @detach
		quit
	else						! remote to remote
		? "remcp: copy must be to or from this system"
		quit
	endif
endif
quit
