
! script name: filing.d/storage
! topic: save file at present location

!A
!A NAME:
!A
!A 	Save
!A
!A DESCRIPTION:
!A
!A 	This script will save a file at the present file location. The first
!A 	time this script is accessed it will ask the storage mode and where
!A 	it should be stored.  The following are the 4 different Synergy system
!A 	component files that will be stored in the file.
!A
!A 		Geometry
!A		Turbo post command files
!A		Turbo post tape files
!A		Turbo post notepad files
!A
!A 	To store a file under a different name, the "SAVE AS" script may be
!A 	used or the name of the file may be edited using the EDIT VARS option.
!A
!B INSTRUCTIONS:
!B
!B 	SAVE MODE IS Entire, Geometry:
!B
!B 		E - Entire will save the entire job, geometry and turbo_info.
!B 		G - Geometry will only save the geometry portion of the file.
!B 		T - Turbo_info will only save the TURBO POST information.
!B 		    This feature is hidden from the question line.
!B
!B 	SAVE JOB AS:  Enter the name the job is to be stored as.
!B 		The name may not have any spaces in it.
!B
!B 	FILE DESCRIPTION : Enter a brief file description
!B 		to be stored with the job.
!B

DIM Answer$[256],Destination$[256],Header$[4000],A$[256]
DIM Descr$[256],Datestr$[256],Sdevice$[256],Jobname$[256]
DIM Tmp$[256],Space$[25],Jobs$[25],Unit$[256],Dir$[256],Dir1$[256]
DIM Mask$[256],Namelist$[5000],Filelist$(100)[100]
DIM Name$[256],D_label$[256],Sub_label$[256],F_label$[256],Choice$[256]


DeActivateUndo
Stat=ChangeLabelStr("ScriptNameStatus","Function: save ")

Stat= NamedVarStr("storage dir",WsSystemVars,"/usr/weber/ST/",Sdevice$)

Op_mode= NamedVarVal("how to save files",WsSystemVars,9999.0)

IF Op_mode= 9999 THEN
	Stat=SetInputFormat("a1",",E,G,T,")
	Stat=GetValue("SAVE MODE IS Entire, Geometry ",Answer$)
	IF (ExitScript(Destination$)) THEN Leave
	SELECT Answer$
	CASE "G"
		Tempvar= CreateVariable("how to save files",WsSystemVars,1.0)
	CASE "E"
		Tempvar= CreateVariable("how to save files",WsSystemVars,2.0)
	CASE "T"
		Tempvar= CreateVariable("how to save files",WsSystemVars,3.0)
	END SELECT
	Op_mode= NamedVarVal("how to save files",WsSystemVars,9999.0)
END IF


Stat= CurrentHeader(Header$)
IF LEN(Header$) THEN
	Head=ReadUserData(WsGeneralString,"HEADER",Header$)
	IF ValidGeometry(Head) THEN
		Stat=GetUserString(Head,"File name:",Jobname$)
		if(LEN(Jobname$)) THEN Ask_name=0
		if(POS(Jobname$,"NONE")) THEN Ask_name=1
	END IF
ELSE
	Ask_name=1
END IF

