; INTEGRATE
;
; This script basically allows to create a new starting
; script for Amiga disks under AROS. It uses quite the
; same basics of ADD_PROGRAM, so use it for further
; references
; -----------------------------

; CLEANUP
delete env:AMIGANAMEINFO >NIL:
delete env:DISKNAME > NIL:
delete env:AMIGAPATH >NIL:
delete env:Choice >NIL:

; adding preparatory task.

; we have to work in the current dir
cd extras:AmiBridge/AmigaApps

; asks for an amiga application and stores its path in $AMIDISK
IF NOT EXISTS env:AMIDISK
requestfile >env:AMIDISK TITLE="Choose an Amiga Disk" PATTERN="#?.adf"
  IF WARN
     delete env:AMIDISK
     EndCli
  EndIF
EndIF
echo noline ${AMIDISK} >env:TEMP
join CHR34 env:TEMP CHR34 AS env:COMMAAMIDISK

; well, now we need to know the file name only and we'll
; store it in a env variable called $DISKNAME. This
; will help us later with creating the proper files.
; purges the app name from path, and stores it in $DISKNAME
list ${AMIDISK} lformat "%s" files >ENV:DISKNAME
echo noline ${DISKNAME} >env:TEMP
join CHR34 env:TEMP CHR34 AS env:COMMADISKNAME

; debug
;echo File we are working on
;echo $AMIDISK
;echo $DISKNAME
;echo $COMMADISKNAME

; now we have to store in some nice variables the final name
; that the uaerc file will have. This is obviously DISKNAME.urc
; we noh have to add the .urc extension.

; creates a name for uaerc file in $DISKURC
echo NOLINE ${DISKNAME} >env:DISKNAME2
echo NOLINE ".urc" >>env:INFO_FILE
join env:DISKNAME2 env:INFO_FILE AS env:DISKURC
delete env:DISKNAME2 >NIL:
delete env:INFO_FILE >NIL:
echo noline ${DISKURC} >env:TEMP
join CHR34 env:TEMP CHR34 AS env:COMMADISKURC

; debug
;echo $DISKURC
;echo $COMMADISKURC
;echo executing test if $diskurc exists

; NEW QUESTION!
; Does $COMMADISKURC exists in AmigaApps? If yes, run it. Else, continue.
IF EXISTS ${COMMADISKURC}
; echo ${COMMADISKURC} " already exists, normally running uae"
 echo "true" >env:runnow
 skip finaltask
EndIF

; debug
;echo asking name for diskscript

; now we have to assign a name to our game or demo disk. Thank you
; for the requeststring command I needed so hard!!!
; we will store the launch script name in $DISKSCRIPT
RequestString >env:USERTEMP TITLE="Enter game name" TEXT="AmiBridge needs to know the name of the game*Nor the demo you need to integrate into Icaros Desktop"
; IF WARN
;  delete env:AMIDISK >nil:
;  delete env:DISKNAME >nil:
;  delete env:DISKURC >nil:
;  delete env:DISKICON >nil:
;  endcli
; EndIf
echo noline ${USERTEMP} >env:TEMP
join CHR34 env:TEMP CHR34 AS env:DISKSCRIPT

; debug
echo ${DISKSCRIPT}
echo asking for amiga model

IF NOT EXISTS extras:AmiBridge/System/S/startup-sequence
 RequestChoice >env:MODEL "Choose a classic Amiga model" "Please choose the Commodore Amiga model which best fits*nwith the chosen ADF files" "AROS A500|AROS A1200"
Else
  RequestChoice >env:MODEL "Choose a classic Amiga model" "Please choose the Commodore Amiga model which best fits*nwith the chosen ADF files" "AROS A500|Amiga A500|Expanded A500|Amiga A1200|AROS A1200"
EndIf

; IF WARN
;  delete env:AMIDISK >nil:
;  delete env:DISKNAME >nil:
;  delete env:DISKURC >nil:
;  delete env:DISKICON >nil:
;  delete env:DISKSCRIPT >nil:
;  endcli
; EndIf

; debug
echo copying proper config file

IF ${MODEL} eq 1
 copy extras:amibridge/emulator/arosa500.urc ${COMMADISKURC}
EndIf
IF ${MODEL} eq 2
 copy extras:amibridge/emulator/a500.urc ${COMMADISKURC}
