/*----------------------------------------------------------------------------*\
 * SETUP.RUL
 *
 * InstallSHIELD SDK Edition Template Gepasi script.
 *
 * Intel hardware architectures
 *
 *     Comments:  This template script performs the installation of Gepasi 3
 *                to a Windows 95 or Windows NT platform.  The installation
 *                includes components: Application Program Files, Samples and
 *                Gnuplot, and Online Help Files.
\*----------------------------------------------------------------------------*/



// Size of components.
#define SIZE_REQ_SAMPLES        80000
#define SIZE_REQ_HELP		380000
#define SIZE_REQ_PROGRAM        1064000
#define SIZE_REQ_GNUPLOT        1024000

#define APP_NAME                "Gepasi"
#define PROGRAM_GROUP_NAME      "Gepasi 3"
#define APPBASE_PATH            "Gepasi\\"
#define COMPANY_NAME            "Enzyme Software"
#define PRODUCT_NAME            "Gepasi"
#define PRODUCT_VERSION         "3.21"
#define PRODUCT_KEY             "gepasi.exe"
#define UNINSTALL_KEY           "GepasiV3"
#define APPBASE_DIR             "Program Files"

#define STR_COMPLETE "by selecting the program icon in the Programs menu.\n\n"

declare

        // Global variable declarations.
        STRING  svGrp, szMsg, szFileSet, szTitle, szAppPath, szAppSharedDir;
        STRING  szProgram, szTemp, svUninstLogFile, szRegKey;
        STRING  svMainDirectory[ _MAX_STRING ];
        STRING  szProgramFolder, szItemName, szLinkDirectory, szCommandLine;
        BOOL    bSpaceOk, bReshowSetupType;
        NUMBER  nResult, nStatusId, nType, nvLastChar, nLen;

        BOOL    bIncludeProgram, bIncludeSamples, bIncludeHelp, bIncludeGnuplot;

        BOOL    bWinNT;

        // Function declarations.
        prototype SetupScreen();
        prototype CheckSpaceRequirements( number, number, number, number, string );
        prototype CreateRegDBEntries();
	STRING svFolderName;
program

StartHere:
        Disable( BACKGROUND );

        // Set up the installation screen.
        SetupScreen();

        // Set installation info., which is required for registry entries.
        InstallationInfo( COMPANY_NAME, PRODUCT_NAME, PRODUCT_VERSION, PRODUCT_KEY );

// Create a Welcome dialog.
WelcomeDlg:
        Disable( BACKBUTTON );
        Welcome( "Welcome to Gepasi setup", 0 );
        Enable( BACKBUTTON );

        // Ask user for a destination location for the installation.
GetTargetDirectory:

         svMainDirectory = TARGETDISK ^ APPBASE_DIR ^ APPBASE_PATH;
         szAppSharedDir = WINSYSDIR;

        szMsg = "";
        if ( AskDestPath( "Choose Destination Location", szMsg,
                          svMainDirectory, 0 ) = BACK ) then
           goto WelcomeDlg;
        endif;

		// make sure the directory path ends in a '\\'
		nLen = StrLength( svMainDirectory );
		GetByte( nvLastChar, svMainDirectory, nLen );
		if( nvLastChar != 92 ) then
		 svMainDirectory = svMainDirectory ^ "\\";
		endif;

        nType = TYPICAL;

        // Check to see if target system meets space requirements.
        bSpaceOk = CheckSpaceRequirements( bIncludeSamples,
                                           bIncludeProgram,
                                           bIncludeHelp,
                                           bIncludeGnuplot,
                                           svMainDirectory );

        // Ask user to try again if not enough space available.
        if (bSpaceOk = FALSE) then goto GetTargetDirectory; endif;

FolderSelection:
           svGrp = APP_NAME;
svFolderName = "";
svFolderName = APP_NAME;
           // Allow user to modify folder name.
           if ( SelectFolder( "Folder Selection", svGrp, svGrp ) = BACK ) then
              goto GetTargetDirectory;
           endif;