IF Ask_name THEN
Space$= " "
Question:!
D_label$="DIRECTORY"
Sub_label$="SUB DIRECTORIES"
F_label$="FILES"
Choice$="FILE TO SAVE"
!   Mask$="[A-Za-z0-9]*"
Mask$= "Synergy Files (*.syn)|*.syn||"
Stat=UserFileBox(Sdevice$,Mask$,D_label$,Sub_label$,F_label$,Choice$,"Synergy")
Ques2:!
Stat=SetInputFormat("a99","")
Stat=GetValue("SAVE JOB AS: "&Space$,Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
!	IF POS(Answer$," ") THEN
!		Space$= " (NO SPACES) "
!		GOTO Ques2
!	ELSE
!		Space$= " "
!	END IF
IF NOT POS(Answer$,"/") THEN
	Jobname$= FileNameString(Sdevice$,Answer$)
ELSE
	Testposr=1
	REPEAT
		Testpos=POS(Answer$[Testposr],"/")
		Testposr=Testposr+Testpos
	UNTIL (NOT Testpos)
	Namelen=LEN(Answer$)-Testposr+1
	
	IF NOT Namelen THEN Ques2
	Jobname$= Answer$
END IF
IF NOT (POS(Jobname$,".syn") OR POS(Jobname$,".SYN")) THEN 
		Jobname$= Jobname$&".syn"
END IF

LowerFileBox
IF NamedVarVal("save dirs",WsSystemVars,0) THEN V1=CreateVariable("storage dir",WsSystemVars,PathNameDirectory(Jobname$))

F_type= DoesFileExist(Jobname$)
SELECT F_type
CASE 1
        Stat=SetInputFormat("a1",",Y,N,")
        Stat=GetValue("FILE EXISTS: OVERWRITE  Yes or No",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "N" THEN
		GOTO Question
        END IF
CASE 2
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("COPY NAME IS A DIRECTORY: Proceed ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
	GOTO Question
END SELECT
ELSE
	Stat= DisplayMessage("STORING FILE "&Jobname$)
END IF

IF ValidGeometry(Head) THEN
	Stat= DateString(Datestr$)
	IF Ask_name THEN
		Stat=SetInputFormat("a99","")
		Stat=GetValue("FILE DESCRIPTION:",Answer$)
		IF (ExitScript(Destination$)) THEN Leave
		Descr$= Answer$
		Stat=PutUserString(Head,"File name:",Jobname$)
		Stat=PutUserString(Head,"Description:",Descr$)
		Stat=PutUserString(Head,"Creation date:",Datestr$)
	ELSE
		Stat= PutUserString(Head,"Revision date:",Datestr$)
	END IF
        Units= CurrentUnits
        Unit$= RPT$("METRIC",Units= WsMetric)&RPT$("INCHES",Units=WsInch)
        Stat=PutUserString(Head,"Units:",Unit$)
	AccTime = TIMEDATE-NamedVarVal("job start time",WsSystemVars,0.0)
	AccTimer = NamedVarVal("accumulated time",WsSystemVars,0.0)
	AccTime=AccTime/60+AccTimer
	Stat=PutUserString(Head,"Accumulated Time:"," "&VAL$(PROUND(AccTime,-2))&" minutes")
END IF

Stat= DelUnusedProp(WsFixtureWorkplane)
Stat= DelUnusedProp(WsPocketBdy)
Stat= DelUnusedProp(WsOffsetBdy)
Stat= DelUnusedProp(Ws3dCpath)
Stat= DelUnusedMachDef
Stat= DelEmptyGroups
TransformCleanup
UnSelectAll

Filecnt= 1
IF Op_mode<3 THEN
	First= 0
	Filelist$(Filecnt)="Geometry"
ELSE
	First=1
END IF

IF ValidGeometry(Head) THEN
	FOR I=1 TO 2
		Stat= PutUserString(Head,"Component files "&VAL$(I)&":","")
	NEXT I
END IF

IF Op_mode> 1 THEN
	Dir$= "./"
	Mask$= "notepad*"
	Stat= DirectoryNames(Dir$,Mask$,Namelist$)
	GOSUB AddFiles
	Mask$= "command_file*"
	Stat= DirectoryNames(Dir$,Mask$,Namelist$)
	GOSUB AddFiles
	Mask$= "tapefile*"
	Stat= DirectoryNames(Dir$,Mask$,Namelist$)
	GOSUB AddFiles
END IF
IF ValidGeometry(Head) THEN
	FOR I=1 TO Filecnt
		Stat= PutUserString(Head,"Component files "&VAL$(I)&":",Filelist$(I))
	NEXT I
	WHILE CheckUserField(Head,"Component files "&VAL$(I)&":",1)
		Stat= PutUserString(Head,"Component files "&VAL$(I)&":","EMPTY")
		I= I+1
	END WHILE
	Enti= UserDataString(Head,Header$)
	Stat= FileHeader(Header$)
	Stat= DeleteEntity(Head)
END IF

stat=ChangeLabelStr("websys",Jobname$)
Stat= UpdateSettings("CadSettings",WsCadSettings)
Stat=StoreFile(Jobname$,Op_mode)
IF Stat THEN
   IF Stat= 2 THEN
      Stat=SetInputFormat("a99","")
      Stat=GetValue("WARNING -- NOT SAVED: file not synergy file",Answer$)
   ELSE
      Stat=SetInputFormat("a99","")
      Stat=GetValue("WARNING -- FILE NOT STORED: cannot open file",Answer$)
   END IF
   IF (ExitScript(Destination$)) THEN Leave
   GOTO Leave
END IF

Leave:!
LowerFileBox
IF ValidGeometry(Head) THEN Stat= DeleteEntity(Head)
IF Op_mode>1 THEN
	Group= GrpByEntType(WsGeneralFile)
	IF ValidGeometry(Group) THEN Stat= DeleteGroup(Group)
END IF

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

Addfiles:!
Comma= POS(Namelist$,",")
WHILE Comma
	Jobs$= Namelist$[1,Comma-1]
	Namelist$= Namelist$[Comma+1]
	Skip_file=0
	IF POS(Jobs$,"tapefile")>1 THEN Skip_file=1
	IF POS(Jobs$,"command_file")>1 THEN Skip_file=1
	IF POS(Jobs$,"notepad")>1 THEN Skip_file=1
	IF NOT Skip_file THEN
	File_there= CreateGeneralFile(Jobs$)
	IF File_there THEN
		IF (LEN(Filelist$(Filecnt))+LEN(Jobs$))>50 THEN 
			Filecnt=Filecnt+1
		ELSE 
			IF First THEN
				First= 0
			ELSE 
				Filelist$(Filecnt)[LEN(Filelist$(Filecnt))+1]= ",  "
			END IF
		END IF
		Filelist$(Filecnt)[LEN(Filelist$(Filecnt))+1]= Jobs$
	END IF
	END IF
	Comma= POS(Namelist$,",")
END WHILE
RETURN
END
