;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; InstallMSDE                                                  ; 
; -------------------------------------------------------------;
; Usage:                                                       ;
;  StrCpy $0 INSTANCE (e.g. StrCpy $0 "OTIS")                  ;
;  push $0                                                     ;
;  Call InstallMSDE                                            ;
;  pop $0                                                      ;
;  StrCmp $0 0 MSDEOK MSDENOTOK                                ;
;--------------------------------------------------------------;
; Will check installed instances of SQL and install MSDE with  ;
; the named instance if not already present. If instance IS    ;
; found it will move existing database files to a subdirectory ;
;  'backup' under the Data directory for the existing database ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Function InstallMSDE

  ;Get required instance name
  pop $0

  ;Get list of instances
  registry::Read "HKLM\Software\Microsoft\Microsoft SQL Server" "InstalledInstances" .R0 .R1 .R2 .R3
  
  ;We need to look at $R2, this holds the list of instances
  ;Build names of instances and compare them to the required instance
  
  StrLen $5 $R2                     # Get length of instance string so we know when to stop
  StrCpy $4 0                       #Counter
  StrCpy $3 "" 
  StrCpy $2 "" 
  
  Loop:
    StrCpy $3 $R2 1 $4              #Get next character of instance string
    StrCmp $3 "$\n" Check +1        #End of an instance marked by a line feed
     
    StrCpy $2 "$2$3"                #Not at the end of an instance, so add character to the
    IntOp $4 $4 + 1                 #string that we're building,increase the counter. If
    StrCmp $4 $5 EndOfList +1       #we've reached the end of the instance string then break
  Goto Loop                         #out, otherwise go round the loop again
    
  Check:                            #Found a line feed so compare the string that we've
  StrCmp $2 $0 FoundExisting +1     #built to the required instance name. If it doesn't
  IntOp $4 $4 + 1                   #match then increase the counter. If we've reached the end
  StrCmp $4 $5 EndOfList +1         #of the instance string then break out, otherwise clear the 
  StrCpy $2 ""                      #string we were building and go back round the loop
  Goto Loop   
                    
  EndOfList:                        #We've reached the end of the instance string 
  StrCmp $2 $0 FoundExisting +1     #One last check of the final string
  
  ;Instance not found, need to install MSDE
  ExecWait '"$ExePath\MSDE\Setup.exe" /qr+ SAPWD=0NL1N3T3CH INSTANCENAME=OTIS SECURITYMODE=SQL' $0
  SetRebootFlag true
  StrCmp $0 0 +4 +1
  StrCmp $0 8192 +3 +1  #8192 = reboot required - treat this as success as reboot will be forced anyway
  StrCpy $0 1
  push $0
  Goto End
  StrCpy $0 0
  push $0
  Goto End
  FoundExisting:                    #An instance of the same name already exists.  
  StrCpy $0 0
  push $0
  End:
  
FunctionEnd