; Script generated by the HM NIS Edit Script Wizard.

; HM NIS Edit Wizard helper defines
; NE1 Defines
!define PRODUCT_NAME "NanoEngineer-1 Suite"
!define PRODUCT_VERSION "1.1.1"
;!define PRODUCT_NICKNAME "(Rattlesnake)"
!define PRODUCT_PUBLISHER "Nanorex, Inc."
!define PRODUCT_WEB_SITE "http://www.nanoengineer-1.com"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_NAME}\${PRODUCT_VERSION}"
;!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\babel.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define NE1_INST_DIR "NanoEngineer-1 v${PRODUCT_VERSION}"

;QMX Defines
!define PRODUCT_QMX_VERSION "0.5.1"
!define PRODUCT_QMX_NAME "QuteMolX"
!define PRODUCT_QMX_PUBLISHER "Nanorex, Inc"
!define PRODUCT_QMX_WEB_SITE "http://www.nanoengineer-1.com/QuteMolX/"
!define PRODUCT_QMX_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_QMX_NAME}\${PRODUCT_QMX_VERSION}"
!define PRODUCT_QMX_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_QMX_NAME}"
!define PRODUCT_QMX_UNINST_ROOT_KEY "HKLM"
!define QMX_INST_DIR "${PRODUCT_QMX_NAME} ${PRODUCT_QMX_VERSION}"

;GMX Defines
!define PRODUCT_GMX_VERSION "3.3.3"
!define PRODUCT_GMX_PATCH_VERSION "p2"
!define PRODUCT_GMX_NAME "GROMACS"
!define PRODUCT_GMX_PUBLISHER "Nanorex, Inc"
!define PRODUCT_GMX_WEB_SITE "http://www.nanorex.com"
!define PRODUCT_GMX_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_NAME}\${PRODUCT_GMX_VERSION}"
!define PRODUCT_GMX_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_GMX_UNINST_ROOT_KEY "HKLM"
!define GMX_INST_DIR "GROMACS_${PRODUCT_GMX_VERSION}"

;NV1 Defines
!define PRODUCT_NV1_VERSION "0.1.0"
!define PRODUCT_NV1_NAME "NanoVision-1 ${PRODUCT_NV1_VERSION}"
!define PRODUCT_NV1_PUBLISHER "Nanorex, Inc"
!define PRODUCT_NV1_WEB_SITE "http://www.nanorex.com"
!define PRODUCT_NV1_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\nv1.exe"
!define PRODUCT_NV1_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_NV1_UNINST_ROOT_KEY "HKLM"
!define NV1_INST_DIR "${PRODUCT_NV1_NAME} ${PRODUCT_NV1_VERSION}"

!macro ReplaceInFile SOURCE_FILE SEARCH_TEXT REPLACEMENT
   Push "${SOURCE_FILE}"
   Push "${SEARCH_TEXT}"
   Push "${REPLACEMENT}"
   Call RIF
!macroend

; MUI 1.67 compatible ------
!include "MUI.nsh"
!include "registerExtension.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "install.ico"
!define MUI_UNICON "uninstall.ico"
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "install-header.bmp"
!define MUI_HEADERIMAGE_UNBITMAP "install-header.bmp"
!define MUI_WELCOMEFINISHPAGE_BITMAP "wizard-sidebar.bmp"
!define MUI_UNWELCOMEFINISHPAGE_BITMAP "wizard-sidebar.bmp"

InstType "Basic"
InstType "Full"

; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!insertmacro MUI_PAGE_LICENSE "..\..\..\cad\src\dist\Licenses\NanoEngineer-1_License.txt"
; Components page
!insertmacro MUI_PAGE_COMPONENTS
; Directory page
!insertmacro MUI_PAGE_DIRECTORY
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!define MUI_FINISHPAGE_RUN "$INSTDIR\${NE1_INST_DIR}\program\main.exe"
!define MUI_FINISHPAGE_SHOWREADME "$INSTDIR\ReadMe.html"
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

; MUI end ------

