
! script name: property.d/maketemplate
! topic: make an info packet template

!A
!A NAME:
!A
!A 	Make Template 
!A
!A DESCRIPTION:
!A
!A 	This script creates an info packet template.
!A
!B INSTRUCTIONS:
!B
!B 	PACKET CLASS DIRECTORY: Select the class directory of the packet
!B 	or type in the name of a new directory.
!B
!B 	PACKET FILE NAME: Select the name of the file containing the
!B 	packet information.  This should be the same as the name in
!B 	the Name: data field.
!B
!B 	PACKET CLASS NAME: Select the name of the packet's class.  All packets
!B 	in the same class directory should have the same class name.
!B
!B 	CLASS ID NUMBER  (10000 - 100000): Select a class ID number
!B 	(from 10000 to 100000).  A unique number must be assigned to
!B 	each packet class. Each packet in the class must have the same
!B 	class number. Each class must have a unique ID number.  Only
!B 	one packet of a specific class number can be associated
!B 	with a Synergy entity, however, many packets of different
!B 	classes may be associated with the same entity.
!B
!B 	DATA FIELD NAME,  Proceed: Enter a data name field.
!B
!B 	DATA VALUE: Enter a data field value.  This data may be a string.
!B
!# see also - htdocs/help.d/property.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: info packet template")

DIM Destination$[512]
DIM Answer$[512]
DIM Name$[512]
DIM A$[512]
DIM B$[512]
DIM Prop_dir$[512]
DIM Prop_class$[512]
DIM Class_dir$[512]
DIM Filename$[512]
DIM Ent_Name$[512]
DIM Class_name$[512]
DIM Prop_name$[512]
DIM Field$[512]
DIM Value$[512]
DIM Data$[32000]
DIM Field_names$(20)[256]
DIM Field_values$(20)[256]
DIM D_label$[512]
DIM Choice$[512]
DIM Current_dir$[512]
DIM Ques$[512]
DIM Sub_class$[512],Sub_class2$[512]

Stat= NamedVarStr("properties dir",WsSystemVars,"/usr/weber/database/properties/",Prop_dir$)

Start:!
D_label$="PACKET CLASSES"
Choice$="CLASS SELECTED"
Ques$= "PACKET CLASS DIRECTORY  List  "
Answer$= SelectDirectory(Prop_dir$,"*",Ques$,D_label$,Choice$,"",Current_dir$)
IF (ExitScript(Destination$)) THEN Leave
IF  Answer$= "CANCEL" THEN Leave
Answer$= TRIM$(Answer$)
Class_dir$= Answer$
Prop_class$= PathNameFile(Answer$)
IF NOT DoesFileExist(Class_dir$) THEN
Next_class:!
	Sub_class2$= Class_dir$
Next_inst:Sub_class$= PathNameDirectory(Sub_class2$)
	IF DoesFileExist(Sub_class$) THEN
		CREATE DIR Sub_class2$
		IF Sub_class2$<>Class_dir$ THEN Next_class
	ELSE
		Sub_class2$= Sub_class$
		GOTO Next_inst
	END IF
END IF
LowerSelectBox

File_name:!
D_label$="EXISTING PACKETS"
Choice$="PACKET SELECTED"
Stat=DirListBox(Class_dir$,"*",D_label$,Choice$,WsAscending)

stat= SetInputFormat("a99","")
stat= GetValue("PACKET FILE NAME ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT LEN(Answer$) THEN File_name
IF Answer$="CANCEL" THEN Start
Prop_name$= MakeNameValid(Answer$,WsValidFileName)
Filename$= FileNameString(Class_dir$,Prop_name$)

F_type= DoesFileExist(Filename$)
SELECT F_type
CASE 1
        Stat=SetInputFormat("a1",",Y,N,")
        Stat=GetValue("PACKET FILE EXISTS: OVERWRITE  Yes or No",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "N" THEN
                GOTO File_name
        END IF
CASE 2
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("PACKET NAME IS A DIRECTORY: Proceed ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        GOTO Filename
END SELECT


Ask_class:stat= SetInputFormat("a99","")
stat= GetValue("PACKET CLASS NAME ",Class_name$)
IF (ExitScript(Destination$)) THEN Leave
IF NOT LEN(Class_name$) THEN Ask_class

Ask_id_num:stat= SetInputFormat("n0a1",",E,")
Id_number= GetValue("CLASS ID NUMBER  (10000 - 100000)     Existing ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "E" THEN
	stat= PropClassNameList(Prop_dir$,":",2)
	GOTO Ask_id_num
END IF
IF Id_number< 10000 OR Id_number> 100000 THEN Ask_id_num

Ask_name:!
IF NOT LEN(Prop_name$) THEN
	stat= SetInputFormat("a99","")
	stat= GetValue("PACKET NAME ",Name$)
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT LEN(Name$) THEN Ask_name
ELSE
	Name$= Prop_name$
END IF


Nfields= 0

Field_loop:!
stat= SetInputFormat("a99","",WsFalse)
stat= GetValue("DATA FIELD NAME,  Proceed  ",Field$)
IF (ExitScript(Destination$)) THEN Leave
Field$= TRIM$(Field$)
SELECT Field$
CASE "P", "p", "Proceed","PROCEED"
	GOTO Make_property
END SELECT
IF NOT LEN(Field$) THEN Field_loop

stat= SetInputFormat("a99","",WsFalse)
stat= GetValue("DATA VALUE ",Value$)
IF (ExitScript(Destination$)) THEN Leave

Nfields= Nfields+1
Field_names$(Nfields)= Field$&":"
Field_values$(Nfields)= Value$
GOTO Field_loop


Make_property:!
E1= CreateUserData(Id_number,Name$,CHR$(10))
Stat= PutUserString(E1,"Class name:",Class_name$)
Stat= PutUserValue(E1,"Class id number:",Id_number)
Stat= PutUserString(E1,"Name:",Name$)
FOR I= 1 TO Nfields
	Field$= Field_names$(I)
	Value$= Field_values$(I)
	Stat= PutUserString(E1,Field$,Value$)
NEXT I
Data$= UserData(E1)
Stat= MessageStringBox(Data$,"Info Packet: "&Name$)
Stat= OutputUserData(E1,Filename$,WsNewFile)
Stat= DeleteEntity(E1)

GOTO Start

Leave:!
Stat=SetInputFormat("n4","")
LowerSelectBox
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END


