' /*@ @{pftorf}
'
' LOGIN.PF3
'
' TASK SETTING

external xq(1)
external xl(1)
external xk(2)
external xs(1)
external xf(1)

external $$clm

' External ANGOSS Configuration Default Public Variables

external a_pw         (1)
external check_path   (1)
external chk_dir_case (1)
external display_message()
external error_push()
external error_pop()
external file_load		(4) 'Central load routine
external get_path     (1)
external open_log     ()
external password     (1)
external uni_yn(2)
external write_fsi    (1)
external write_ulog_flds()
external $$syspath $$usrid $$um $$persdir $$workdir $$smen $$lpath $$dirsep
external $$homedir $$infoset $$fct
external ##aphlp ##tchlp ##prhlp ##ah ##defin ##admin ##lang $$advr
external $$logum ##doslog $$opsys ##numum ##wrkhnd
external ##numlang $$lang ##activeimp ##comfg ##combg $$topp $$botp $_ttl
external ##audlg
external ##scrbg ##sdwbg ##statfg ##statbg ##ahfg ##ahbg ##mmod

global #trys #um_num

global get_environment (1)
global get_fs_info     (1)
global get_lang_info   (1)
global get_um_info     (1)
global pathset         ()
global trans_ro_a_d()

' ************************************************************
main
 local #cz $dtfmt

' BRANCH TO PATHSET IF $$CLM SET

	"@(#)login.pf3	3.15 - 95/02/07" '	SID
	if $$clm=="pathset"
		pathset()
		exit main
	end if
	smartpeek $_cz #cz
	czbreak off
	#trys=0

	error_push(0)
	clearerror
	file_load( "vw", get_path("user")|"user",0 ,1 )
	error_pop()
	if lerror<>0
		display_message( errortext(lerror)&"occurred during load of"&get_path("user")|"user.vw", "Press a key...",1)
		quit quit
	end if
	order change key [User ID]

	if records = 0
		display_message( "No users registered.","Adding START as a first user...",0)
		clearerror
		file_load( "vws", $$syspath|"user", 0, 1 )
		if lerror = 0
			data enter blank
			lock-record
			[User ID]="START"
			[User Name]="First User for start up"
			[Assigned User Mode]="DEV"
			[Autohelp]="Yes"
			[Application Help] = "Author"
			[Techinal Refernce] = "Author"
			[Procedures] = "Author"
			[Personal Directory] = $$dirsep
			[Definitions] = "Author"
			[Administrator] = "Yes"
			[Language Maintenance] = "Yes"
			write-record
			file unload view "user.vws"
			data goto view "user.vw"
		else
			display_message( "No records in user.db.  Could not load to add first record","Press a key...",1)
			quit quit
		end if
		display_message( "","",0)
	end if

	$$usrid=getenv("ANGIIUSR")
	if len($$usrid)>0
		##doslog=1
	end if

