VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form frmMain 
   Caption         =   "Toolbar Brander"
   ClientHeight    =   1560
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   5850
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   204
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   1560
   ScaleWidth      =   5850
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdExit 
      Caption         =   "Exit"
      Height          =   375
      Left            =   4920
      TabIndex        =   6
      Top             =   1080
      Width           =   855
   End
   Begin VB.CommandButton cmdMakeBrandedTS 
      Caption         =   "Make Branded Toolbar Setup"
      Height          =   375
      Left            =   2280
      TabIndex        =   5
      Top             =   1080
      Width           =   2535
   End
   Begin VB.CommandButton cmdWebBrowser 
      Caption         =   "..."
      Height          =   315
      Left            =   5400
      TabIndex        =   2
      Top             =   120
      Width           =   375
   End
   Begin MSComDlg.CommonDialog dlgCommon 
      Left            =   7680
      Top             =   120
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.TextBox txtBrandId 
      Height          =   315
      Left            =   2280
      TabIndex        =   3
      Top             =   600
      Width           =   3015
   End
   Begin VB.TextBox txtFileName 
      Height          =   315
      Left            =   2280
      TabIndex        =   1
      Top             =   120
      Width           =   3015
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      Caption         =   "Brand Id:"
      Height          =   195
      Left            =   120
      TabIndex        =   4
      Top             =   600
      Width           =   675
   End
   Begin VB.Label Label1 
      Caption         =   "Toolbar Template File Name:"
      Height          =   255
      Left            =   120
      TabIndex        =   0
      Top             =   160
      Width           =   2055
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub cmdWebBrowser_Click()
    On Error GoTo ErrorHandler
    dlgCommon.DialogTitle = "Select Toolbar template for branding"
    dlgCommon.Filter = "Toolbar Template Files|*.dat"
    dlgCommon.DefaultExt = "dat"
    dlgCommon.CancelError = True
    dlgCommon.Flags = cdlOFNFileMustExist
    
    dlgCommon.ShowOpen
    txtFileName = dlgCommon.FileName
    Exit Sub

ErrorHandler:
    If Err.Number <> cdlCancel Then
        MsgBox Err.Description, vbExclamation
    End If
End Sub

