! 3 Apr 89
!
! easy.cmd
!
! 3 Apr 89	Fixed colors for normalized color model in 6.1
!

emulate vt100			! so we can use scroll regions to make pretty

setvar logged ""		! no system is currently logged into
setvar fullmsg true		! give full ^E usage (become terminal) only once
setvar tout 4			! seconds to wait for "term -x" to say "OK"

setvar black 0			! set color vars to TERM standards
setvar blue 1
setvar green 2
setvar yellow 3
setvar red 4
setvar magenta 5
setvar cyan 14
setvar white 15


color default white,blue
color bold cyan,blue
cls

proc define
	cls
	wclose 3
	wclose 2
	wclose 1
	wopen 3,0,10,3,60,white,green
	? "              D E F I N E   A   S Y S T E M";
	wopen 2,17,10,7,60,white,green
	? "     \^E or F1 to save         ESCAPE to not save"
	? "     \^Y to clear a field      DELETE to delete left"
	? "     \^R to restore value      BACKSPACE to delete left"
	? "        Cursor keys select fields, Typing inserts"
	? "         SPACE for next option on rotate fields";
	wopen 1,3,10,14,60,white,green
	at clear
	at  0,2 "           System name: " get synam len 8
	at  1,2 "             System is: " get stype list "Direct","Dialup"
	at  2,2 "Phone number if Dialup: " get sphon len 20 
	at  3,2 "  Baud rate to call at: " get sbaud list "9600","4800","2400","1200","300"
	at  4,2 " Is the system variable baud? " get sauto list "YES","NO"
	at  5,2 "     Terminal it wants: " get sterm list "VT100","VT52","ANSI","TV925","TV912","WYSE50"
	at  6,2 "                Duplex: " get sdupl list "Full","Half"
* Nice thing is, we are running term -x and so MUST use termcrc, musn't we.
	setvar sprot "TERMCRC"
	at  8,2 "If you want auto-login,"
	at  9,2 "              Logon ID: " get sname len 8
	at 10,2 "              Password: " get spass len 8
	at 10,36 "(or blank if none)"
	at 11,2 "                Prompt: " get sprom len 8
	at 11,36 "(usually '\$' or '#')"
	at read
	setvar retv _retval
	wselect 3
	wclose 3
	wselect 2
	wclose 2
	wselect 1
	wclose 1
	cls
	if retv=1
		at  0,0 ""
		wait 1
		cls
		return
	endif
	setvar synm trim(synam)+".con"
	if exists(synm)
		? "The configuration '$(synm)' exists... overwrite?"
		abort off
		repeat
			setvar rsp toupper(chrin())
		until rsp='Y' | rsp='N'
		if rsp='N'
			?
			? "Saving to system.con instead..."
			? "Remember, rename it or lose it!"
			setvar synm "system.con"
		endif
	endif
	?
	?
	? "Writing..."
	fcreate 1,synm
	fwriteln 1,'remark This configuration created by easy.cmd'
	fwriteln 1,'baud $(sbaud)'
	fwriteln 1,'parity NONE'
	fwriteln 1,'wordlen 8'
	fwriteln 1,'stopbits 1'
	fwriteln 1,'mode $(sdupl)'
	fwriteln 1,'emulate $(sterm)'
	fwriteln 1,'xprot $(sprot)'
	fwriteln 1,'protocol ASCII'
	if sauto="NO"
	     fwrite 1,'login "\^L06\^T05\\r\^Slogin:\^S\^W'+trim(sname)+'\\r'
	else
	     fwrite 1,'login "\^T05\^L06\^B\\r\^Slogin:\^S'+trim(sname)+'\\r'
	endif
	if trim(spass)!=""
		fwrite 1,'\^Sword:\^S\^W'+trim(spass)+'\\r'
	endif
	setvar spmt sprom
	if trim(sprom)="\$"
		setvar spmt "\\\$"
	endif
	fwriteln 1,'\^T10\^S'+trim(spmt)+'\^S\\r"'
	fwriteln 1,'logout "\\rexit\\r"'
	if stype="Dialup"
		fwriteln 1,"call $(sphon)"
	else
		fwriteln 1,"call NONE"
	endif
	fclose 1
