; Script for Opera Portable
; Website: www.kejut.com/operaportable
; 
; You may not distribute modified version of this file and claim as yours
; without permission from the author.
;
; Any comments and suggestions, please send to www.kejut.com/operaportable
; Thanks!


; function StrReplace
; by Hendri Adriaens

; HendriAdriaens@hotmail.com

function StrReplace
  Exch $0 ;this will replace wrong characters
  Exch
  Exch $1 ;needs to be replaced
  Exch
  Exch 2
  Exch $2 ;the orginal string
  Push $3 ;counter
  Push $4 ;temp character
  Push $5 ;temp string
  Push $6 ;length of string that need to be replaced
  Push $7 ;length of string that will replace
  Push $R0 ;tempstring
  Push $R1 ;tempstring
  Push $R2 ;tempstring
  StrCpy $3 "-1"
  StrCpy $5 ""
  StrLen $6 $1
  StrLen $7 $0
  Loop:
  IntOp $3 $3 + 1
  Loop_noinc:
  StrCpy $4 $2 $6 $3
  StrCmp $4 "" ExitLoop
  StrCmp $4 $1 Replace
  Goto Loop
  Replace:
  StrCpy $R0 $2 $3
  IntOp $R2 $3 + $6
  StrCpy $R1 $2 "" $R2
  StrCpy $2 $R0$0$R1
  IntOp $3 $3 + $7
  Goto Loop_noinc
  ExitLoop:
  StrCpy $0 $2
  Pop $R2
  Pop $R1
  Pop $R0
  Pop $7
  Pop $6
  Pop $5
  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch $0
FunctionEnd

!macro ReplaceInFile SOURCE_FILE SEARCH_TEXT REPLACEMENT
  Push "${SOURCE_FILE}"
  Push "${SEARCH_TEXT}"
  Push "${REPLACEMENT}"
  Call RIF
!macroend

Function RIF

  ClearErrors  ; wanna be a newborn

  Exch $0      ; REPLACEMENT
  Exch
  Exch $1      ; SEARCH_TEXT
  Exch 2
  Exch $2      ; SOURCE_FILE

  Push $R0     ; SOURCE_FILE file handle
  Push $R1     ; temporary file handle
  Push $R2     ; unique temporary file name
  Push $R3     ; a line to sar/save
  Push $R4     ; shift puffer

  IfFileExists $2 +1 RIF_error      ; knock-knock
  FileOpen $R0 $2 "r"               ; open the door

  GetTempFileName $R2               ; who's new?
  FileOpen $R1 $R2 "w"              ; the escape, please!

  RIF_loop:                         ; round'n'round we go
    FileRead $R0 $R3                ; read one line
    IfErrors RIF_leaveloop          ; enough is enough
    RIF_sar:                        ; sar - search and replace
      Push "$R3"                    ; (hair)stack
      Push "$1"                     ; needle
      Push "$0"                     ; blood
      Call StrReplace               ; do the bartwalk
      StrCpy $R4 "$R3"              ; remember previous state
      Pop $R3                       ; gimme s.th. back in return!
      StrCmp "$R3" "$R4" +1 RIF_sar ; loop, might change again!
    FileWrite $R1 "$R3"             ; save the newbie
  Goto RIF_loop                     ; gimme more

  RIF_leaveloop:                    ; over'n'out, Sir!
    FileClose $R1                   ; S'rry, Ma'am - clos'n now
    FileClose $R0                   ; me 2

    Delete "$2.old"                 ; go away, Sire
    Rename "$2" "$2.old"            ; step aside, Ma'am
    Rename "$R2" "$2"               ; hi, baby!

    ClearErrors                     ; now i AM a newborn
    Goto RIF_out                    ; out'n'away

  RIF_error:                        ; ups - s.th. went wrong...
    SetErrors                       ; ...so cry, boy!

  RIF_out:                          ; your wardrobe?
  Pop $R4
  Pop $R3
  Pop $R2
  Pop $R1
  Pop $R0
  Pop $2
  Pop $0
  Pop $1

FunctionEnd





;=======================

!define nd "hebat900"
!define VER "9.00"
!define EXEDIR "d:\operasi\cobaopera"
; !define EXEDIR "h:\_sh\inet"
; !define HOMEFILE "opera-portable-home"

SetCompressor /solid lzma
SetCompressorDictSize 16
; SetCompressorDictSize 32

Icon d:\itu\ikonopera.ico
!ifdef NTU
  !ifdef PERSONAL
    OutFile "${EXEDIR}\opera-portable-personal-ntu-${VER}.exe"
  !else
    OutFile "${EXEDIR}\opera-portable-oneuse-ntu-${VER}.exe"
  !endif
!else
  !ifdef PERSONAL
    OutFile "${EXEDIR}\opera-portable-personal-${VER}.exe"
  !else
    OutFile "${EXEDIR}\opera-portable-oneuse-${VER}.exe"
  !endif
!endif

InstallDir "$TEMP"
SilentInstall silent

Section "MainSection" SEC01
  
  GetTempFileName $0
  GetFileTime $0 $1 $2
  SetOutPath "$INSTDIR\${nd}-$1$2"
  Delete $0

  SetOverwrite on
  File /r "d:\${nd}\*"
  File "f:\aku\tumpuksemuafile.exe"
  !ifdef NTU
    File "c:\proxy.pac"
  !endif
SectionEnd

Section -Post
  !insertmacro ReplaceInFile "$OUTDIR\profile\opera6.ini" "d:\${nd}" "$OUTDIR"
  !insertmacro ReplaceInFile "$OUTDIR\operadef6.ini" "d:\${nd}" "$OUTDIR"
  !insertmacro ReplaceInFile "$OUTDIR\spellcheck.ini" "d:\${nd}" "$OUTDIR"
  !ifdef NTU
    WriteINIStr "$OUTDIR\profile\opera6.ini" "Proxy" "Use Automatic Proxy Configuration" "1"
    WriteINIStr "$OUTDIR\profile\opera6.ini" "Proxy" "Automatic Proxy Configuration URL" "$OUTDIR\proxy.pac"
  !endif
  
  !ifndef PERSONAL ; if NOT personal
    WriteINIStr "$OUTDIR\OperaDef6.ini" "User Prefs" "Enable Wand" "0"
  !endif

; cmdline[4] = opera profile dir
; cmdline[5] = this exe dir
  !ifdef PERSONAL
    ExecWait '"$OUTDIR\tumpuksemuafile.exe" "ekstrak" "$OUTDIR" "$EXEDIR"'
  !endif
  ExecWait '"$OUTDIR\opera.exe"'
  !ifdef PERSONAL
    ExecWait '"$OUTDIR\tumpuksemuafile.exe" "arsip,bakar" "$OUTDIR" "$EXEDIR"'
  !else
    ExecWait '"$OUTDIR\tumpuksemuafile.exe" "bakar" "$OUTDIR" "$EXEDIR"'
  !endif
  SetOutPath $TEMP
  RmDir /r "$OUTDIR"
SectionEnd