Private Sub cmdMakeBrandedTS_Click()
    On Error GoTo ERROR_HANDLER
    
    Dim BeginPos As Long
    Dim varTemp As Variant
    Dim pb As New PropertyBag
    Dim a As String
    Dim temp As String
    Dim i As Long
    Dim strExeName As String
    Dim strToolbarName As String
    Dim byteArr() As Byte
    Dim strFileName As String
            
    Dim bUpload As Boolean
    bUpload = False

    ' 0. Get result exe file name
    dlgCommon.DialogTitle = "Save Toolbar Setup File"
    dlgCommon.FileName = "toolbar"
    dlgCommon.InitDir = "c:\"
    dlgCommon.Filter = "Executable Files|*.exe"
    dlgCommon.DefaultExt = "exe"
    dlgCommon.Flags = cdlOFNOverwritePrompt
    dlgCommon.ShowSave
    strFileName = dlgCommon.FileName
    

    ' 1. Load toolbar template file and "Brand It"
    Open txtFileName.Text For Binary As #1
    Get #1, , varTemp
    Close #1
    byteArr = varTemp
    If UBound(byteArr) > LBound(byteArr) Then
        pb.Contents = byteArr
    End If
    Set varTemp = Nothing
    pb.WriteProperty "BrandId", txtBrandId.Text
        
        ' 1. Create copy of template dll
    FileCopy App.Path & "\IEToolbarTemplate.dll", App.Path & "\IEToolbar.dll"
    
    ' 2. Save data to dll.
    Open App.Path & "\IEToolbar.dll" For Binary As #1
    BeginPos = LOF(1)
    
    varTemp = pb.Contents
    Seek #1, LOF(1)
    Put #1, , varTemp
    Put #1, , BeginPos
    Close #1
    Set varTemp = Nothing
   
    '3. Delete old setup
    On Error Resume Next
    Kill App.Path & "\setup.exe"
    Kill App.Path & "\IEToolbar.iss"
    Kill strFileName
    On Error GoTo 0
    
    ' 4. Make .iss file
    temp = pb.ReadProperty("Name")
    If temp = "" Then temp = "IE Toolbar Generated In IE Toolbar Editor"
    Open App.Path & "\IEToolbarTemplate.iss" For Binary As #1
    a = StrConv(InputB(LOF(1), 1), vbUnicode)
    Close #1
    
    ' Replace templates in setup file
    strToolbarName = temp
    a = Replace(a, "%%%TOOLBAR_NAME%%%", temp)
    a = Replace(a, "%%%SEARCHTEXT%%%", "[ABARSEARCH]")
    
    If g_iMode = 1 Then
        a = Replace(a, "%%%SEARCHURL%%%", ABARSEARCHURL)
    ElseIf g_iMode = 2 Or g_iMode = 3 Then
        a = Replace(a, "%%%SEARCHURL%%%", "")
    ElseIf g_iMode = 4 Then
        a = Replace(a, "%%%SEARCHURL%%%", pb.ReadProperty("AddressBarSearch"))
    End If

    ' Replace toolbar setup name
    i = InStrRev(strFileName, "\")
    If i > 0 Then
        temp = Mid(strFileName, i + 1)
        i = InStr(temp, ".")
        If i > 0 Then
            temp = Left(temp, i - 1)
        End If
        strExeName = temp
    End If
    
    a = Replace(a, "%%%EXENAME%%%", temp)

    Open App.Path & "\IEToolbar.iss" For Binary As #1
    Put #1, , a
    Close #1
    
    Set pb = Nothing
    
    ' 5. Call Inno Setup compiler.
    Dim res As Long
    Dim sinfo As STARTUPINFO
    Dim pinfo As PROCESS_INFORMATION
    Dim strCmdLine As String
    sinfo.cb = Len(sinfo)
    sinfo.lpReserved = vbNullString
    sinfo.lpDesktop = vbNullString
    sinfo.lpTitle = vbNullString
    sinfo.wShowWindow = 0               ' 0 = SW_HIDE
    sinfo.dwFlags = 1                   '1 = STARTF_USESHOWWINDOW
    
    Load frmWait
    frmWait.lblStatus = "Please, wait, while Toolbar setup program will be generated..."
    frmWait.Show vbModeless, Me
    strCmdLine = """" & App.Path & "\InnoSetup\ISCC.exe"" """ & App.Path & "\IEToolbar.iss"""
    res = CreateProcessBynum(vbNullString, strCmdLine, 0, 0, True, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, sinfo, pinfo)
    If res Then
        Do
            res = WaitForSingleObject(pinfo.hProcess, 0)
            If res = WAIT_OBJECT_0 Then
                Exit Do
            End If
            DoEvents
        Loop While True
        CloseHandle pinfo.hThread
        CloseHandle pinfo.hProcess
        frmWait.Hide
        
        ' Copy files
        CopyFiles App.Path & "\" & strExeName & ".exe", strFileName
        'frmResult.lblFileName = strFileName
        frmResult.lblLink.Tag = strFileName
        frmResult.lblToolbarName = """" & strToolbarName & """"
        frmResult.lblNNN = Replace(frmResult.lblNNN, "%%%TOOLBARNAME%%%", strToolbarName)
        MessageBeep MB_ICONASTERISK
        frmResult.Show vbModal, Me
    Else
        frmWait.Hide
        MsgBox "Can't start Inno Setup Complier!", vbExclamation
    End If
    
    Exit Sub
ERROR_HANDLER:
    If Err.Number <> cdlCancel Then
        MsgBox Err.Description, vbExclamation
    End If
End Sub