Name "${PRODUCT_NAME} Version v${PRODUCT_VERSION}"
OutFile "..\..\..\cad\src\build\NanoEngineer-1_Suite_v${PRODUCT_VERSION}.exe"
InstallDir "$PROGRAMFILES\Nanorex"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
;InstallDirRegKey HKLM "${PRODUCT_QMX_DIR_REGKEY}" ""
;InstallDirRegKey HKLM "${PRODUCT_GMX_DIR_REGKEY}" ""
;InstallDirRegKey HKLM "${PRODUCT_NV1_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show

SectionGroup /e "NanoEngineer-1 Suite Installer"

SectionGroup /e "NanoEngineer-1"
Section "NanoEngineer-1 Program" SECNE1
  SectionIn 1 2
  SetOutPath "$INSTDIR\${NE1_INST_DIR}"
  SetOverwrite try
  File "..\..\..\cad\src\dist\ReadMe.html"
  SetOutPath "$INSTDIR"
  File "..\Readme.html"
  SetOverwrite try
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\bin"
  File /r "..\..\..\cad\src\dist\bin\*"
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\doc"
  File /r "..\..\..\cad\src\dist\doc\*"
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\Licenses"
  File /r "..\..\..\cad\src\dist\Licenses\*"
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\partlib"
  File /r "..\..\..\cad\src\dist\partlib\*"
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\plugins"
  File /r "..\..\..\cad\src\dist\plugins\*"
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\program"
  File /r "..\..\..\cad\src\dist\program\*"
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\src"
  File /r "..\..\..\cad\src\dist\src\*"
  
  ; hack
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\program"
  File "C:\Qt\4.3.5\bin\QtSvg4.dll"
  
  SetOutPath "$SYSDIR"
  SetOverwrite off
  File "..\..\Win32\glut32.dll"
  File "..\..\Win32\gle32.dll"
  CreateDirectory "$SMPROGRAMS\Nanorex\${NE1_INST_DIR}"
  CreateShortCut "$SMPROGRAMS\Nanorex\${NE1_INST_DIR}\NanoEngineer-1.lnk" "$INSTDIR\${NE1_INST_DIR}\program\main.exe"
  CreateShortCut "$DESKTOP\NanoEngineer-1.lnk" "$INSTDIR\${NE1_INST_DIR}\program\main.exe"
  CreateShortCut "$SMPROGRAMS\${NE1_INST_DIR}\ReadMe.html.lnk" "$INSTDIR\${NE1_INST_DIR}\ReadMe.html"
  SetOutPath "$INSTDIR"
  SetOutPath "$INSTDIR\Pref_Mod"
  File /r "..\..\Pref_mod\dist\*"
  SetOutPath "$INSTDIR"
SectionEnd
Section /o "Source" SEC_NE1_SRC
  SectionIn 2
  SetOutPath "$INSTDIR\${NE1_INST_DIR}\source"
  SetOverwrite try
  File /r "..\..\..\cad\src\dist\source\*"
  SetOutPath "$INSTDIR"
SectionEnd
SectionGroupEnd

SectionGroup /e "QuteMolX"
Section "QuteMolX (Required)" SEC_QMX_BASE
  SectionIn 1 2
  SetOutPath "$INSTDIR\${QMX_INST_DIR}"
  SetOverwrite try
  File "\QMX_Install\License.txt"
  File "\QMX_Install\ReadMe.html"
  File "\QMX_Install\build\*"
  SetOutPath "$INSTDIR\${QMX_INST_DIR}\image"
  File "\QMX_INSTALL\build\image\*"
  SetOutPath "$INSTDIR\${QMX_INST_DIR}\presets"
  File "\QMX_Install\build\presets\*"
  SetOutPath "$INSTDIR\${QMX_INST_DIR}"
  CreateDirectory "$SMPROGRAMS\Nanorex\${QMX_INST_DIR}"
  CreateShortCut "$SMPROGRAMS\Nanorex\${QMX_INST_DIR}\QuteMolX.lnk" "$INSTDIR\${QMX_INST_DIR}\QuteMolX.exe"
  CreateShortCut "$DESKTOP\QuteMolX.lnk" "$INSTDIR\${QMX_INST_DIR}\QuteMolX.exe"
  ExecWait '"$INSTDIR\Pref_Mod\pref_modifier.exe" -k qutemol_enabled -v True'
  ExecWait '"$INSTDIR\Pref_Mod\pref_modifier.exe" -k qutemol_path -v "$INSTDIR\${QMX_INST_DIR}\QuteMolX.exe"'
  SetOutPath "$INSTDIR"