endproc define

proc login
	cls
	if logged !=""
	 	wopen 1,1,2,8,53,white,red
		?
		? "   You have not logged out from $(logged)..."
		? "   Do you want to try to log into another system"
		? "   anyway, instead of returning to the menu and"
		? "   logging out first? (Y/N) ";
		set abort off
		repeat
			setvar vlu toupper(chrin())
		until vlu="Y" | vlu="N"
		if vlu="Y"
			? "YES";
			wait 2
			set abort on
			wclose 1
		else
			? "NO";
			wait 2
			set abort on
			wclose 1
			return
		endif
	endif
	wclose 1
	menu 2 close	! etc

	menu 2 open "System ?",0,15,white,green

	dimstr systems 21
	setvar sysnum 0
	for i in (*.con) do
		if sysnum<21
			setvar systems[sysnum] left(i,len(i)-4)
			menu 2 entry systems[sysnum]
			setvar sysnum sysnum+1
		endif
	endfor
	if sysnum>0
		setvar men2 menu(2,0)
		menu 2 off
		if men2=0
			return
		endif
		at 0,0
		iferror connect $(systems[men2-1])
		 	wopen 1,2,5,7,41,white,red
			?
			? "   Unable to connect to $(systems[men2-1])"
			? "   Make sure the definition is right"
			? "   ";
			pause Hit any key to continue...
		else
		 	wopen 1,2,5,6,41,white,green
			setvar logged systems[men2-1]
			?
			? "   Connected to $(systems[men2-1])"
			? "   ";
			pause Hit any key to continue...
		endif
		wclose 1
	else
	 	wopen 1,2,5,6,36,white,red
		?
		? "    No systems defined."
		? "    ";
		pause Hit any key to continue...
		wclose 1
	endif
	cls
endproc login

proc terminal
	cls
	if fullmsg
		setvar fullmsg false
	 	wopen 1,2,5,8,46,white,green
		?
		? "   Now entering TERMINAL mode..."
		? "   Type control-E (control is like shift)"
		? "   to return to the menu."
		? "   ";
		pause Hit any key to continue...
		wclose 1
		cls
	endif
	if logged=""
		at 0,0 "[Connected without logging in, \^E to return to menu]"
		?
	else
		at 0,0 "[Connected to $(logged), \^E to return to menu]"
		?
	endif
	terminal
	cls
endproc terminal

proc send
	cls
	at  0,40 "     Send a file to another system:    "
	at  1,40 "select the file to be sent by entering "
	at  2,40 "the directory it is in on this system, "
	at  3,40 "and the filename.  Wildcards (*) are   "
	at  4,40 "allowed.  For instance, *.c will send  "
	at  5,40 "all files in the specified directory   "
	at  6,40 "which begin with anything and end in a "
	at  7,40 "'.c'.  If you use a wildcard, fill in  "
	at  8,40 "the 'to' directory (. means the current"
	at  9,40 "directory), but leave the 'to' filename"
	at 10,40 "blank.  Use text file conversion if you"
	at 11,40 "send (text files only!) DOS-TO-UNIX,   "
	at 12,40 "UNIX-TO-DOS, etc, but not for sending  "
	at 13,40 "DOS-TO-DOS, UNIX-TO-UNIX, etc.         "

setvar ok true
repeat
	if logged=""
	 	wopen 1,1,2,9,53,white,red
		?
		? "    You must either be previously logged in or"
		? "    you must have logged in from the menu if you"
		? "    want to transfer a file.  You did not log in"
		? "    from the menu.  Do you want to try to send a"
		? "    file anyway? (Y/N) ";
		set abort off
		repeat
			setvar vlu toupper(chrin())
		until vlu="Y" | vlu="N"
		if vlu="Y"
			? "YES";
			wait 2
			set abort on
			wclose 1
		else
			? "NO";
			wait 2
			set abort on
			wclose 1
			return
		endif
	endif
