;---------------------------------------------
;	Esimerkki - Valinta 1
;---------------------------------------------
; 01-SEP-89 / Soft Service / Leonard Norrgård
;
; Copyright © 1989 Soft Service
;---------------------------------------------

	init
	keyboard nosend
	call main
	keyboard send
takaisin:
	change Esimerkki.vtex

header:
	echo "<cs>"		; Tyhjentää kuvaruudun
	echo "<cya><nbg><crlf>"
	echo "<yel><dbl> Soft Videotex <crlf><crlf>"
	echo "<cya><nbg><crlf>"
	echo "<crlf><cya>Automaattinen sivunhaku - TeleSampo<crlf>"
	return

main:	call header

	echo "<CYA>Tämä esimerkkiskripti hakee<CRLF>"
	echo "<CYA>kuusi sivua TeleSammosta, käyttäen<CRLF>"
	echo "<CYA>TeleSammon demo-tunnus (0#).<CRLF><CRLF>"

	echo "<GRE>Anna modeemin siirtonopeus,<crlf>"
	echo "<GRE>joko<yel>300, 1200<gre>tai<yel>2400<gre>:<con><yel>"

	call readword

	if "<ret>" = "300" then goto baud-ok
	if "<ret>" = "1200" then goto baud-ok
	if "<ret>" = "2400" then goto baud-ok

	echo "<crlf><crlf><red>Virheellinen nopeus '<ret>'<crlf>"
	echo "<crlf><gre>Paina näppäintä,<yel>L<gre>Lopettaa: <con>"
	key flush
	if "<inkey>" ~= "l" then return
	goto main

baud-ok:
	echo "<crlf>"
	wait 00:00.01
	set baud <ret>

	when nocartrap "NO CARRIER" do goto nocar
	call tee-se Hae-joka

	echo "<GRE>Esimerkki suoritettu,<crlf>"
	echo "<GRE>paina näppäintä: <con>"
	key flush
	wait 00:00.01
	change Esimerkki.vtex

;
nocar:  echo "<cs><red>Modeemi menetti yhteyden<crlf><crlf>"
	echo "<gre>Paina näppäintä.<crlf>"
	key flush
	keyboard send
	change Esimerkki.vtex

;
; Hae halutut sivut
;
Hae-joka:
	echo "<cof>"

	; Tänne voi halutessa lisätä sivuja.
	; Ilman tunnusta ei kuitenkin pääse pitkälle
	; TeleSammossa...
	call hae-sivu "T01a"
	call hae-sivu "T02a"
	call hae-sivu "T021a"
	call hae-sivu "HOTLINE1a"
	call hae-sivu "8a"
	call hae-sivu "T09a"
	return

;
; Hae ja tallenna sivu
;
hae-sivu:
	set virhe-jatko hae-sivu2   ; minne mennä virhekorjauksen jälkeen
;	when serial error do goto linja-virhe
	when eiooo "tee uusi valinta" do goto hae-skip
        send "<sivu-komento1><1><sivu-komento2>"

hae-sivu2:
	wait 00:00.10
;	save "kuvat:<1>"

hae-skip:
	cancel serial
	cancel eiooo
	return
;
linja-virhe:
	echo "<clear><red><double>Linja-virhe:<yellow>sivu haetaan uudestaan<crlf>"
	wait 00:00.01
	send "<sivu-uudelleen>"		; Hae sivu uudestaan
	goto <virhe-jatko>

;
; Aseta parametrit TeleSampoa varten
;
par-telesampo:

        pass 0#		; TeleSammon "Demo-Tunnus"

        set puhelin-numero 92922

	set dial-command "atdt <puhelin-numero>^M"

	set sivu-komento1 "*"
	set sivu-komento2 "#"
	set sivu-uudelleen "**"

	; Siirtonopeus annettu ylhäällä
	set parity even
	set databits 7
	set stopbits 1
	set handshake none

	return

;
; Pure linja
;
pure-linja:
	call CheckModem	; modeemi komentotilaan
	wait 00:00.01
	send "ath0^M"   ; puretaan linja
	wait 00:00.01
	return
       
