failat 21

; ======================================
;    FASTCONFIG v1.0 by Paolo Besser
; --------------------------------------
;
; Script which allow creating, saving &
; restoring a system configuration in a
; single passage. 
; ======================================

; initialization

if exists T:SVCNFG
  delete T:SVCNFG ALL FORCE QUIET
endif

; creates the main repository for files
makedir T:SVCNFG
assign ddest: T:SVCNFG

; main choice
requestchoice >env:mainchc "FASTCONFIG 1.0" "This script allows easy save & recover of system*nsettings. Its goal is the creation of hardware profiles*nwhich may be easily applied to new installations.*nBut it can be used for personal backups, too!" "Load settings|Save settings|Quit"

if ${mainchc} EQ 0 
 skip FINE
EndIf

; --- START LOADCONFIG ---

If ${mainchc} EQ 1

requestfile >env:conffile TITLE="Choose settings file" DRAWER="SYS:Storage/Config" PATTERN="#?.zip"
 if WARN
  skip FINE
 endif

; wait 5 SECS
unzip -o ${conffile} -d T:SVCNFG
; wait 3 SECS

delete env:conffile >NIL:

; perform a couple of test to see if it's a good archive

if NOT EXISTS T:SVCNFG/AROSTCP/AutoRun
  requestchoice >NIL: "Error!" "Required configuration file is missing or corrupt." "Quit"
  skip FINE
endif

if NOT EXISTS T:SVCNFG/SYS/ahi.prefs
  requestchoice >NIL: "Error!" "Required configuration file is missing or corrupt." "Quit"
  skip FINE
endif

if WARN
  requestchoice >NIL: "Error!" "Required configuration file is missing or corrupt." "Quit"
  skip FINE
endif

copy T:SVCNFG/#? TO envarc: ALL QUIET FOOVR NOPRO

requestchoice >NIL: "Restore completed" "Configuration files successfully applied.*nPlease reboot system to enable them." "Exit"

skip FINE

EndIF

; END LOADCONFIG --------------------

; --- START SAVECONFIG --------------

; ask user the type of backup he's (or she's) performing
requestchoice >env:svmode "Save menu" "FASTCONFIG can save different degrees of settings:*n*nHARDWARE = saves only hardware related settings*nCUSTOM = user can choose and add other files*nFULL = user can make a complete envirnonment backup" "Hardware|Custom|Full|Cancel"

; depending on the kind of backup, naming convention may change
; anyway, variable $conffile will store the name of the backup archive.

; if the user choose FULL backup, copy all settings
if ${svmode} EQ 3
 copy envarc:#? TO ddest: ALL QUIET NOPRO
 ; save configuration file
 requestfile >env:conffile TITLE="Save backup as..." FILE="backup.zip" PATTERN="#?.zip" SAVEMODE DRAWER="MyWorkspace:"
  If WARN
   skip FINE
  endif
 skip COMPRESS
EndIf

makedir ddest:AROSTCP
; makedir ddest:Icaros
makedir ddest:SYS

copy envarc:AROSTCP ddest:AROSTCP ALL QUIET NOPRO
copy envarc:hdaudio.config ddest:
copy envarc:SYS/ahi.prefs ddest:SYS
; ......
; add here everything you should copy in default backups

; skip to choose name section of this script.
if ${svmode} EQ 1
 skip CHOOSENAME
EndIf