SectionEnd
Section /o "Source" SEC_QMX_SRC
  SectionIn 2
  SetOutPath "$INSTDIR\${QMX_INST_DIR}\source"
  SetOverwrite try
  File /r "\QMX_Install\source\*"
  SetOutPath "$INSTDIR"
SectionEnd
SectionGroupEnd

;SectionGroup /e "NanoVision-1"
;Section "NanoVision-1 Base" NV1_BASE
;  SetOutPath "$INSTDIR\${NV1_INST_DIR}\bin"
;  SetOverwrite try
;  File "\NV1_Install\bin\*"
;  CreateDirectory "$SMPROGRAMS\Nanorex\${NV1_INST_DIR}"
;  CreateShortCut "$SMPROGRAMS\Nanorex\${NV1_INST_DIR}\NanoVision-1.lnk" "$INSTDIR\${NV1_INST_DIR}\bin\nv1.exe"
;  CreateShortCut "$DESKTOP\NanoVision-1.lnk" "$INSTDIR\${NV1_INST_DIR}\bin\nv1.exe"
;  SetOutPath "$INSTDIR\${NV1_INST_DIR}\include"
;  File /r "\NV1_Install\include\*"
;  SetOutPath "$INSTDIR\${NV1_INST_DIR}\lib"
;  File /r "\NV1_Install\lib\*"
;  SetOutPath "$INSTDIR\${NV1_INST_DIR}"
;  SetOverwrite  on
;  File "\NV1_Install\ReadMe.html"
;  File "\NV1_Install\License.txt"

;  SetOutPath "$APPDATA\Nanorex\NanoVision-1"
;  File "\NV1_Install\NanoVision-1.ini"
;  Push "$INSTDIR\${NV1_INST_DIR}\lib"
;  Push "\"
;  Push "/"
;  Call StrRep
;  Pop "$R0" ;result
;  !insertmacro ReplaceInFile "$APPDATA\Nanorex\NanoVision-1\NanoVision-1.ini" "@PLUGSRCHPATH@" "$R0"

;  SetOutPath "$APPDATA\Nanorex\NanoVision-1"
;  File "\NV1_Install\NanoVision-1.ini"
;  Push "$INSTDIR\NanoVision-1\lib"
;  Push "\"
;  Push "/"
;  Call StrRep
;  Pop "$R0" ;result
;  !insertmacro ReplaceInFile "$APPDATA\Nanorex\NanoVision-1.ini" "@PLUGSRCHPATH@" "$R0"

;  ExecWait '"$INSTDIR\Pref_Mod\pref_modifier.exe -k nv1_enabled -v True"'
;  ExecWait '"$INSTDIR\Pref_Mod\pref_modifier.exe -k nv1_path -v $INSTDIR\${NV1_INST_PATH}\nv1.exe"
;  SetOutPath "$INSTDIR"
;SectionEnd

;Section /o "source" NV1_SRC
;  SetOutPath "$INSTDIR\${NV1_INST_DIR}\source"
;  File /r "\NV1_Install\src\*"
;SectionEnd
;SectionGroupEnd

SectionGroup /e "GROMACS"
Section "GROMACS (Required)" SEC_GMX_BASE
  SectionIn 1 2
;  SetOutPath "$INSTDIR"
  SetOutPath "c:\${GMX_INST_DIR}"
  SetOverwrite try
  File "\GMX_Install\ReadMe.html"
  File "\GMX_Install\.\License.txt"
  SetOutPath "c:\${GMX_INST_DIR}\bin"
  SetOverwrite try
  File "\GMX_Install\dist\bin\*"
  ExecWait '"$INSTDIR\Pref_Mod\pref_modifier.exe" -k gromacs_enabled -v True"'
  ExecWait '"$INSTDIR\Pref_Mod\pref_modifier.exe" -k gromacs_path -v "c:\${GMX_INST_DIR}\bin\mdrun.exe"'