! position so attr works on televideo
	at 14,1 ""
	attr bold
	at 14,1 "F1 or \^E sends, ESC gets menu"
	attr default
	wopen 2,0,0,3,37,white,green
	? "       S E N D   A   F I L E";
 	wopen 1,3,0,11,37,white,green
	at 0,2 "Transfer from here"
	attr default
	at clear
	at 1,2 "Directory: " get ldir len 20
	at 2,2 " Filename: " get lfil len 20

	if logged=""
		at 4,2 "Transfer to unknown"
	else
		at 4,2 "Transfer to "+logged
	endif
	at 5,2 "Directory: " get rdir len 20
	at 6,2 " Filename: " get rfil len 20
	at 8,2 "Text file convert all files? " get cnv list "NO","YES"
	at read					! how do we know if ESC?
	setvar retv _retval
	wclose 1
	wselect 2
	wclose 2
! next line extra big and begins at 0 to zap tvi type attrs
	at 14,0 "                                    "
	if retv=1
		return
	endif

	? "\033[16;24r"		! install region
	? "\033[16;1H\033[J"	! erase from region start to end of screen

	? "Setting up for transfer...  ";
	setvar count 0
	repeat
		xmit "\r"
		wait 1
		xmit "term -x\r"
		wait "OK",tout
		if _timeout
			setvar count count+1
			if count > 2
				?
				? "   Unable to set up for transfer..."
				? "   make sure you are logged in and"
				? "   not running any other programs"
				? "   ";
				pause Hit any key to continue...
				? "\033[1;24r"	! remove region
				return
			endif
		endif
	until !_timeout
	? "Done"
	?
	? "Transferring files ";
	if cnv="YES"
		? "with";
		setvar cnv "[c]"
	elseif cnv="NO"
		? "without";
		setvar cnv ""
	endif
	? " text conversion."

	iferror xfer $(trim(ldir)+trim(lfil)) $(trim(rdir)+trim(rfil))$cnv
		?
		? "Error during transfer or transfer aborted"
		? "during transfer of file \"$(_filename)\""
		?
		setvar ok false
	endif
	pause Hit any key to continue...
	remote quit
	? "\033[1;24r"	! remove region
until ok
cls
endproc send

proc receive
	cls
	at  0,40 "     Get a file from another system:   "
	at  1,40 "select the file to receive by entering "
	at  2,40 "the directory it is in on the other    "
	at  3,40 "system, and the name.  Wildcards (*)   "
	at  4,40 "are allowed.  For instance, *.c will   "
	at  5,40 "get all files in the remote directory  "
	at  6,40 "which begin with anything and end in a "
	at  7,40 "'.c'.  If you use a wildcard, fill in  "
	at  8,40 "the 'to' directory (. means the current"
	at  9,40 "directory), but leave the 'to' filename"
	at 10,40 "blank.  Use text file conversion if you"
	at 11,40 "send (text files only!) DOS-TO-UNIX,   "
	at 12,40 "UNIX-TO-DOS, etc, but not for sending  "
	at 13,40 "DOS-TO-DOS, UNIX-TO-UNIX, etc.         "

setvar ok true
repeat
	if logged=""
	 	wopen 1,1,2,9,52,white,red
		?
		? "   You must either be previously logged in or"
		? "   you must have logged in from the menu if you"
		? "   want to get a file.  You did not log in from"
		? "   the menu.  Do you want to try to get a file"
		? "   anyway? (Y/N) ";
		set abort off
		repeat
			setvar vlu toupper(chrin())
		until vlu="Y" | vlu="N"
		if vlu="Y"
			? "YES";
			wait 2
			set abort on
			wclose 1
		else
			? "NO";
			wait 2
			set abort on
			wclose 1
			return
		endif
	endif
! position so attr works on televideo
	at 14,1 ""
	attr bold
	at 14,1 "F1 or \^E receives, ESC gets menu"
	attr default
	wopen 2,0,0,3,37,white,green
	? "        G E T   A   F I L E";
 	wopen 1,3,0,11,37,white,green
	if logged=""
		at 0,2 "Transfer from unknown"
	else
		at 0,2 "Transfer from "+logged
	endif
	attr default
	at clear
	at 1,2 "Directory: " get ldir len 20
	at 2,2 " Filename: " get lfil len 20

	at 4,2 "Transfer to here"
	at 5,2 "Directory: " get rdir len 20
	at 6,2 " Filename: " get rfil len 20
	at 8,2 "Text file convert all files? " get cnv list "NO","YES"
	at read					! how do we know if ESC?
	setvar retv _retval
	wclose 1
	wselect 2
	wclose 2