label ask_user_id

	if #trys>=3
		quit quit
	end if
	screen clear box 1 1 scrheight scrwidth ##comfg ##combg no-border
	if not(##doslog)
		$$usrid=Null
		screen print scrheight-3 1 ##comfg ##combg "Enter user ID:"
		screen input scrheight-3 2+len("Enter user ID:") ##comfg ##combg 8 $$usrid (if  records=1 and [user id]="START" then "START" else "")
		$$usrid=trim($$usrid)
	end if
	if $$usrid=="PATHSET"
		pathset()
		quit quit
	end if
	#trys=#trys+1

	if $$opsys == "DOS"                     'Uppercase User ID for DOS only
		$$usrid=upper($$usrid)
	end if

	data find [User ID] equal upper($$usrid) options "g"
	if ($$opsys == "UNIX" and [user id]=upper($$usrid)) or ($$opsys == "DOS" and [user id]==$$usrid)
		if password([password])
			$$persdir=[Personal Directory]

			if right($$persdir,1)<>$$dirsep and $$persdir<>Null and $$persdir<>blank
				$$persdir=$$persdir|$$dirsep
			end if

			$$lang=[Language]
			if len($$lang)<>0
				$$lpath=$$syspath|chk_dir_case("language"|$$dirsep|$$lang)|$$dirsep
			end if
			get_lang_info($$lang)
			get_environment($$lang)

			$$usrid=[User ID]
			$$smen=[Start Menu]
			if $$smen=Null or $$smen=blank or $$smen=0
				$$smen="mm"
			end if

			##aphlp = trans_ro_a_d([Application Help],0)
			##tchlp = trans_ro_a_d([Technical Reference],2)
			##prhlp = trans_ro_a_d([Procedures],0)
			##defin = trans_ro_a_d([Definitions],0)
			##ah    = uni_yn([Autohelp],1)
			##admin = uni_yn([Administrator],0)
			##lang  = uni_yn([Language Maintenance],0)
			$$infoset=[Information Set]
			$$um = chk_dir_case([Assigned User Mode])
			$$logum=$$um
			if get_um_info($$um)=0
				display_message ("Usermode"&chr(34)|$$um|chr(34)&"is not registered on this system.", "Press a key...",0)
				quit quit
			end if

			if ##mmod = 4 ' Calculate starting menu
				$$smen = "vaidx"|str(#um_num)
			end if

			if len($$infoset)=0
				$$infoset="standard"
			end if
			get_fs_info($$infoset)

		else
			quit quit
		end if
	else
		display_message( "Invalid User ID."&"Press a key...","",1)
		##doslog=false
		jump ask_user_id
	end if
	file unload view "user.vw"
	screen clear box 1 1 scrheight scrwidth ##comfg ##combg no-border

	if #cz
		czbreak on
	end if

	if NOT( open_log() )
		quit quit
	end if

	if ##audlg
		write_ulog_flds("","LOGIN","","",0)
	end if

end main

' ************************************************************
function get_lang_info($language)
' ************************************************************
local $numlang #ct #found $str $lang $ver $curver

	$curver="SWIIV001"

	clearerror
	fopen $$syspath|"language.lst" as ##wrkhnd options ro_share
	if lerror>0
		display_message( "Error opening language.lst."&"Press a key...","",0)
		clearerror
		return 0
	end if

	fseek ##wrkhnd 0
	fread ##wrkhnd into $ver
	if not($ver==$curver)
		fclose ##wrkhnd
		display_message( "language.lst"&"has an invalid version #."&"Please run conversion.","Press a key...",1)
		display_message( "Expecting:"&$curver|", Found:"&$ver|".","Press a key...",1)
		return 0
	end if

	fread ##wrkhnd into $numlang
	##numlang=val($numlang)
	fread ##wrkhnd into $str
	##activeimp=val($str)
	#found=0
	for #ct=1 to ##numlang
		fread ##wrkhnd into $str
		$lang=trim(left($str,8))
		if $lang==$language
			#found=1
			exit for
		end if
	end for
	fclose ##wrkhnd
	if #found
		return 1
	else
		return 0
	end if
end function

' ************************************************************
function get_environment($language)
' ************************************************************
local $path $savedvr $str

	if len($language)=0
		return
	end if

	$path=get_path("language")
	if len($path)=0
		display_message( "Unable to get path for language.vw.","Press a key...",1)
		return
	end if

	file_load( "vws", $path|"language",0 ,1 )

	if records>0
		data find [language code] equal $language options "gi"
	end if

	if not([language code]==$language)
		display_message( "Language"&$language&"not found in language.vw.","Press a key...",1)
		file unload view "language.vws"
		$$lpath=Null
		$$lang=Null
		data goto view "user.vw"
		return
	end if

	if len(trim([environment path]))>0
		$savedvr=$$advr
		$$advr=[environment path]
		if not(file($$advr|"anglib0h.rf3"))
			screen clear box scrheight-3 1 scrheight scrwidth ##comfg ##combg no-border
			screen print scrheight-3 1 ##comfg ##combg "Error:"&"Alternate ANGOSS Environment not found in"&$$advr
			screen print scrheight-2 1 ##comfg ##combg "Reverting to Default ANGOSS Environment."&"Press a key..."
			$str=inchar
			$$advr=$savedvr
		else
			remember unload "anglib0h.rf3"
			remember unload "anglib3h.rf3"
			load $$advr|"anglib0h" in-memory
		end if
	end if

	$$topp=[Top of Menu Prompt]
	$$botp=[Bottom Menu Prompt]
	$_ttl=[System Title]

	file unload view "language.vws"
	data goto view "user.vw"

	return

end function

' ************************************************************
function get_um_info($usermode)
' ************************************************************
local $num_um $um #ct $desc #found $str $ver $curver

	clearerror
	file_load( "vws", $$syspath|"usermode",0 ,1 )
	if lerror>0
		screen clear box scrheight-3 1 scrheight-2 scrwidth 15 0 no-border
		display_message( "Error loading usermode.vw."&"Login aborted.","Press a key...",1)
		clearerror
		return 0
	end if

	##numum=records-1
	data find [User Mode] equal $usermode options "gi"
	if [User Mode]==$usermode
		#found   = 1
		$desc    = [Description]
		$$smen   = [Start Menu]
		#um_num     = [User Mode #]
		if not(iserr([Screen Bg]))
			if [Screen Bg]<>blank and [Screen Bg]<>null
				##scrbg=[Screen Bg]
			end if
			if [Shadow Bg]<>blank and [Shadow Bg]<>null
				##sdwbg=[Shadow Bg]
			end if
			if [Status Line Fg]<>blank and [Status Line Fg]<>null
				##statfg=[Status Line Fg]
			end if
			if [Status Line Bg]<>blank and [Status Line Bg]<>null
				##statbg=[Status Line Bg]
			end if
			if [Autohelp Fg]<>blank and [Autohelp Fg]<>null
				##ahfg=[Autohelp Fg]
			end if
			if [Autohelp Bg]<>blank and [Autohelp Bg]<>null
				##ahbg=[Autohelp Bg]
			end if
			if [Default Menu]<>0 and [Default Menu]<>blank and [Default Menu]<>null
				##mmod=[Default Menu]
			end if
		end if
	else
		display_message( "Cannot find usermode"&$usermode|".","Press a key...",1)
		return 0
	end if

	file unload view "usermode.vws"
	if #found
		return 1
	else
		return 0
	end if

end function

' ************************************************************
function get_fs_info($fssys)
' ************************************************************

local #ct $vname #add #found

	if $fssys=="standard" and not(file($$syspath|"standard.fsi"))
		display_message("Creating default information set...","",14)
		file_load( "vws", get_path("loclist")|"loclist",0 ,1 )
		if records=0
			display_message("","",14)
			display_message( "System error."&"Empty loclist.db.", "Press a key...", 1)
			file unload view "loclist.vws"
			return 0
		end if
		file_load( "vws", get_path("infoset")|"infosloc",0 ,1 )
		file_load( "vws", get_path("infoset")|"infoset",0 ,1 )
		if records=0
			#add=1
		else
			data find [Set ID] equal "standard" options "g"
			if not([Set ID]="standard")
				#add=1
			else
				#add=0
			end if
		end if
		if #add
			data enter blank
			lock-record
			[Set ID]="standard"
			[Description]="Default Information Set"
			write-record
		end if
		data goto view "loclist.vws"
		let #ct=1
		while #ct<=records
			data goto record record-number #ct
			#ct=#ct+1
			$vname=[Variable Name]
			data goto view "infosloc.vws"
			if records>0
				#found=0
				data goto record record-number 1
label find
				data find [Variable Name] equal $vname options "i"
				if [Set ID]=="standard" and [Variable Name]==$vname
					#found=1
				else
					if record<>records
						data goto record next
						jump find
					end if
				end if
			else
				#found=0
			end if
			if #found=0
				data enter blank
			end if
			lock-record
			[Set ID]="standard"
			[Variable Name]=lower($vname)
			case lower($vname)
			when "$$sysfiles"
				[Path]=$$homedir|chk_dir_case("files")|$$dirsep
			when "$$sysfiles2"
				[Path]=$$homedir|chk_dir_case("files")|$$dirsep
			when "$$sysfiles3"
				[Path]=$$homedir|chk_dir_case("files")|$$dirsep
			when "$$sysfiles4"
				[Path]=$$homedir|chk_dir_case("files")|$$dirsep
			when "$$sysfiles5"
				[Path]=$$homedir|chk_dir_case("files")|$$dirsep
			when "$$syspath"
				[Path]=$$homedir|chk_dir_case("system")|$$dirsep
			when "$$quepath"
				[Path]=$$homedir|chk_dir_case("query")|$$dirsep
			when "$$senpath"
				[Path]=$$homedir|chk_dir_case("sendss")|$$dirsep
			when "$$reppath"
				[Path]=$$homedir|chk_dir_case("report")|$$dirsep
			when "$$sorpath"
				[Path]=$$homedir|chk_dir_case("sort")|$$dirsep
			when "$$merpath"
				[Path]=$$homedir|chk_dir_case("merge")|$$dirsep
			when "$$macpath"
				[Path]=$$homedir|chk_dir_case("macro")|$$dirsep
			when "$$archpath"
				[Path]=$$homedir|chk_dir_case("arch")|$$dirsep
			when "$$procpath"
				[Path]=$$homedir|chk_dir_case("proc")|$$dirsep
			when "$$tpltpath"
				[Path]=$$homedir|chk_dir_case("template")|$$dirsep

			otherwise
				[Path]=$$homedir|chk_dir_case("files")|$$dirsep
			end case
			write-record
			data goto view "loclist.vws"
		end while
		let $$fct="load"
		load $$advr|"fs_maint"
		write_fsi($fssys)
		unload "fs_maint"
		file unload view "loclist.vws"
		file unload view "infosloc.vws"
		file unload view "infoset.vws"
		display_message("","",14)
	end if

	if file($$syspath|$fssys|".fsi")
		execute $$syspath|$fssys|".fsi" in-memory
	else
		display_message( "Missing Infoset definition. ("|$$syspath|$fssys|".fsi)"&"Defaulting to standard.","",0)
		execute $$syspath|"standard.fsi" in-memory
		$$infoset="standard"
	end if

end function

' ************************************************************
function pathset()
' ************************************************************
local $loc
	display_message( "PATHSET utility invoked!  Press a key and then enter ADMIN password...","",1)
	if not(file($$syspath|"standard.fsi"))
		display_message( "Default infoset not created yet."&"Don't use PATHSET.","Press a key...",1)
		quit quit
	end if
	if a_pw("u")
		$loc=$$syspath
label check_lock
		if not(file($loc|"infoset.vw"))
			screen clear box 1 1 scrheight scrwidth ##comfg ##combg no-border
			screen shortinput $loc "Enter the path of infoset.vw :"
			$loc=check_path($loc)
		end if
		file_load( "vw", $loc|"infoset",0 ,1 )
		data find [set id] equal "standard" options "g"
		if [set id]=="standard"
			repaint on
			f1help on
			data update only-one
			f1help off
			repaint off
			let $$fct="load"
			load $$advr|"fs_maint"
			write_fsi("standard")
		else
			display_message( "System error."&"Could not find standard infoset in DB.","Press a key revert to defaults...",1)
			tools file erase $$syspath|"standard.fsi"
			get_fs_info("standard")
		end if
	end if

end function

function trans_ro_a_d($str,#nomatch)
	$str = lower($str)
	if $str = "read_only" or \
		$str = "nur_leson"
		return 0
	elseif $str = "author" or \
		$str = "autor"
		return 1
	elseif $str = "disable" or \
		$str = "deaktivieren"
		return 2
	else
		return #nomatch
	end if
end function