SectionEnd
Section /o "Headers & Libraries" SEC_GMX_LIBHDR
  SectionIn 2
  SetOutPath "c:\${GMX_INST_DIR}\include\gromacs"
  File "\GMX_Install\dist\include\gromacs\*"
  SetOutPath "c:\${GMX_INST_DIR}\include\gromacs\types"
  File "\GMX_Install\dist\include\gromacs\types\*"
  SetOutPath "c:\${GMX_INST_DIR}\include\gromacs"
  File "\GMX_Install\dist\include\gromacs\*"
  SetOutPath "c:\${GMX_INST_DIR}\lib"
  File "\GMX_Install\dist\lib\*"
SectionEnd
Section "Documentation" SEC_GMX_DOC
  SectionIn 1 2
  SetOverwrite try
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\html"
  File "\GMX_Install\dist\share\gromacs\html\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\html\images"
  File "\GMX_Install\dist\share\gromacs\html\images\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\html\online"
  File "\GMX_Install\dist\share\gromacs\html\online\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\template"
  File "\GMX_Install\dist\share\gromacs\template\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\top"
  File "\GMX_Install\dist\share\gromacs\top\*"
SectionEnd
Section "Tutorials" SEC_GMX_TUTOR
  SectionIn 1 2
  SetOverwrite try
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\tutor"
  File "\GMX_Install\dist\share\gromacs\tutor\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\tutor\gmxdemo"
  File "\GMX_Install\dist\share\gromacs\tutor\gmxdemo\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\tutor\methanol"
  File "\GMX_Install\dist\share\gromacs\tutor\methanol\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\tutor\mixed"
  File "\GMX_Install\dist\share\gromacs\tutor\mixed\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\tutor\nmr1"
  File "\GMX_Install\dist\share\gromacs\tutor\nmr1\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\tutor\nmr2"
  File "\GMX_Install\dist\share\gromacs\tutor\nmr2\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\tutor\speptide"
  File "\GMX_Install\dist\share\gromacs\tutor\speptide\*"
  SetOutPath "c:\${GMX_INST_DIR}\share\gromacs\tutor\water"
  File "\GMX_Install\dist\share\gromacs\tutor\water\*"
SectionEnd
Section /o "Source" SEC_GMX_SRC
  SectionIn 2
  SetOverwrite try
  SetOutPath "c:\${GMX_INST_DIR}\source"
  file /r "\GMX_Install\dist\src\gromacs-3.3.3\*"
SectionEnd
SectionGroupEnd

SectionGroup /e "MCPP"
Section "MCPP (Required)" SEC_MCPP
  SectionIn 1 2
  SetOutPath "c:\${GMX_INST_DIR}\MCPP\bin"
  File "\GMX_Install\mcpp\bin\mcpp.exe"
  SetOutPath "c:\${GMX_INST_DIR}\MCPP"
  File "\GMX_Install\mcpp\LICENSE"
  File "\GMX_Install\mcpp\mcpp-manual-jp.html"
  File "\GMX_Install\mcpp\mcpp-manual.html"
  File "\GMX_Install\mcpp\NEWS"
  File "\GMX_Install\mcpp\README"
  ExecWait '"$INSTDIR\Pref_Mod\pref_modifier.exe" -k cpp_enabled -v True'
  ExecWait '"$INSTDIR\Pref_Mod\pref_modifier.exe" -k cpp_path -v "c:\${GMX_INST_DIR}\mcpp\bin\mcpp.exe"'
SectionEnd
Section /o "Source" SEC_MCPP_SRC
  SectionIn 2
  SetOutPath "c:\${GMX_INST_DIR}\mcpp\source"
  File /r "\GMX_Install\mcpp\source\*"
SectionEnd
SectionGroupEnd

SectionGroupEnd

Section -AdditionalIcons
  SetOutPath $INSTDIR
  CreateShortCut "$SMPROGRAMS\Nanorex\NanoEngineer-1 v${PRODUCT_VERSION}\partlib.lnk" "$INSTDIR\NanoEngineer-1 v${PRODUCT_VERSION}\partlib"
  CreateShortCut "$SMPROGRAMS\Nanorex\NanoEngineer-1 v${PRODUCT_VERSION}\Licenses.lnk" "$INSTDIR\NanoEngineer-1 v${PRODUCT_VERSION}\Licenses"
  CreateShortCut "$SMPROGRAMS\Nanorex\NanoEngineer-1 v${PRODUCT_VERSION}\Uninstall.lnk" "$INSTDIR\uninst.exe"