FileTransferSetup:

        // Prepare InstallSHIELD to record deinstallation information.
        DeinstallStart( svMainDirectory, svUninstLogFile, UNINSTALL_KEY, 0 );
        RegDBSetItem( REGDB_UNINSTALL_NAME, APP_NAME );

        // Set registry App Paths key information for the main application.
        szAppPath = svMainDirectory + ";" + szAppSharedDir;
        RegDBSetItem( REGDB_APPPATH, szAppPath );
        szProgram = svMainDirectory ^ "gepasi.exe";
        RegDBSetItem( REGDB_APPPATH_DEFAULT, szProgram );

        // Define the "General" file set.
        szFileSet = "General";
        TARGETDIR = svMainDirectory;

        FileSetBeginDefine( szFileSet );

          SetStatusWindow( -1, "Copying program files..." );

          // Always copy README & related files, located at
          // the root level in the DATA.Z library file.
          CompressGet( "data.z", "*.*", COMP_NORMAL );

             TARGETDIR = svMainDirectory;
             CompressGet( "data.z", "program\\*.*", INCLUDE_SUBDIR );
             TARGETDIR = szAppSharedDir;
             CompressGet( "data.z", "shared\\*.*", SHAREDFILE | COMP_UPDATE_VERSION );

             TARGETDIR = svMainDirectory ^ "Samples";
             CompressGet( "data.z", "samples\\*.*", INCLUDE_SUBDIR );

             TARGETDIR = svMainDirectory;
             CompressGet( "data.z", "help\\*.*", INCLUDE_SUBDIR );

             TARGETDIR = svMainDirectory;
             CompressGet( "data.z", "gnuplot\\*.*", INCLUDE_SUBDIR );

        FileSetEndDefine( szFileSet );

DoFileTransfer:
        // Set up progress indicator and information gauge.
        Enable( STATUSDLG );
        StatusUpdate( ON, 90 );

        // Perform the file set.
        nResult = FileSetPerformEz( szFileSet, 0 );

        switch (nResult)

        case FS_DONE: // Successful completion.

        case FS_CREATEDIR: // Create directory error.
             MessageBox( "Unable to create a directory under " + TARGETDIR + "."+
                         "Please check write access to this directory.", SEVERE );
             abort;

        default: // Group all other errors under default label.
             NumToStr( szTemp, nResult );
             MessageBox( "General file transfer error."+
                          "Please check your target location and try again."+
                          "\n\n Error Number:"+szTemp, SEVERE );

             abort;
        endswitch;

SetRegistryEntries:

        CreateRegDBEntries( );

        Delay(2);
        Disable( FEEDBACK_FULL );
        Disable( STATUSDLG );

        // Create program groups (folders) and icons.

CreateFolderIcons:
        SetStatusWindow( 95, "Creating Folder and Icons...." );

        TARGETDIR = svMainDirectory;

           CreateProgramFolder( svGrp );
           ShowProgramFolder( svGrp, SW_SHOW );
           LongPathToShortPath( svMainDirectory );
           Delay(1);

           szProgram = TARGETDIR ^ "gepasi.exe";
           LongPathToQuote( szProgram, TRUE );
           AddFolderIcon( svGrp, APP_NAME, szProgram,
                             TARGETDIR,
                             "", 0, "", REPLACE );
           Delay( 1 );
           szProgram = TARGETDIR ^ "LICENSE.TXT";
           LongPathToQuote( szProgram, TRUE );
           AddFolderIcon( svGrp, "License", szProgram, TARGETDIR,
                          "", 0, "", REPLACE );
           Delay( 1 );
           szProgram = TARGETDIR ^ "README.TXT";
           LongPathToQuote( szProgram, TRUE );
           AddFolderIcon( svGrp, "Readme", szProgram, TARGETDIR,
                          "", 0, "", REPLACE );
           Delay( 1 );
           szProgram = TARGETDIR ^ "GEP3FAQ.TXT";
           LongPathToQuote( szProgram, TRUE );
           AddFolderIcon( svGrp, "F.A.Q.", szProgram,
                          TARGETDIR, "", 0, "", REPLACE );
           Delay( 1 );

           szProgram = TARGETDIR ^ "wgnuplot.exe";
           LongPathToQuote( szProgram, TRUE );
           AddFolderIcon( svGrp, "Gnuplot", szProgram,
                             TARGETDIR, "", 0, "", REPLACE );
           Delay( 1 );