EndIf
IF ${MODEL} eq 3
 copy extras:amibridge/emulator/a500plus.urc ${COMMADISKURC}
EndIf
IF ${MODEL} eq 4
 copy extras:amibridge/emulator/a1200.urc ${COMMADISKURC}
EndIf
IF ${MODEL} eq 0
 copy extras:amibridge/emulator/arosa1200.urc ${COMMADISKURC}
EndIf

copy extras:amibridge/emulator/disklaunch env:scripttemp1

; debug
echo creating launch script for ADF

; finally create the launch script for the disk
; echo "run extras:amibridge/emulator/janus-uae -G -f" ${COMMADISKURC} >>env:scripttemp
; note: -G temporarily removed!!!

echo noline "run extras:amibridge/emulator/janus-uae -f " >env:tempstring1
echo noline ${DISKURC} >env:tempstring2
join env:tempstring1 CHR34 env:tempstring2 CHR34 AS env:tempstring
join env:scripttemp1 env:tempstring AS env:scripttemp
copy env:scripttemp ${DISKSCRIPT}

; and copies the icon
; creates a name for script icon file in $DISKICON
echo NOLINE ${USERTEMP} >env:DISKNAME2
echo NOLINE ".info" >>env:INFO_FILE
join env:DISKNAME2 env:INFO_FILE AS env:DISKICON
echo noline ${DISKICON} >env:TEMP
join CHR34 env:TEMP CHR34 AS env:COMMADISKICON
delete env:DISKNAME2 >NIL:
delete env:INFO_FILE >NIL:
copy ADD_ADF-GAME.info TO ${COMMADISKICON}

; replace script file default value with $AMIDISK
lua:lua replacer.lua ${COMMADISKURC} "flop0" ${COMMAAMIDISK}

;debug
echo "adding the first adf disk went fine. Now adding more"

; ==== OTHER DISKS STUFF =====
; most games came in groups of 2 or more disks. AmiBrige
; support multiple disk games using up to 4 disk.

RequestChoice >env:choice Title="Multiple disks" Body="Amiga games and demos usually came on more disks.*NAmiBridge allow mounting up to 4 disks in emulated external*Ndevices. Please select other floppies' ADF files, or click*NCancel to stop adding parts." GADGETS="Add other discs|No more discs"

if ${choice} eq 0
 skip finaltask
endif

execute amibridge.addisk
if ${AMI2DISK} eq "---nodsk---"
 skip finaltask
endif
lua:lua replacer.lua ${COMMADISKURC} "flop1" ${COMMAAMIDISK}

execute amibridge.addisk
if ${AMI2DISK} eq "---nodsk---"
 skip finaltask
endif
lua:lua replacer.lua ${COMMADISKURC} "flop2" ${COMMAAMIDISK}

execute amibridge.addisk
if ${AMI2DISK} eq "---nodsk---"
 skip finaltask
endif
lua:lua replacer.lua ${COMMADISKURC} "flop3" ${COMMAAMIDISK}

LAB finaltask

; debug
echo executing finaltask

if ${runnow} eq "true"
 delete env:runnow >NIL:
 stack 512000
 run extras:amibridge/emulator/janus-uae -G -f ${COMMADISKURC} >NIL:
endcli
endif

requestchoice >env:choice title="Operation complete" body="A configuration file has been created in extras:/AmiBridge/AmigaApps.*NThis file includes all informations needed to work with the specified Amiga model.*NYou may need to fine tune them. Use Janus-UAE to play or to edit configuration." gadgets=RUN|Cancel

if ${choice} eq 1
 stack 512000
 run extras:amibridge/emulator/janus-uae -G -f ${COMMADISKURC} >NIL:
endif

; clean env from unnecessary files
delete env:AMIDISK >NIL:
delete env:COMMAAMIDISK >NIL:
delete env:DISKDIR >NIL:
delete env:DISKICON >NIL:
delete env:COMMADISKNAME >NIL:
delete env:DISKURC >NIL:
delete env:USERTEMP >NIL:
delete env:TEMP >NIL:
delete env:COMMADISKURC >NIL:
if exists env:AMI2SDISK
 delete env:AMI2DISk >NIL:
endif

endcli
