
'*****************************************************************************
'   Copyright (c) 1994, Intergraph Corporation
'
'   File:  INGRGEN.MST
'
'   MS Test Full 'AUTO' - Installation Script
'       Application:  Generic "starting point"
'
'****************************************************************************

'$INCLUDE 'declares'

GLOBAL PRODUCT_NAME$
PRODUCT_NAME = "SMS"

GLOBAL SettingsFile$
GLOBAL NewSettingsFile$
GLOBAL SerialFile$

' INI switches, read from INI file
    GLOBAL defPath$
    GLOBAL defInstallation$
    GLOBAL defUser$
    GLOBAL defCompany$
    GLOBAL defSerial$
    GLOBAL Producer$
    Global ProductName$         ' Full app name
    GLOBAL Version$             ' App version number
    GLOBAL PName$               ' Short (9 char) app name
    GLOBAL OurCaption$           ' Title displayed in main setup window
    GLOBAL SetupModule$
    GLOBAL MIF$


'Script Variables -- customize for your product.  These are now read as part
'of the compiled .pcd file.

'CONST    Producer$       = ""
'CONST    ProductName$    = ""  Full App name
'CONST    Version$        = ""  App version number
'CONST    PName$          = ""  Short (9 char) App name
'CONST    OurCaption$     = ""  Title displayed in main setup window
'CONST    SetupModule$    = ""
'CONST    MIF$            = ""

DECLARE FUNCTION StartSetup()                   AS INTEGER
DECLARE FUNCTION CleanString(instring$)        As String
DECLARE SUB LogResult(msg AS STRING)
DECLARE SUB GetRegistration()
DECLARE SUB GetProductInfo()

' Standard script constants
CONST Yes$                  = "Yes"         ' Affirmative Action, INI
CONST Continue$             = "Continue"    ' Continue Button
CONST Cancel$               = "Cancel"      ' Cancel Button
CONST OK$                   = "OK"          ' OK button
CONST CancelSetup$          = "Cancel Setup"' Cancel Button
CONST Failure$              = "Failure"     'Simple failure message
CONST FailureUnknown$       = "Failure - Unknown Situation - Exitting"
CONST Success$              = "Success"     ' Script Success

GLOBAL sMessage(30) AS STRING  ' Error Definitions
    sMessage(0) = "System was out of memory, executable file was corrupt, or relocations were invalid."
    sMessage(1) = "Unknown Error - Not Defined - hInst = 1"
    sMessage(2) = "File was not found."
    sMessage(3) = "Path was not found."
    sMessage(4) = "Unknown Error - Not Defined - hInst = 4"
    sMessage(5) = "Attempt was made to dynamically link to a task, or there was a sharing or network-protection error."
    sMessage(6) = "Library required seperate data segments for each task."
    sMessage(7) = "Unknown Error - Not Defined - hInst = 7"
    sMessage(8) = "There was insufficient memory to start the application."
    sMessage(9) = "Unknown Error - Not Defined - hInst = 9"
    sMessage(10) = "Windows version was incorrect."
    sMessage(11) = "Exectable file was invalid.  Either it was not a Windows application or there was an error in the .EXE image."
    sMessage(12) = "Application was designed for a different operating system."
    sMessage(13) = "Application was designed for MS-DOS 4.0."
    sMessage(14) = "Type of executable file was unknown."
    sMessage(15) = "Attempt was made to load a real-mode application (developed for an earlier version of Windows)."
    sMessage(16) = "Attempt was made to load a second instance of an executable file containing multiple data segments that were not marked read-only"
    sMessage(17) = "Unknown Error - Not Defined - hInst = 17"
    sMessage(18) = "Unknown Error - Not Defined - hInst = 18"
    sMessage(19) = "Attempt was make to load a compressed executable file.  The file must be decompressed before it can be loaded."
    sMessage(20) = "Dynamic-link library (DLL) file was invalid.  One of the DLLs required to run this application was corrupt."
    sMessage(21) = "Application requires Microsoft Windows 32-bit extensions."


'   Function:       CleanString
'
'   Purpose:        Strip non-printing control characters out of a string
'
'   Parameters:     instring$   string to be cleaned
'
'   Return Value:   The cleaned string
'
STATIC FUNCTION CleanString( instring$) As String
    DIM part1$, part2$
    DIM x%, y%
    startover:
    y% = 0
    for x% = 1 to len(instring)
        if mid$(instring, x, 1) < " " then
            part1$ = mid$(instring, 1, x - 1)
            part2$ = mid$(instring, x + 1, len(instring))
            instring = part1$ + part2$
            y% = 1
        endif
    next x%

    if y% = 1 then
        goto startover
    endif
    CleanString = instring
END FUNCTION



'   Function:       StartSetup
'
'   Purpose:        Starts the product's Setup program, passing the
'                   settings file as an "/h:" argument
'
'   Parameters:     none
'
'   Return Value:   0       Setup program failed to start
'                   1       Setup program started successfully
'
STATIC FUNCTION StartSetup () AS INTEGER
    DIM hInst%
    DIM ssMessage$
    DIM iReturn%

    LogResult "Starting setup"               ' leave a trail
    hInst% = WinExec(SetupModule$ + " /h:" + NewSettingsFile$, SW_SHOW) ' start setup program
    IF hInst < 32 THEN                      ' in the error range
        IF hInst < 22 THEN                  ' in the known error range
            ssMessage$ = sMessage(hInst)    ' get error message
        ELSE                                ' dump unknown error message
            ssMessage$ =  "Unknown Error - Not Defined - hInst =" + Str$(hInst)
        ENDIF
        StartSetup% = 0                     ' set startsetup flag = FALSE
    ELSE
        StartSetup% = 1                     ' set startsetup flag = TRUE
    END IF
END FUNCTION



'   Function:       GetProductInfo()
'
'   Purpose:        Retrieve product information from settings file
'
'   Parameters:     None
'
'   Return Value:   None


STATIC SUB GetProductInfo()
    dim u%
    dim t$

    t$ = space$( 256 )
    u% = GetPrivateProfileString("SMS_Options", "Producer", "", t$, 255, SettingsFile$)
    Producer$ = cleanstring(t$)

    t$ = space$( 256 )
    u% = GetPrivateProfileString("SMS_Options", "ProductName", "", t$, 255, SettingsFile$)
    ProductName$ = cleanstring(t$)

    t$ = space$( 256 )
    u% = GetPrivateProfileString("SMS_Options", "Version", "", t$, 255, SettingsFile$)
    Version$ = cleanstring(t$)

    t$ = space$( 256 )
    u% = GetPrivateProfileString("SMS_Options", "PName", "", t$, 255, SettingsFile$)
    PName$ = cleanstring(t$)

    t$ = space$( 256 )
    u% = GetPrivateProfileString("SMS_Options", "OurCaption", "", t$, 255, SettingsFile$)
    OurCaption$ = cleanstring(t$)

    t$ = space$( 256 )
    u% = GetPrivateProfileString("SMS_Options", "SetupModule", "", t$, 255, SettingsFile$)
    SetupModule$ = cleanstring(t$)

    t$ = space$( 256 )
    u% = GetPrivateProfileString("SMS_Options", "MIF", "", t$, 255, SettingsFile$)
    MIF$ = cleanstring(t$)

END SUB



'   Function:       GetRegistration()
'
'   Purpose:        Retrieve User name, Company name, and serial number
'                   from initialization files
'
'   Parameters:     None
'
'   Return Value:   None
'
STATIC SUB GetRegistration()
    dim u%
    dim iReturn%
    dim t$
    DIM WinDrive$
    DIM SysDrive$

    WinDrive$ = space$(256)
    iReturn% = GetWindowsDirectory(WinDrive$, 255) ' get directory
    SysDrive$ = mid$(WinDrive$, 1, 3)

    ' To avoid colliding with other users who may be installing the same product
    ' simultaneously, and problems that might occur with shares that are "read-only",
    ' make a 'private' copy of the settings file for our own use.
    '
    NewSettingsFile$ = SysDrive$ + Command$
    COPY SettingsFile$ TO NewSettingsFile$

    '   Look in SMS.INI for this stuff first
    '
    t$ = space$(256)
    u% = GetPrivateProfileString("Local", "UserName", "", t$, 255, SysDrive$ + PRODUCT_NAME + ".INI")
    defUser$ = cleanstring(t$)
    t$ = space$(256)
    u% = GetPrivateProfileString("Local", "CompanyName", "", t$, 255 , SysDrive$ + PRODUCT_NAME + ".INI")
    defCompany$ = cleanstring(t$)
    IF defUser$ <> "" THEN
        '
        ' This is tricky -- if we found any "actual" user and company information in
        ' SMS.INI, we're going to put it *into* our "private" settings file.  This way,
        ' each user's copy will be "individualized"
        '
        u%=WritePrivateProfileString("Registration", "name", defUser$, NewSettingsFile$)
        u%=WritePrivateProfileString("Registration", "organization", defCompany$, NewSettingsFile$)
    ENDIF

    '
    ' And to let each user have his/her own serial number, you can create a SERIAL.INF
    ' file that lists by product, the serial number each user should get.
    '
    IF EXISTS(SerialFile$) THEN
        t$ = space$(64)
        u% = GetPrivateProfileString(PName$, defUser$, "", t$, 64, SerialFile$)
        defSerial$ = cleanstring(t$)
        IF defSerial$ <> "" THEN
            u% = WritePrivateProfileString("Registration", "serial", defSerial$, NewSettingsFile$)
        ENDIF
    ENDIF
END SUB



'   Function:       LogResult
'
'   Purpose:        Writes a MIF (Management Information Format) file that summarizes
'                   the result of the installation (success/failure) so the information
'                   can be passed back to the System Management Server
'
'    Parameters:    msg     Message string to be logged in MIF file
'
'    Return Value:  none
'
STATIC SUB LogResult(msg AS STRING)
    DIM iReturn AS INTEGER
    DIM sFileName AS STRING
    DIM hMIFHandle AS INTEGER

    'Create the MIF file.
    '====================

    sFileName$ = STRING$(256, 32)       ' create buffer for directory
    iReturn% = GetWindowsDirectory(sFileName$, 255) ' get directory
    sFileName$ = sFileName$ + MIF$      ' append name to directory

    hMIFHandle = FREEFILE               ' get freefile handle
    OPEN sFileName FOR OUTPUT AS #hMIFHandle ' open mif

    'Write the ComponetID group.
    '===========================        ' start dumping information to mif

    PRINT #hMIFHandle, "START COMPONENT"
    PRINT #hMIFHandle, , "NAME = " ; CHR$(34) ; "WORKSTATION" ; CHR$(34)
    PRINT #hMIFHandle, , "START GROUP"
    PRINT #hMIFHandle, , , "NAME = " ; CHR$(34) ; "ComponentID" ; CHR$(34)
    PRINT #hMIFHandle, , , "ID = 1"
    PRINT #hMIFHandle, , , "CLASS = " ; CHR$(34) ; "DMTF|ComponentID|1.0" ; CHR$(34)
    PRINT #hMIFHandle, , , "START ATTRIBUTE"
    PRINT #hMIFHandle, , , , "NAME = " ; CHR$(34) ; "Manufacturer" ; CHR$(34)
    PRINT #hMIFHandle, , , , "ID = 1"
    PRINT #hMIFHandle, , , , "ACCESS = READ-ONLY"
    PRINT #hMIFHandle, , , , "STORAGE = SPECIFIC"
    PRINT #hMIFHandle, , , , "TYPE = STRING(64)"
    PRINT #hMIFHandle, , , , "VALUE =" ; CHR$(34) ; Producer ; CHR$(34)
    PRINT #hMIFHandle, , , "END ATTRIBUTE"
    PRINT #hMIFHandle, , , "START ATTRIBUTE"
    PRINT #hMIFHandle, , , , "NAME = " ; CHR$(34) ; "Product" ; CHR$(34)
    PRINT #hMIFHandle, , , , "ID = 2"
    PRINT #hMIFHandle, , , , "ACCESS = READ-ONLY"
    PRINT #hMIFHandle, , , , "STORAGE = SPECIFIC"
    PRINT #hMIFHandle, , , , "TYPE = STRING(64)"
    PRINT #hMIFHandle, , , , "VALUE =" ; CHR$(34) ; ProductName ; CHR$(34)
    PRINT #hMIFHandle, , , "END ATTRIBUTE"
    PRINT #hMIFHandle, , , "START ATTRIBUTE"
    PRINT #hMIFHandle, , , , "NAME = " ; CHR$(34) ; "Version" ; CHR$(34)
    PRINT #hMIFHandle, , , , "ID = 3"
    PRINT #hMIFHandle, , , , "ACCESS = READ-ONLY"
    PRINT #hMIFHandle, , , , "STORAGE = SPECIFIC"
    PRINT #hMIFHandle, , , , "TYPE = STRING(64)"
    PRINT #hMIFHandle, , , , "VALUE =" ; CHR$(34) ; Version ; CHR$(34)
    PRINT #hMIFHandle, , , "END ATTRIBUTE"
    PRINT #hMIFHandle, , , "START ATTRIBUTE"
    PRINT #hMIFHandle, , , , "NAME = " ; CHR$(34) ; "Serial Number" ; CHR$(34)
    PRINT #hMIFHandle, , , , "ID = 4"
    PRINT #hMIFHandle, , , , "ACCESS = READ-ONLY"
    PRINT #hMIFHandle, , , , "STORAGE = SPECIFIC"
    PRINT #hMIFHandle, , , , "TYPE = STRING(64)"
    PRINT #hMIFHandle, , , , "VALUE =" ; CHR$(34) ; defUser ; " at " ; defCompany ; ", #"; defSerial; CHR$(34)
    PRINT #hMIFHandle, , , "END ATTRIBUTE"
    PRINT #hMIFHandle, , , "START ATTRIBUTE"
    PRINT #hMIFHandle, , , , "NAME = " ; CHR$(34) ; "Installation" ; CHR$(34)
    PRINT #hMIFHandle, , , , "ID = 5"
    PRINT #hMIFHandle, , , , "ACCESS = READ-ONLY"
    PRINT #hMIFHandle, , , , "STORAGE = SPECIFIC"
    PRINT #hMIFHandle, , , , "TYPE = STRING(64)"
    PRINT #hMIFHandle, , , , "VALUE =" ; CHR$(34) ; DATETIME$ ; CHR$(34)
    PRINT #hMIFHandle, , , "END ATTRIBUTE"
    PRINT #hMIFHandle, , "END GROUP"
    PRINT #hMIFHandle, , "START GROUP"
    PRINT #hMIFHandle, , , "NAME = " ; CHR$(34) ; "InstallStatus" ; CHR$(34)
    PRINT #hMIFHandle, , , "ID = 2"
    PRINT #hMIFHandle, , , "CLASS = " ; CHR$(34) ; "MICROSOFT|JOBSTATUS|1.0" ; CHR$(34)
    PRINT #hMIFHandle, , , "START ATTRIBUTE"
    PRINT #hMIFHandle, , , , "NAME = " ; CHR$(34) ; "Status" ; CHR$(34)
    PRINT #hMIFHandle, , , , "ID = 1"
    PRINT #hMIFHandle, , , , "ACCESS = READ-ONLY"
    PRINT #hMIFHandle, , , , "STORAGE = SPECIFIC"
    PRINT #hMIFHandle, , , , "TYPE = STRING(32)"
    IF Msg$ = "Success" Then
        PRINT #hMIFHandle, , , , "VALUE =" ; CHR$(34) ; "Success" ; CHR$(34)
    ELSE
        PRINT #hMIFHandle, , , , "VALUE =" ; CHR$(34) ; "Failed" ; CHR$(34)
    ENDIF
    PRINT #hMIFHandle, , , "END ATTRIBUTE"
    PRINT #hMIFHandle, , , "START ATTRIBUTE"
    PRINT #hMIFHandle, , , , "NAME = " ; CHR$(34) ; "Description" ; CHR$(34)
    PRINT #hMIFHandle, , , , "ID = 2"
    PRINT #hMIFHandle, , , , "ACCESS = READ-ONLY"
    PRINT #hMIFHandle, , , , "STORAGE = SPECIFIC"
    PRINT #hMIFHandle, , , , "TYPE = STRING(64)"
    PRINT #hMIFHandle, , , , "VALUE =" ; CHR$(34) ; Msg$ ; CHR$(34)
    PRINT #hMIFHandle, , , "END ATTRIBUTE"
    PRINT #hMIFHandle, , "END GROUP"
    PRINT #hMIFHandle, "END COMPONENT"

    CLOSE hMIFHandle