//        if ( bWinNT ) then
//           szProgram = WINDIR ^ "UNINST.EXE";
//           LongPathToShortPath( szProgram );
//           LongPathToShortPath( svUninstLogFile );
//           AddFolderIcon( svGrp, "Uninstall Gepasi 3",
//                          szProgram + " -f" + svUninstLogFile,
//                          WINDIR,
//                          "", 0, "", REPLACE );
//       endif;
//        Delay( 1 );

        // Announce setup complete and offer to read README file.

        SetStatusWindow( 100, "Installation complete." );

        // If shared files could not be installed, then users must restart system.
        if (BATCH_INSTALL = TRUE) then
           szMsg = "Some files could not be installed because they are "+
                   "currently in use by other programs in the system.  "+
                   "To allow for proper operation of the new program you should restart"+
                   "your system at this time.";
           CommitSharedFiles(0);
           RebootDialog( "Restart Windows", szMsg, SYS_BOOTMACHINE );
        else

           szMsg = "Setup is complete.  You may run the installed program ";
           szMsg = szMsg + STR_COMPLETE;
           MessageBeep( 0 );
           MessageBox( szMsg, INFORMATION );
        endif;

        exit;


/*---------------------------------------------------------------------------*\
 *
 * Function:  SetupScreen
 *
 *  Purpose:  This function will set up the screen look.  This includes
 *            colors, fonts, text to be displayed, etc.
 *
 *
 *    Input:
 *
 *  Returns:
 *
 * Comments:
\*---------------------------------------------------------------------------*/

function SetupScreen()
        begin

          Enable( INDVFILESTATUS );

          SetTitle( APP_NAME + " " + PRODUCT_VERSION + " Setup", 40, WHITE );

          SetTitle( "Setup", 0, BACKGROUNDCAPTION ); // Caption bar text.

          Enable( BACKGROUND );

        end;


/*---------------------------------------------------------------------------*\
 *
 * Function:  CheckSpaceRequirements
 *
 *  Purpose:  This function will check space requirements based on the
 *            elements being installed.
 *
 *    Input:
 *
 *  Returns:
 *
 * Comments:
\*---------------------------------------------------------------------------*/

function CheckSpaceRequirements( bIncludeSamples,
                                 bIncludeProgram,
                                 bIncludeHelp,
                                 bIncludeGnuplot,
                                 szDir )
          number  nSizeRequired;
        begin

          nSizeRequired =  SIZE_REQ_SAMPLES 
                         + SIZE_REQ_HELP
                         + SIZE_REQ_PROGRAM
                         + SIZE_REQ_GNUPLOT;

          // Check space on target drive.
          bSpaceOk = TRUE;
          if (GetDiskSpace( szDir ) < nSizeRequired) then
             szMsg = "There is not enough space available on the disk\n" +
                     "'" + svMainDirectory + "' \n" +
                     "Please free up some space or change the target location\n" +
                     "to a different disk";
             MessageBeep(0);
             MessageBox( szMsg, WARNING );
             bSpaceOk = FALSE;
          endif;

          return bSpaceOk;
        end;


/*---------------------------------------------------------------------------*\
 *
 * Function:  CreateRegDBEntries
 *
 *  Purpose:  This function will create necessary keys and values for
 *            the sample program.
 *
 *    Input:
 *
 *  Returns:
 *
 * Comments:
\*---------------------------------------------------------------------------*/

function CreateRegDBEntries()
        string szKey[255], szValue, szDemo, szProgram, szGnuPath, szSoundPath;
