; ==================================================
; ==================================================
;
; LIVE UPDATE v 0.2 (c) Yannick Erb 2009
; modified and integrated by Paolo Besser
; This script checks for the latest Icaros Update Version
; and propose to install it if it is more recent the current
; installation.
;;
; ==================================================
; ==================================================

; echo "This program will check available updates for Icaros Desktop system files"

; Local variable

; Download site (including username and password for ftp and path)
setenv LUPD_SITE "http://www.icarosdesktop.com/updates/"
; Version File
setenv LUPD_VERFILE "Latest_Version"
; Needed Version
setenv LUPD_NEEDEDFILE "Needed_Version"
; initialize archive
delete env:LUPD_ARCFILE >NIL:
; ISO Name
; setenv LUPD_ISONAME "aros-pc-i386.iso" --> DEPRECATED
; Temporary Directory
setenv LUPD_TMPDIR "Downloads:LUPD"
; Extracter
alias extracter extras:Misc/aminet/C/7zDec x


IF NOT EXISTS ${LUPD_TMPDIR}
  mkdir ${LUPD_TMPDIR}
EndIF
assign LUPD: ${LUPD_TMPDIR}
cd LUPD:

IF NOT EXISTS ENV:CURRENT_INSTALL
  setenv CURRENT_INSTALL SAVE "1.003000"
EndIF  

;look at the latest available version 
echo "wget -q " ${LUPD_SITE} NOLINE >LUPD:download_cmd
echo ${LUPD_VERFILE} >> LUPD:download_cmd
;echo "debug - command:" ${LUPD:download_cmd}
execute download_cmd
;delete download_cmd QUIET
copy ${LUPD_VERFILE} ENV:LATEST_UPDATE QUIET MOVE

IF ERROR 
RequestChoice >NIL: Title="Automatic Update Failure" Body="Icaros Desktop Live Updater couldn't retrieve needed informations*nand files from the Internet. Update will stop." Gadgets=OK
Skip END
EndIf
  
;look at the archive name of latest update
echo "wget -q " ${LUPD_SITE} NOLINE >LUPD:download_cmd
echo "Latest_Name" >>LUPD:download_cmd
execute download_cmd
delete download_cmd QUIET
copy Latest_Name env:LUPD_ARCHIVE QUIET MOVE

;look at the needed version (other versions can't be updated automatically) 
echo "wget -q " ${LUPD_SITE} NOLINE >LUPD:download_cmd
echo ${LUPD_NEEDEDFILE} >> LUPD:download_cmd
execute download_cmd
delete download_cmd QUIET
copy ${LUPD_NEEDEDFILE} ENV:NEEDED_VERSION QUIET MOVE 


IF ${CURRENT_INSTALL} ge ${LATEST_UPDATE}
  echo "*"Your System is up to date, Version is" ${CURRENT_INSTALL} "*"" >env:TEMP
  if ${Icaros/liveupmode} eq "false"
   RequestChoice >env:Chx Title="System message" Body=${TEMP} Gadgets=OK
  endif
  setenv LUPD_TODO "nothing"
  skip END
EndIF

IF not ${NEEDED_VERSION} eq "ANY"
  IF ${NEEDED_VERSION} gt ${CURRENT_INSTALL} 
    ;System can't be updated automatically
    echo "*"Update From " ${CURRENT_INSTALL} " to " ${LATEST_UPDATE} " isn't possible automatically **N You have to install manually the former updates **N using the process described in the user manual*"" > ENV:TEMP
    RequestChoice >env:Chx Title="Automatic Update Failure" Body=${TEMP} Gadgets=OK
    EndCLI
  EndIf
EndIF    

;System can be updated
echo "*"Update From " ${CURRENT_INSTALL} " to " ${LATEST_UPDATE} "?*"" > ENV:TEMP
RequestChoice >ENV:Choicex Title="An update is available" Body=${TEMP} Gadgets=UPDATE|CANCEL
IF ${Choicex} eq 0
  Skip END
EndIF
  
;echo "Downloading Update"        
echo "wget" ${LUPD_SITE} NOLINE >LUPD:download_cmd
echo ${LUPD_ARCHIVE} >> LUPD:download_cmd
execute download_cmd
delete download_cmd QUIET
echo "Update Downloaded"

echo "Extracting files..."
extracter ${LUPD_ARCHIVE}
echo "Files extracted"

; debug lines
; echo "SUMMARY"
; echo "version to be installed:" $LATEST_UPDATE
; echo "version needed to install:" $NEEDED_VERSION
; echo "archive name:" $LUPD_ARCHIVE
; echo "---------------------------------------"
; list $LUPD_TMPDIR
; echo "---------------------------------------"
; ask "enter to continue"

echo "Delete archive"
delete ${LUPD_ARCHIVE} QUIET
echo "Archive deleted"

; this part is now deprecated ==================================
;echo "Mount install ISO"
;IF EXISTS SYS:DiskImages/Unit0
;  assign CD0: dismount
;  assign CD0: remove
;  rename SYS:DiskImages/Unit0 SYS:DiskImages/Unit0.LUPDbckp QUIET
;    rem System can't be updated automatically
;    echo "*"Update From " ${CURRENT_INSTALL} " to " ${LATEST_UPDATE} " isn't possible automatically **N A virtual disk already mounted has been detected.*NYou have to install manually chooseing*Nthe ISO option instead. You will find the update ISO file in*Nthis drawer:" $LUPD_TMPDIR "*"" > ENV:TEMP
;    RequestChoice >env:Chx Title="Automatic Update Failure" Body=${TEMP} Gadgets=OK
;    Skip END
;EndIF
;Copy ${LUPD_ISONAME} TO SYS:DiskImages/Unit0 FOOVR QUIET
;mount CD0:
;echo "ISO mounted"
; ========================================================end==

;ask "enter to continue"

; create necessary assigns
ASSIGN sysource: lupd:UPDATE
ASSIGN sydest: SYS:
cd ${EXTRASPATH}//
cd >env:OTHDEST
ASSIGN othdest: "${OTHDEST}"
delete env:OTHDEST >NIL:
setenv LUPD_TODO "continue"
cd sys:tools/LUPD


Lab END

cd sys:Tools/LUPD
unsetenv LUPD_SITE
unsetenv LUPD_VERFILE
unsetenv LUPD_NEEDEDFILE
unsetenv LUPD_ARCHIVE