END SUB




DIM iReturn%        ' return value for this script

' START THE SCRIPT HERE
SettingsFile$ = CURDIR$ + "\" + command$
SerialFile$ = CURDIR$ + "\SERIAL.INF"

GetProductInfo          ' get product specific parameters
GetRegistration         ' get the users name and company

IF EXISTS("C:\results.log") <> 0 THEN
    COPY "C:\results.log" TO "C:\results.sav"
    KILL "C:\results.log"
ENDIF

IF StartSetup() THEN    ' if setup process started successfully
    DIM hResFile%
    DIM hResString$
    DIM cpActive%       ' Controls the looping action through the dialogs

    cpActive% = 1       ' control process active, TRUE(1) = continue


    ' We keep looking for the Setup window caption as evidence that
    ' the program is still running.  When that window goes away,
    ' this loop terminates.

    SLEEP 15

    WHILE cpActive
        IF WFndWnd(OurCaption$, FW_DEFAULT) = 0 THEN
            cpActive = 0
        ENDIF
        SLEEP 5
    WEND

    ' Examine result stored in C:\RESULTS.LOG

    IF EXISTS ("C:\results.log") THEN
        hResFile% = FREEFILE
        OPEN "C:\results.log" FOR INPUT AS #hResFile%
        stillGoing% = 1
        WHILE EOF(hResFile%) <> -1 AND stillGoing <> 0
            LINE INPUT #hResFile, hResString$
            IF INSTR(hResString$, PName$) <> 0 THEN
                stillGoing% = 0
                IF INSTR(hResString$, "succeeded") <> 0 THEN
                    LogResult "Success"
                ENDIF
                IF INSTR(hResString$, "FAILED") <> 0 THEN
                    LogResult "Failed"
                ENDIF
            ENDIF
        WEND
        CLOSE #hResFile%
    ELSE
        LogResult "Failed"
    END IF
END IF


END ' Ends main program