begin

        RegDBSetDefaultRoot( HKEY_CURRENT_USER );

        // Create PRODUCT_KEY key.
        szKey = "SOFTWARE\\" + COMPANY_NAME + "\\" + PRODUCT_NAME + "\\" + "Default";
        RegDBCreateKeyEx( szKey, "" );
        RegDBSetKeyValueEx( szKey, "Absolute tolerance", REGDB_STRING, "1e-012", -1 );
        RegDBSetKeyValueEx( szKey, "Adams max order", REGDB_STRING, "12", -1 );
        RegDBSetKeyValueEx( szKey, "BDF max order", REGDB_STRING, "5", -1 );
        RegDBSetKeyValueEx( szKey, "Column width", REGDB_STRING, "12", -1 );
        RegDBSetKeyValueEx( szKey, "Concentration unit", REGDB_STRING, "mM", -1 );
        RegDBSetKeyValueEx( szKey, "Concentrations", REGDB_STRING, "1e-005", -1 );
        RegDBSetKeyValueEx( szKey, "Derivation Factor", REGDB_STRING, "0.001", -1 );
        RegDBSetKeyValueEx( szKey, "Dynamics", REGDB_STRING, "1", -1 );
        RegDBSetKeyValueEx( szKey, "End time", REGDB_STRING, "5", -1 );
        RegDBSetKeyValueEx( szKey, "Kinetic constants", REGDB_STRING, "1", -1 );
        RegDBSetKeyValueEx( szKey, "MCA", REGDB_STRING, "1", -1 );
        RegDBSetKeyValueEx( szKey, "Newton limit", REGDB_STRING, "50", -1 );
        RegDBSetKeyValueEx( szKey, "Points", REGDB_STRING, "50", -1 );
        RegDBSetKeyValueEx( szKey, "Quotes", REGDB_STRING, "0", -1 );
        RegDBSetKeyValueEx( szKey, "Relative tolerance", REGDB_STRING, "1e-006", -1 );
        RegDBSetKeyValueEx( szKey, "Report", REGDB_STRING, "0", -1 );
        RegDBSetKeyValueEx( szKey, "Report Comments", REGDB_STRING, "1", -1 );
        RegDBSetKeyValueEx( szKey, "Report stability", REGDB_STRING, "0", -1 );
        RegDBSetKeyValueEx( szKey, "Report structural", REGDB_STRING, "0", -1 );
        RegDBSetKeyValueEx( szKey, "Separator", REGDB_STRING, "32", -1 );
        RegDBSetKeyValueEx( szKey, "Steady state", REGDB_STRING, "1", -1 );
        RegDBSetKeyValueEx( szKey, "Steady state resolution", REGDB_STRING, "1e-009", -1 );
        RegDBSetKeyValueEx( szKey, "Time unit", REGDB_STRING, "s", -1 );
        RegDBSetKeyValueEx( szKey, "Titles", REGDB_STRING, "0", -1 );
        RegDBSetKeyValueEx( szKey, "Unscaled MCA", REGDB_STRING, "0", -1 );
        RegDBSetKeyValueEx( szKey, "Volume unit", REGDB_STRING, "ml", -1 );

        szKey = "SOFTWARE\\" + COMPANY_NAME + "\\" + PRODUCT_NAME + "\\" + "Helpers";
        RegDBCreateKeyEx( szKey, "" );
        RegDBSetKeyValueEx( szKey, "Text Editor", REGDB_STRING, "notepad.exe", -1 );
	    szGnuPath = "";
        if (bIncludeGnuplot) then
		 szGnuPath = svMainDirectory + "wgnuplot.exe";
		endif;
        RegDBSetKeyValueEx( szKey, "Gnuplot", REGDB_STRING, szGnuPath, -1 );

        szKey = "SOFTWARE\\" + COMPANY_NAME + "\\" + PRODUCT_NAME + "\\" + "Preferences";
        RegDBCreateKeyEx( szKey, "" );
        RegDBSetKeyValueEx( szKey, "Load Report", REGDB_STRING, "1", -1 );
        RegDBSetKeyValueEx( szKey, "Sound Enabled", REGDB_STRING, "0", -1 );
	    szSoundPath = svMainDirectory + "finish.wav";
        RegDBSetKeyValueEx( szKey, "Stop Sound", REGDB_STRING, szSoundPath, -1 );

        // Create "MRU" key.
        szKey = "SOFTWARE\\" + COMPANY_NAME + "\\" + PRODUCT_NAME + "\\" + "Recent File List";
        RegDBCreateKeyEx( szKey, "" );
        szDemo = svMainDirectory ^ "Samples\\Brusselator.gps";
        RegDBSetKeyValueEx( szKey, "File1", REGDB_STRING, szDemo, -1 );
        szDemo = svMainDirectory ^ "Samples\\HMM.gps";
        RegDBSetKeyValueEx( szKey, "File2", REGDB_STRING, szDemo, -1 );
        szDemo = svMainDirectory ^ "Samples\\SEQFB.gps";
        RegDBSetKeyValueEx( szKey, "File3", REGDB_STRING, szDemo, -1 );
        szDemo = svMainDirectory ^ "Samples\\linear5.gps";
        RegDBSetKeyValueEx( szKey, "File4", REGDB_STRING, szDemo, -1 );

end;