; if the user chose CUSTOM, prompt for files to add
if ${svmode} EQ 2

 requestchoice >NIL: "SaveConfig Informations" "SaveConfig will now prompt for files. Please add*nany configuration setting you'd like to add to your*nhardware configuration. Click on Cancel to stop adding files." "Continue"

 lab LOOPBEGIN

 requestfile >env:SETTFILE DRAWER="envarc:" TITLE="Add setting file..." POSITIVE="Add" NEGATIVE="Stop"
  if WARN
   ; exit this loop if the user clicks on Cancel
   skip CHOOSENAME
  endif

 ; tests if user has chosen the a file from envarc: (thanks Ball000)
 c:cut ${SETTFILE} WORD 1 SEPARATOR ":" >env:SETTEST
    ; debug
    ; echo ${SETTEST}
  if ${SETTEST} NOT EQ envarc
    requestchoice >NIL: "Warning!" "You must choose a setting file from envarc*nNo other locations are allowed. Please repeat selection" "Continue" 
    delete env:SETTEST >NIL:
    skip LOOPBEGIN back
  endif
  delete env:SETTEST >NIL:

 ; tests if the user has included parent files into the archive
 c:cut ${SETTFILE} CHAR 8 >env:SETTEST
  if ${SETTEST} EQ "/"
    requestchoice >NIL: "Warning!" "You must choose a setting file from envarc*nNo other locations are allowed. Please repeat selection" "Continue" 
    delete env:SETTEST >NIL:
    skip LOOPBEGIN back
  endif
  delete env:SETTEST >NIL:

 ; store in SETTPATH path of selected file
 c:list ${SETTFILE} lformat=%P >env:SETTPATH

 ; OK, now take just the first line for drawers sake (thanks, Vincenzo)
 development:bin/head -1 env:SETTPATH >env:SETTPATH1
    ; debug
    ; type env:SETTPATH
    ; echo -------------------------
    ; type env:SETTPATH1
 delete env:SETTPATH >NIL:
 rename env:SETTPATH1 env:SETTPATH
 
 ; purge SETTPATH from the final / character, and place in SETTPATHOK (Thanks, Oliver!)
 development:bin/sed -e "s/\/$//" ENV:SETTPATH >ENV:SETTPATHOK
    ; debug note
    ; echo "path ${SETTPATHOK}"

 ; clean SETTPATHOK from the initial envarc: volume
 lua:lua extras:S/replacer.lua env:SETTPATHOK "envarc:" ""
    ; place in SETTFILEOK filename only, and show it for debug
    ; list ${SETTFILE} lformat=%N >env:SETTFILEOK
    ; echo "file ${SETTFILEOK}"

 ; create the same directory as source, in the temp destination drawer
 makedir ddest:${SETTPATHOK} >NIL:

 ; ..and finally copy file
 copy ${SETTFILE} TO ddest:${SETTPATHOK} QUIET NOPRO ALL
 ; if SETTFILE is a directory, copy all contents inside

 delete env:SETTPATHOK >NIL:
 skip LOOPBEGIN back
 
EndIf


; final tasks 

lab CHOOSENAME

; naming for HARDWARE and CUSTOM configs should always include
; computer producer and model.

requeststring >env:cpbrand TITLE="Enter Brand name" TEXT="Please enter name of your computer producer.*n(For instance ACER, ASUS, ARES, CLUSTRUK...)*NDO NOT USE SPACES!"
requeststring >env:cptname TITLE="Enter computer model" TEXT="Now insert computer model, with any related*numbering (Timeline-4838, AspireOne-A150...)*NDO NOT USE SPACES!"

; now create a name like BRANDNAME_ComputerModel...
echo xxx_yyy >env:preconffile
lua:lua extras:S/replacer.lua env:preconffile "xxx" ${cpbrand}
lua:lua extras:S/replacer.lua env:preconffile "yyy" ${cptname}

requestfile >env:conffile TITLE="Save backup as..." FILE=${preconffile} PATTERN="#?.zip" SAVEMODE DRAWER="MyWorkspace:"
 If WARN
  skip FINE
 EndIf

lab COMPRESS

 ; delete CURRENT_INSTALL if present, or it may overwrite Icaros versions in future
 delete ddest:CURRENT_INSTALL >NIL:
; compress files into a ZIP archive
zip -r ${conffile} ddest:#?

lab FINE

assign ddest: REMOVE
delete env:preconffile >NIL:
delete env:cpbrand >NIL:
delete env:cptname >NIL:
delete env:conffile >NIL:
delete env:mainchc >NIL:
delete env:SETTFILE >NIL:
delete env:SETTPATH >NIL:
delete env:svmode >NIL:
