
! script name: archive.d/archivefile
! topic: archive a file

!A
!A NAME:
!A
!A 	Archive a File
!A
!A DESCRIPTION:
!A
!A 	The file archive is designed to store the synergy file along with
!A 	all associated file components into a directory structure.
!A
!B INSTRUCTIONS:
!B
!B 	SELECT PROJECT New directory,  List: This question
!B 		appears when a project is to be archived for the
!B 		first time. Enter the name of the project to be
!B 		created or create a new directory of projects.
!B
!B 	If a project name is specified then a new project of that name will
!B 	be created in the current directory. An information box will then
!B 	appear to be filled in for the project. This is the information that
!B 	is associated with the project in the file info.txt.
!B
!B 	If a New directory of projects is needed, the name of the directory
!B 	is to be specified and an information box will appear to be filled in.
!B 	This is the data that is associated with the directory in dinfo.txt.
!B
!B 	If a project has been loaded from the archive, Archiving the project
!B 	will result in a new revision of that project being created.  The
!B 	first time a project is archived it is called revision1.
!B
!B 	If a project is Updated, the current revision is changed and no new
!B 	revision number is created. Only the current revision may be updated.
!B
!B 	The system will not allow storage onto a previous revision.
!B
!# see also - htdocs/help.d/archive.htm

OPTION BASE 1
DIM Answer$[256],Destination$[256],Header$[4000],Header1$[4000]
DIM Descr$[256],Creation_date$[256],Revision_date$[256],Acc_time$[256]
DIM Space$[25],Unit$[256]
DIM Ques$[256]
DIM D_label$[256],Choice$[256],Dir$[256],Mask$[256]
DIM Archive$[512]
DIM Project$[512]
DIM Revision$[512],Last_rev$[512],Rev_name$[512]
DIM Name$[512]
DIM Synfile$[512]
DIM Prefix$[256]
DIM Archive_dir$[512],Project_dir$[512],Current_dir$[512],Prop_dir$[512]
DIM Namelist$[32000],Storelist$[32000]
DIM Archive_status$[256]
DIM A$[32000],B$[512],C$[512]
DIM File_types$[256]
DIM Filename$[512]

Stat=ChangeLabelStr("ScriptNameStatus","Function: archive ")

DeActivateUndo
IF ARGC<2 THEN
	Archive_status$="ARCHIVE"
ELSE
        Archive_status$= ARGV(1)
END IF
IF ARGC<3 THEN
   A$=" synfile command_file notepad tapefile .gif .jpg .html .htm .hpgl .plt "
   A$= A$&" .doc .xls "
   Stat= NamedVarStr("archive file types",WsSystemVars,A$,File_types$)
ELSE
   File_types$= ARGV(2)
END IF


Archive_status$= UPC$(Archive_status$)
Stat= NamedVarStr("archive dir",WsSystemVars,"/usr/weber/archive/",Archive$)
Stat= NamedVarStr("current project",WsSystemVars,"",Project_dir$)
Stat= NamedVarStr("current revision",WsSystemVars,"",Revision$)

IF NOT DoesFileExist(Archive$) THEN
        CREATE DIR Archive$
END IF
Archive_dir$= Archive$
Prefix$= "revision"

IF NOT LEN(Project_dir$) THEN Project_dir
Namelist$=SubDirectoryNames(Project_dir$,"*",2,WsAscending)
IF NOT POS(Namelist$,Prefix$) THEN Project_dir

Rev_name$=""
Last_rev$=""
SELECT Archive_status$
CASE "ARCHIVE_AS"
	Rev_name$=""
	Last_rev$= ""
	GOTO Project_dir
CASE "ARCHIVE"
	Rev_name$= NextNumberedName(Namelist$,Prefix$,",")
	Last_rev$= FileNameString(Project_dir$,Revision$)
	GOTO Store_revision
CASE "UPDATE"
	Rev_name$= LastNumberedName(Namelist$,Prefix$,",")
	IF Rev_name$<> Revision$ THEN
		Stat=SetInputFormat("a99"," ")
		Stat=GetValue("UPDATE NOT ALLOWED: NOT LATEST REVISION",Answer$)
		IF (ExitScript(Destination$)) THEN Leave
		GOTO Leave
	ELSE
		Rev_name$= LastNumberedName(Namelist$,Prefix$,",")
		Last_rev$= FileNameString(Project_dir$,Rev_name$)
		GOTO Store_revision
	END IF
END SELECT

Project_dir:!
Ques$= "SELECT PROJECT New directory,  List "
D_label$= "PROJECTS"
Choice$= "selected project"
Answer$= SelectDirectory(Archive_dir$,"*",Ques$,D_label$,Choice$,Prefix$,Current_dir$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "CANCEL"
	GOTO Leave
	GOTO Project_dir
CASE "New"
	LowerSelectBox
	IF NOT LEN(Current_dir$) THEN Current_dir$= Archive_dir$
	Stat=ChangeLabelStr("Status","Location: "&Current_dir$)
	Stat=SetInputFormat("a99","")
        Stat=GetValue("NEW PROJECT DIRECTORY NAME",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        Project$=UniformPathName(Answer$)
        Project_dir$= UniformPathName(FileNameString(Current_dir$,Project$))
        IF NOT DoesFileExist(Project_dir$) THEN
		CALL DirectoryInfo(Namelist$,Destination$,Leave_script,Project$)
		IF Leave_script THEN Leave
                CREATE DIR Project_dir$
        	Archive_dir$= Project_dir$
		IF LEN(Namelist$) THEN
			DirInfo=ReadUserData(WsGeneralString,"Project directory information",Namelist$)
			Stat= OutputUserData(DirInfo,FileNameString(Project_dir$,"dinfo.txt"),WsNewFile)
			Stat= DeleteEntity(DirInfo)
		END IF
        	GOTO Project_dir
        ELSE
                Stat=SetInputFormat("a99","")
                Stat=GetValue("PROJECT DIRECTORY ALREADY EXISTS, Proceed",Answer$)
                IF (ExitScript(Destination$)) THEN Leave
                GOTO Project_dir
        END IF
        GOTO Project_dir
END SELECT
Project$= UniformPathName(PathNameFile(Answer$))
Project_dir$= UniformPathName(Answer$)
IF NOT DoesFileExist(Project_dir$) THEN
        CREATE DIR Project_dir$
	Rev_name$= Prefix$&"1"
	GOTO Store_revision
ELSE
	IF DoesFileExist(FileNameString(Project_dir$,"dinfo.txt")) THEN
		Archive_dir$= Project_dir$
		Project_dir$=""
		GOTO Project_dir
	END IF
	Namelist$=SubDirectoryNames(Project_dir$,"*",2,WsAscending)
	IF POS(Namelist$,Prefix$) THEN 
		Stat=SetInputFormat("a99","")
                Stat=GetValue("PROJECT ALREADY EXISTS, Proceed",Answer$)
                IF (ExitScript(Destination$)) THEN Leave
		Project_dir$=""
		Archive_dir$= Archive$
		GOTO Project_dir
	ELSE
	   	Rev_name$= Prefix$&"1"
	   	GOTO Store_revision
	END IF
END IF
LowerSelectBox
GOTO Store_revision

Store_revision:!
Creation_date$=""
Revision_date$=""
Acc_time$=""
IF LEN(Last_rev$) THEN
   A$=FileNameString(Last_rev$,"info.txt")
   ProjectInfo= ReadUserData(WsArchiveFileInfo,"Project information",A$)
END IF
IF NOT ValidGeometry(ProjectInfo) THEN
   CALL ProjectInfo(Namelist$,Destination$,Leave_script,Project$)
   IF Leave_script THEN Leave
   IF LEN(Namelist$) THEN
      ProjectInfo=ReadUserData(WsGeneralString,"Project information",Namelist$)
   END IF
END IF

Revision_notes:!
	Rev_number=0
	N1= POS(Rev_name$,Prefix$)
	N2= N1+LEN(Prefix$)
	IF N1 THEN Rev_number= VAL(Rev_name$[N2])
	IF Rev_number < 2 THEN Update_revision
	
	Stat=SetInputFormat("a99","")
	A$= UPC$(Prefix$[1,1])&Prefix$[2]&" "&VAL$(Rev_number)
	Answer$ = UserDataStr(ProjectInfo,A$&":")
	IF LEN(Answer$) THEN Stat= SetEditString(Answer$)
	A$= UPC$(Prefix$)&" "&VAL$(Rev_number)
        Stat=GetValue(A$&" NOTE:   None ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
	SELECT Answer$
	CASE "None","none","NONE","n","N"
		GOTO Update_revision
	END SELECT
	A$= UPC$(Prefix$[1,1])&Prefix$[2]&" "&VAL$(Rev_number)
	Stat= PutUserString(ProjectInfo,A$&":",Answer$)

Update_revision:!
SELECT Archive_status$
CASE "UPDATE"
	Stat=SetInputFormat("a1=",",Y,N,")
	Stat= SetEqualsValue(1,"N")
	Stat=GetValue("UPDATE LATEST REVISION Yes or No",Answer$)
	IF (ExitScript(Destination$)) THEN Leave
	IF Answer$="N" THEN Leave
END SELECT

Remove_comp_ref:!
I= 1
WHILE CheckUserField(ProjectInfo,"Component files "&VAL$(I)&":",1)
     Stat= DeleteUserField(ProjectInfo,"Component files "&VAL$(I)&":")
     I= I+1
END WHILE

Creation_date:!
IF ValidGeometry(ProjectInfo) THEN
	Descr$= UserDataStr(ProjectInfo,"Description:")
	Creation_date$ = UserDataStr(ProjectInfo,"Creation date:")
END IF
IF NOT LEN(Descr$) THEN
	IF LEN(Project$)>0 THEN
		Descr$=Project$
	ELSE
		Stat=SetInputFormat("a99","")
		Stat=GetValue("FILE DESCRIPTION:",Answer$)
		IF (ExitScript(Destination$)) THEN Leave
		Descr$= Answer$
	END IF
END IF


Accumulated_time:!
AccTime=TIMEDATE-NamedVarVal("job start time",WsSystemVars,0.0)
AccTimer=NamedVarVal("accumulated time",WsSystemVars,0.0)
AccTime=AccTime/60+AccTimer
AccTime= PROUND(AccTime,-2)
Acc_time$= " "&VAL$(AccTime)&" minutes"

Revision$= FileNameString(Project_dir$,Rev_name$)
IF NOT DoesFileExist(Revision$) THEN
	CREATE DIR Revision$
END IF
Op_mode= 1  ! store synergy geometry only rest stored as seperate files.
Synfile$=FileNameString(Revision$,"synfile")

Header_info:!
stat= CurrentHeader(Header1$)
Header$= Header1$
IF LEN(Header$) THEN
   Head=ReadUserData(WsGeneralString,"HEADER",Header$)
   IF ValidGeometry(Head) THEN
	Creation_date$ = UserDataStr(Head,"Creation date:")
	IF ( NOT LEN(Creation_date$)) OR POS(Creation_date$,"NONE") THEN
		Stat= DateString(Creation_date$)
		Revision_date$=""
	ELSE
		stat= DateString(Revision_date$)
	END IF
	Stat= PutUserString(Head,"Creation date:",Creation_date$)
	Stat= PutUserString(Head,"Revision date:",Revision_date$)

	Stat=PutUserString(Head,"File name:",Project_dir$)
	Stat=PutUserString(Head,"Description:",Descr$)
       	Units= CurrentUnits
       	Unit$= RPT$("METRIC",Units= WsMetric)&RPT$("INCHES",Units=WsInch)
       	Stat=PutUserString(Head,"Units:",Unit$)
	Stat= UserDataString(Head,Header$)
   	Stat= PutUserString(Head,"Accumulated Time:",Acc_time$)
	Stat= FileHeader(Header$)
   END IF
END IF

IF ValidGeometry(ProjectInfo) THEN
	IF NOT LEN(Creation_date$) THEN
		Creation_date$ = UserDataStr(ProjectInfo,"Creation date:")
	END IF
	IF ( NOT LEN(Creation_date$)) OR POS(Creation_date$,"NONE") THEN
           	Stat= DateString(Creation_date$)
		Revision_date$=""
	ELSE
		stat= DateString(Revision_date$)
	END IF
        Stat= PutUserString(ProjectInfo,"Creation date:",Creation_date$)
        Stat= PutUserString(ProjectInfo,"Revision date:",Revision_date$)

	Stat=PutUserString(ProjectInfo,"Description:",Descr$)
	Stat=PutUserString(ProjectInfo,"Units:",Unit$)
	Stat=PutUserString(ProjectInfo,"Accumulated Time:",Acc_time$)
END IF

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

Store_header:!   don't put component files into synergy file header

IF LEN(Header1$) THEN Stat= FileHeader(Header1$)

Dir$= "./"
Namelist$= ""

IF ((File_types$="")OR(POS(File_types$,"synfile")>0)) THEN
	Namelist$= "synfile,"
END IF
IF ((File_types$="")OR(POS(File_types$,"notepad")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"notepad")
	Namelist$= Namelist$&A$
END IF

IF ((File_types$="")OR(POS(File_types$,"command_file")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"command_file")
	Namelist$= Namelist$&A$
END IF
IF ((File_types$="")OR(POS(File_types$,"tapefile")>0)) THEN
	Mask$= "tapefile*"
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"tapefile")
	Namelist$= Namelist$&A$
END IF
IF ((File_types$="")OR(POS(File_types$,".gif")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"",".gif")
	Namelist$= Namelist$&A$
END IF

IF ((File_types$="")OR(POS(File_types$,".jpg")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"",".jpg")
	Namelist$= Namelist$&A$
END IF

IF ((File_types$="")OR(POS(File_types$,".html")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"",".html")
	Namelist$= Namelist$&A$
END IF

IF ((File_types$="")OR(POS(File_types$,".htm")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"",".htm")
	Namelist$= Namelist$&A$
END IF

IF ((File_types$="")OR(POS(File_types$,".hpgl")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"",".hpgl")
	Namelist$= Namelist$&A$
END IF

IF ((File_types$="")OR(POS(File_types$,".plt")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"",".plt")
	Namelist$= Namelist$&A$
END IF
IF ((File_types$="")OR(POS(File_types$,".doc")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"",".doc")
	Namelist$= Namelist$&A$
END IF
IF ((File_types$="")OR(POS(File_types$,".xls")>0)) THEN
	A$= DirectoryNamesStr(Dir$,"*",WsAscending,"",".xls")
	Namelist$= Namelist$&A$
END IF

IF ValidGeometry(Head) THEN
	! remove references to components from the header
	I=1
	WHILE CheckUserField(Head,"Component files "&VAL$(I)&":",1)
		Stat= DeleteUserField(Head,"Component files "&VAL$(I)&":")
		I= I+1
	END WHILE
	Stat= UserDataString(Head,Header$)
	Stat= FileHeader(Header$)
	Stat= DeleteEntity(Head)
END IF

Store_info:!
IF ValidGeometry(ProjectInfo) THEN
   Filecnt= StringFieldCount(Namelist$,",")+1
   I1=1
   Storelist$=""
   FOR I=1 TO Filecnt
      Stat= StringFieldStr(Namelist$,",",I,A$)
      A$= TRIM$(A$)
      Name$= A$
      IF POS(A$, ".")=1 THEN A$=""
      IF POS(Storelist$,CHR$(1)&A$&CHR$(1)) THEN A$=""
      IF LEN(A$) THEN
         IF NOT (POS(A$,"synfile")) THEN 
            IF NOT (POS(A$,".html")+ POS(A$,".gif") + POS(A$,".jpeg")) THEN 
               IF NOT (POS(A$,".plt")+ POS(A$,".hpgl")) THEN 
               IF NOT (POS(A$,".doc")+ POS(A$,".xls")) THEN 
               IF NOT (POS(A$,".htm")+ POS(A$,".jpg")) THEN 
                  Name$= A$&".txt"
	       END IF
	       END IF
	       END IF
	    END IF
	 END IF
         Stat= CopyFile(A$,FileNameString(Revision$,Name$))
         Stat= PutUserString(ProjectInfo,"Component files "&VAL$(I1)&":",Name$)
	 Storelist$= Storelist$&CHR$(1)&A$&CHR$(1)
         I1= I1+1
      END IF
   NEXT I
   I= I1
   WHILE CheckUserField(ProjectInfo,"Component files "&VAL$(I)&":",1)
        Stat= DeleteUserField(ProjectInfo,"Component files "&VAL$(I)&":")
        I= I+1
   END WHILE
   Stat= OutputUserData(ProjectInfo,FileNameString(Revision$,"info.txt"),WsNewFile)
   Stat= DeleteEntity(ProjectInfo)
END IF

Store_synergy:!
Stat= UpdateSettings("CadSettings",WsCadSettings)
Stat=StoreFile(Synfile$,Op_mode)
IF Stat THEN
   Stat=SetInputFormat("a99","")
   Stat=GetValue("WARNING -- FILE NOT ARCHIVED: cannot open file",Answer$)
   IF (ExitScript(Destination$)) THEN Leave
   GOTO Leave
END IF
V1=CreateVariable("current project",WsSystemVars,Project_dir$)
V1=CreateVariable("current revision",WsSystemVars,Rev_name$)
A$= BaseNamePath(Project_dir$,Archive$)
stat=ChangeLabelStr("websys",A$)

Leave:!
Stat= DeleteEntity(DirInfo)
Stat= DeleteEntity(ProjectInfo)
LowerSelectBox
Stat=ChangeLabelStr("Status"," ")
IF ValidGeometry(Head) THEN Stat= DeleteEntity(Head)

Group= GrpByEntType(WsGeneralFile)
IF ValidGeometry(Group) THEN Stat= DeleteGroup(Group)

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

SUB DirectoryInfo(DirInfo$,Destination$,Leave_script,Project$)
DIM A$[512]
DIM Prop_dir$[512]
DIM Ans$[512]

Leave_script=0
DirInfo$=""
Directory_info:!
Stat= NamedVarStr("properties dir",WsSystemVars,"/usr/weber/database/properties/",Prop_dir$)
A$= UniformPathName(FileNameString(Prop_dir$,"Projects/dirtemplate"))
DirInfo= ReadUserData(WsArchiveFileInfo,"Project directory information",A$)
IF NOT ValidGeometry(DirInfo) THEN Leave_script

Stat= PutUserString(DirInfo,"Directory Name:",Project$)
Edit_data:!
Input_stat= MultiEditDataStr(DirInfo,":")
IF (ExitScript(Destination$)) THEN Leave_script
IF Input_stat=27 THEN Leave_script
IF Input_stat=1 THEN Edit_data

Stat = GetUserString(DirInfo,"Description:",Ans$)
IF LEN(Ans$)<1 THEN Stat=PutUserString(DirInfo,"Description:",Project$)

Stat= UserDataString(DirInfo,DirInfo$)
Stat= DeleteEntity(DirInfo)
GOTO Leave_info

Leave_script: Leave_script=1
Stat= DeleteEntity(DirInfo)
Leave_info:!
SUBEND


SUB ProjectInfo(ProjectInfo$,Destination$,Leave_script,Project$)
DIM A$[512]
DIM Prop_dir$[512]

Project_info$=""
Leave_script=0
Directory_info:!
Stat= NamedVarStr("properties dir",WsSystemVars,"/usr/weber/database/properties/",Prop_dir$)
A$= UniformPathName(FileNameString(Prop_dir$,"Projects/template"))
ProjectInfo= ReadUserData(WsArchiveFileInfo,"Project information",A$)

IF NOT ValidGeometry(ProjectInfo) THEN Leave_script

Stat= PutUserString(ProjectInfo,"Project Name:",Project$)
Edit_data:!
Input_stat= MultiEditDataStr(ProjectInfo,":")
IF (ExitScript(Destination$)) THEN Leave_script
IF Input_stat=27 THEN Leave_script
IF Input_stat=1 THEN Edit_data

Stat= UserDataString(ProjectInfo,ProjectInfo$)
Stat= DeleteEntity(ProjectInfo)
GOTO Leave_info

Leave_script: Leave_script=1
Stat= DeleteEntity(ProjectInfo)
Leave_info:!
SUBEND

