.KEY conffile,mode
.BRA {
.KET }
;ECHO "{conffile}"

failat 21

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

makedir T:SVCNFG
assign ddest: T:SVCNFG

; if the user added ALL as 2nd parameter, copy all settings
if {mode} EQ ALL
 copy envarc:#? TO ddest: ALL QUIET NOPRO
 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

; if the user added PROMPT as 2nd parameter, prompt for files to add
if {mode} EQ PROMPT
 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 COMPRESS
  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
 list ${SETTFILE} lformat=%P >env:SETTPATH
 ; purge SETTPATH from the final / character, and place in SETTPATHOK (Thanks, Oliver!)
 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} ddest:${SETTPATHOK} QUIET NOPRO
 ; if SETTFILE is a directory, copy all contents inside
 copy ${SETTFILE}/#? TO ddest:${SETTPATHOK}/ ALL QUIET NOPRO >NIL:
 delete env:SETTPATHOK >NIL:
 skip LOOPBEGIN back
EndIf

; final tasks
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:#?
assign ddest: REMOVE

lab FINE