;  WriteIniStr "$INSTDIR\NanoVision-1\${PRODUCT_NV1_NAME}-${PRODUCT_NV1_VERSION.url" "InternetShortcut" "URL" "${PRODUCT_NV1_WEB_SITE}"
;  CreateShortCut "$SMPROGRAMS\Nanorex\NanoVision-1\Website.lnk" "$INSTDIR\NanoVision-1\${PRODUCT_NV1_NAME}.url"
  WriteIniStr "$INSTDIR\${QMX_INST_DIR}\${PRODUCT_QMX_NAME}-${PRODUCT_QMX_VERSION}.url" "InternetShortcut" "URL" "${PRODUCT_QMX_WEB_SITE}"
  CreateShortCut "$SMPROGRAMS\Nanorex\${QMX_INST_DIR}\Website.lnk" "$INSTDIR\${QMX_INST_DIR}\${PRODUCT_QMX_NAME}-${PRODUCT_QMX_VERSION}.url"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\program\main.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\${NE1_INST_DIR}\program\main.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
  ${registerExtension} "$INSTDIR\${NE1_INST_DIR}\program\main.exe " ".mmp" "NanoEngineer-1 File"
SectionEnd


Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
  Delete "$INSTDIR\*"
  RMDir /r "$INSTDIR\src"
;  RMDir /r "$INSTDIR\program"
;  RMDir /r "$INSTDIR\plugins"
;  RMDir /r "$INSTDIR\partlib"
;  RMDir /r "$INSTDIR\Licenses"
;  RMDir /r "$INSTDIR\doc"
;  RMDir /r "$INSTDIR\bin"
  RMDIR /r "$INSTDIR\${NE1_INST_DIR}"
  RMDIR /r "$INSTDIR\${QMX_INST_DIR}"
  RMDIR /r "$INSTDIR\${GMX_INST_DIR}"
;  RMDIR /r "$INSTDIR\${NV1_INST_DIR}"
  RMDir /r "$INSTDIR"
  RMDir /r "C:\${GMX_INST_DIR}"

  Delete "$DESKTOP\NanoEngineer-1.lnk"
  Delete "$DESKTOP\NanoVision-1.lnk"
  Delete "$DESKTOP\QuteMolX.lnk"

  RMDir /r "$SMPROGRAMS\Nanorex\${NE1_INST_DIR}"
  RMDir /r "$SMPROGRAMS\Nanorex\${QMX_INST_DIR}"
;  RMDir /r "$SMPROGRAMS\Nanorex\${NV1_INST_DIR}"
  RMDir /r "$SMPROGRAMS\Nanorex"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  ${unregisterExtension} ".mmp" "NanoEngineer-1 File"
  SetAutoClose true

SectionEnd

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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!
    Delete "$2.old"                 ; Nano-Hive addition

    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

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
  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
  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

Function StrRep
  Exch $R4 ; $R4 = Replacement String
  Exch
  Exch $R3 ; $R3 = String to replace (needle)
  Exch 2
  Exch $R1 ; $R1 = String to do replacement in (haystack)
  Push $R2 ; Replaced haystack
  Push $R5 ; Len (needle)
  Push $R6 ; len (haystack)
  Push $R7 ; Scratch reg
  StrCpy $R2 ""
  StrLen $R5 $R3
  StrLen $R6 $R1
loop:
  StrCpy $R7 $R1 $R5
  StrCmp $R7 $R3 found
  StrCpy $R7 $R1 1 ; - optimization can be removed if U know len needle=1
  StrCpy $R2 "$R2$R7"
  StrCpy $R1 $R1 $R6 1
  StrCmp $R1 "" done loop
found:
  StrCpy $R2 "$R2$R4"
  StrCpy $R1 $R1 $R6 $R5
  StrCmp $R1 "" done loop
done:
  StrCpy $R3 $R2
  Pop $R7
  Pop $R6
  Pop $R5
  Pop $R2
  Pop $R1
  Pop $R4
  Exch $R3
FunctionEnd