;
; Ottaa yhteyden Videotex-tietokantaan
;
soita:  call checkmodem
        echo  "<GRE>Modeemi on kunnossa<CRLF>"
	when temp1 "BUSY" do goto ts-busy
	when temp2 "NO CARRIER" do goto ts-nc
	when temp3 "NO DIALTONE" do goto ts-nodial
	when temp3a "CONNECT" do goto ts-online

	echo "<GRE>Soitto meneillä...<crlf>"
	send <dial-command>
	hold

ts-busy:
	call kysy "<RED>Numero on varattu,"
	goto <ret>

ts-nc:  call kysy "<RED>Ei vastausta,"
	goto <ret>

ts-nodial:
	call kysy "<RED>Modeemi ei saanut valintaääntä,"
	goto <ret>

ts-online:
	cancel temp1 temp2 temp3 temp3a		; Poista when-ansiot
	return

kysy:	wait 00:00.01
	echo "<crlf><1><crlf>"
	echo "<RED>yritetäänkö uudestaan (K/E)?<con> "
	key flush
	wait 00:00.01
	echo "<crlf>"
	if "<inkey>" ~= "k" then return "main"
	else return "takaisin"

;
; Testaa modeemi -- varmista että parametrit ovat asetettuja ensin!
;
CheckModem:
	online <baud> <parity> <databits> <stopbits> <handshake>
	;
	; Modeemi komentotilaan: Odota, lähetä "+++" ja odota taas.
	;
	echo "<GRE>Modeemitesti:<CRLF>"
	wait 00:00.02
	send +++
	wait 00:00.02
	echo "<crlf>"
        when tmp1 after 00:00.02 do goto nomodem
        sewa "^M^Mat^M" "OK"       ; tarkista että modeemi vastaa
        cancel tmp1                ; poista ansa tmp1
        return                     ; modeemi on kunnossa, jatkaa!

NoModem:
	echo "<crlf>"
        echo "<RED>Modeemi-ongelma: modeemi ei vastaa.<CRLF>"
	echo "<RED>Tarkista että siirtonopeus on oikea ja<CRLF>"
	echo "<RED>modeemi kytketty.<CRLF>"
        cancel tmp1               ; poista temp
        goto takaisin

;
; Alustus
;
init:
	set esc	   "^["
	set cs     "^L"
	set con    "^Q"
	set cof    "^T"
	set red    "<esc>A"
	set gre    "<esc>B"
	set yel    "<esc>C"
	set cya    "<esc>F"
	set dbl    "<esc>M"
	set nrm    "<esc>L"
	set nbg    "<esc>]"
	set crlf   "^M^J"
	return

;
; Soita, hae sivut, vapauta puhelin
;
tee-se:
	call par-telesampo	; Aseta nopeus, pariteetti ym. parametreja
	call soita		; Ota yhteys
	wait 00:00.15		; Odota viisitoista sekuntia
;	hide screen 		; poista näytön nimi ruudusta
	call <1>		; suorita määritty työ
	call header
	call pure-linja		; vapauta linja
	return			; Valmis

;
; Sananluku-rutiini
;
readword:
	lset result ""

	; Tyhjennä näppäimistöpuskuri
	if "<#KEYBUF>" # "" THEN KEY

read-continue:

	; Lue yksi merkki
	key

	; Jos se oli RETURN-näppäin, palauta tulos
	if "<INKEY>" = "^M" THEN RETURN "<result>"

	; Lisätään merkki `result'-merkkijonoon
	lset result "<result><inkey>"

	echo "<inkey>"

	; Backspace?
	if "<inkey>" # "^H" then goto read-continue

	; poista edellinen merkki
	split "<result>" "^H" result none

	strlen "<result>"
	lset result "<result>" 1 <ret>
	goto read-continue

;
; strlen - antaa merkkijonon pituus
;
strlen: lset result 0
	lset work "<1>"
strlen-2:
	lset temp "<work>" <result> <result>
	if "<temp>" = "" then return "<result>"
	lset result <result> + 1
	goto strlen-2