! next line extra big and begins at 0 to zap tvi type attrs
	at 14,0 "                                    "
	if retv=1
		return
	endif

	? "\033[16;24r"		! install region
	? "\033[16;1H\033[J"	! erase from region start to end of screen

	? "Setting up for transfer...  ";
	setvar count 0
	repeat
		xmit "\r"
		wait 1
		xmit "term -x\r"
		wait "OK",tout
		if _timeout
			setvar count count+1
			if count > 2
				?
				? "   Unable to set up for transfer..."
				? "   make sure you are logged in and"
				? "   not running any other programs"
				? "   ";
				pause Hit any key to continue...
				? "\033[1;24r"	! remove region
				return
			endif
		endif
	until !_timeout
	? "Done"
	?
	? "Transferring files ";
	if cnv="YES"
		? "with";
		setvar cnv "[c]"
	elseif cnv="NO"
		? "without";
		setvar cnv ""
	endif
	? " text conversion."

	iferror get $(trim(ldir)+trim(lfil)) $(trim(rdir)+trim(rfil))$cnv
		?
		? "Error during transfer or transfer aborted"
		? "during transfer of file \"$(_filename)\""
		?
		setvar ok false
	endif
	pause Hit any key to continue...
	remote quit
	? "\033[1;24r"	! remove region
until ok
cls
endproc receive

proc logout
 	wopen 1,2,5,7,45,white,green
	logout "^L06^T01\rexit\r^Sogin:^S"
	if logged=""
		?
		? "    Cannot logout unless first connected"
		? "    via login or prior call"
		? "    ";
		pause Hit any key to continue...
		wclose 1
		return
	endif
	iferror logout
		?
		? "    Unable to logout from $(logged)"
		? "    Make sure no program is running there"
		? "    ";
		pause Hit any key to continue...
	else
		wait 2
		hangup
		setvar logged ""
	endif
	wclose 1
endproc logout

menu 1 close	! etc

menu 1 open "",3,3,white,green

menu 1 entry "Define a system","Log in to a system","Become a terminal","Send a file","Get a file","Log out of a system","Quit"

cls
repeat
	! HELPINFO is here
	at  0,40 "                                       "
	at  1,40 "                                       "
	at  2,40 "                                       "
	at  3,40 "                                       "
	at  4,40 "                                       "
	at  5,40 "                                       "
	at  6,40 "                                       "
	at  7,40 "                                       "
	at  8,40 "                                       "
	at  9,40 "                                       "
	at 10,40 "                                       "
	at 11,40 "                                       "
	at 12,40 "                                       "
	at 13,40 "                                       "

	at 20,0
	at 21,0
	at 21,0 "Use cursor keys to move around"
	at 22,0
	at 22,0 "Use return key to choose an option"

	wclose 1
	wopen 1,0,3,3,23,white,green
	? "    O P T I O N S";
	wselect 0
	setvar men1 menu(1,0)
	switch
	case men1=1
		menu 1 off
		wclose 1
		do @define
	case men1=2
		menu 1 off
		wclose 1
		do @login
	case men1=3
		menu 1 off
		wclose 1
		do @terminal
	case men1=4
		menu 1 off
		wclose 1
		do @send
	case men1=5
		menu 1 off
		wclose 1
		do @receive
	case men1=6
		menu 1 off
		wclose 1
		do @logout
	case men1=7
		menu 1 off
		wclose 1

		if logged!=""
		 	wopen 1,1,2,8,52,white,red
			?
			? "   You are trying to quit without first using"
			? "   the 'logout' option from the menu to log out"
			? "   from $(logged).  Do you really want to quit"
			? "   before logging out? (Y/N) ";
			set abort off
			repeat
				setvar vlu toupper(chrin())
			until vlu="Y" | vlu="N"
			if vlu="Y"
				? "YES";
				wait 2
				set abort on
				wclose 1
			else
				? "NO";
				wait 2
				set abort on
				wclose 1
				setvar men1 0
			endif
		endif
	endswitch
until men1>6
menu 1 off
wclose 1

flush
cls
quit
