/* Copyright (c) Oracle Corporation 1992.  All Rights Reserved */
/*****************************************************************************
  NAME
    rsf3271.ins - V3 installation script for Windows Required
                Support Files 7.1 (32 bit)

  DESCRIPTION
    This script installs Windows Required Suport Files 7.0.

  MODIFIED    DD-MMM-YY  Reason 
    LMurphy   01-DEC-94  modified for 32 bit  
    echien    17-NOV-94  version 7.1.4.0.4          
    echien    09-NOV-94  version 7.1.4.0.3
    peiyu     05-OCT-94  version 7.1.4.0.2
    peiyu     19-SEP-94  version 7.1.4.0.1
    peiyu     24-Aug-94  rsf70->rsf71
    Colello   20-APR-94  make 3.0.10.1 compliant
    Colello   06-DEC-93  catch errors around v6translate of PATH
    akelley   21-OCT-93  Toolkit, Help, Multimedia removed
    ming      23-SEP-93  DLL version checking added
    akelley   31-AUG-93  NLS added
    akelley   02-AUG-93  set_rdbms error handlers added
    akelley   28-MAY-93  modified to match OS/2 scripts.
    akelley   27-APR-93  OH105 added
    akelley   27-APR-93  MM105 added
    akelley   09-MAR-93  TK20 added
    gcostanz  22-FEB-93  Added instantiation to install messages
    akelley   26-OCT-92  Added RDBMS70.
    handrus   16-SEP-92  Fixed.
    ekabatch  07-MAR-92  Normalized.
    ekabatch  28-JAN-92  Created.
*****************************************************************************/
{
  /**************************************************************************
      Start main logic...
   **************************************************************************/

  add_bin_to_path = false;

  if (doit)
    {
      ins_ratchet = "7.1.4.1.0";

      execute("%installer_home%\windows.ins");

      permit_retry_operations = TRUE;

      install(winrsf71);

      ui_action(instantiate(installing_scripts));
      copy(deinstl);

      ui_action(instantiate(installing_dlls));
      copy(dll);

      /**********************************************************************
         DLL version checking...
      ***********************************************************************/

      {
        registration_comment = "";
        old_dlls_txt = ""; /* initialize to "" to indicate not modified yet */

        no_version_version = "0.0.0.0.0.0.0";
        reinstall_or_downgrade = (version_status == 'reinstall) ||
                                 (version_status == 'downgrade);

        /********************************************************************
           Get the path and try to get rid of white space...
         ********************************************************************/

        {
          path = v6_translate("%%PATH%%");
          path = replace(path,"; ",";");
          path = replace(path," ;",";");
          path = replace(path,"%tab_character%;",";");
          path = replace(path,";%tab_character%",";");
        } [ 'OS_ERROR: path = "";
            'UNBOUND_ENVIRONMENT_VARIABLE: path = ""; ]

        /********************************************************************
           Separate the path into its components.  Add %boot_drive%:\WINDOWS,
           %boot_drive%:\WINDOWS\SYSTEM, and %oracle_home%\BIN
         ********************************************************************/

        old_exploded_path = explode(path,";");
        subtract(old_exploded_path,list(".","..")); /* don't count . and .. */

        oracle_home_bin = "%oracle_home%\BIN";

        /* Note:  Don't change the order below!!!! */
        exploded_path = list(oracle_home_bin,windows_directory(),
                             windows_system_directory());
        union(exploded_path,old_exploded_path);

        /* loop for each member of target_list */
        while (not(empty(target_list)))
          {
            target = first(target_list);
            target_list = rest(target_list);

            target_base = first(explode(target,"."));
            bin_dll = "%oracle_home%\BIN\%target%"; /* .DLL (DLL currently in oracle home) */
            new_dll = "%oracle_home%\BIN\%target_base%.NEW"; /*    .NEW (DLL from installation media) */

            ui_action(instantiate(searching_path));

            /****************************************************************
               Get the version of new_dll...
             ****************************************************************/

            if (not((earlier_version(installer_version,"3.0.9.5.0"))))
              {
                {
                  version_no = windows_file_version(new_dll);
                } [ 'OS_ERROR: version_no = no_version_version; ]
              }
            else
              version_no = no_version_version;

            unsorted_dll_list = list(list(version_no,new_dll));

            ui_action(sorting_dlls);

            /****************************************************************
               Get the versions of all DLL's...
             ****************************************************************/

            dll_path = exploded_path;         /* don't modify exploded_path */
            while (not(empty(dll_path)))
              {
                path_member = first(dll_path);
                dll_path = rest(dll_path);

                if (file_name(path_member)==".")
                  continue();
                full_target = replace("%path_member%\%target%","\\","\");
                ui_action(instantiate(searching_file));
                if (exists(full_target))
                  {
                    if (not((earlier_version(installer_version,"3.0.9.5.0"))))
                      {
                        {
                          version_no = windows_file_version(full_target);
                        } [ 'OS_ERROR: version_no = no_version_version; ]
                      }
                    else
                      version_no = no_version_version;
                    unsorted_dll_list = cons(list(version_no,full_target),
                                             unsorted_dll_list);
                  }
              } [ 'INVALID_FILE_NAME,'PERMISSION_DENIED,'OS_ERROR: continue(); ]

            /****************************************************************
               Sort list of DLLs by version number.  dll_list will contain
               list of DLL_path with newest version at head of list.
               version_list will be in same order but will give list of form
               "DLL_path DLL_version", with special format for currently
               installed version, and version from installation media.  This
               has to be a STABLE sort!
             ****************************************************************/

            bin_str = "";            /* necessary b/c bin_dll may not exist */

            dll_list = list();
            version_list = list();
            done = false;
            while (not(done))
              {

                /************************************************************
                   Get the oldest version of the dll...
                 ************************************************************/

                oldest = first(unsorted_dll_list);
                oldest_version_no = first(oldest);

                tmp_list = rest(unsorted_dll_list);
                while ((oldest_version_no != no_version_version) &&
                       not(empty(tmp_list)))
                  {
                    current = first(tmp_list);
                    tmp_list = rest(tmp_list);
                    current_version_no = first(current);
                    if (earlier_version(current_version_no,oldest_version_no))
                      {
                        oldest = current;
                        oldest_version_no = current_version_no;
                      }
                  }

                /************************************************************
                   Remove the oldest version from unsorted_dll_list and cons
                   it to dll_list and version_list
                 ************************************************************/

                extract(unsorted_dll_list,oldest);
                oldest_dll = first(rest(oldest));
                dll_list = cons(oldest_dll,dll_list);

                /* The spaces in " %no_version_version%" and "
                   %no_version_info%" are important.  Don't change them. */
                if (oldest_dll == bin_dll)
                  {
                    bin_str = replace(instantiate(bin_format),
                                      " %no_version_version%",
                                      " %no_version_info%");
                    str = bin_str;
                  }
                else if (oldest_dll==new_dll)
                  {
                    new_str = replace(instantiate(new_format),
                                      " %no_version_version%",
                                      " %no_version_info%");
                    str = new_str;
                  }
                else
                  str = replace(instantiate(default_format),
                                " %no_version_version%",
                                " %no_version_info%");

                if (empty(unsorted_dll_list))
                  {
                    done = TRUE;
                    newest_str = str;
                    newest_dll = oldest_dll;
                    newest_version_no = oldest_version_no;
                  }

                version_list = cons(str,version_list);
              }

            /****************************************************************
               Decide which dll to install...
             ****************************************************************/
            {
              preferred = new_dll;              /* assume preferred is .NEW */

              if (newest_dll == new_dll)
                signal('DO_MOVE,"");

              if ((newest_dll == bin_dll) &&
                  (first(rest(dll_list)) == new_dll)) /* if .DLL > .NEW >= ... */ {
                    if (reinstall_or_downgrade)
                      signal('DO_MOVE,"");

                    if (not(yesno_dialog(instantiate(overwrite_message),
                                         false,overwrite_content,
                                         instantiate(overwrite_help))))
                      preferred = bin_dll;

                    signal('DO_MOVE,"");
                  }
              else
                {
                  if (reinstall_or_downgrade)
                    {
                      preferred = new_dll;
                      preferred_str = new_str;
                      message = prefer_new_message;
                    }
                  else
                    {
                      preferred = newest_dll;
                      preferred_str = newest_str;
                      if (newest_dll==bin_dll)
                        message = prefer_installed_message;
                      else
                        message = prefer_newest_message;
                    }

                  mark
                    {
                      if (not(yesno_dialog(instantiate(message),true,
                                           prefer_content,
                                           instantiate(prefer_help))))
                        {
                          mark
                            pick = single_selection_dialog(instantiate(choose_dll_message),
                                                           version_list,
                                                           preferred_str,
                                                           choose_dll_content,
                                                           instantiate(choose_dll_help));
                          if (pick==new_str)
                            preferred = new_dll;
                          else if (pick==bin_str)
                            preferred = bin_dll;
                          else
                            preferred = first(explode(pick));
                        }
                    }
                  signal('DO_MOVE,"");
                }
            } [ 'DO_MOVE: continue(); ]

            /****************************************************************
               Install the preferred version and delete new_dll...
             ****************************************************************/

            if (preferred != bin_dll)
              {
                copy_file(preferred,bin_dll);
              } [ 'PERMISSION_DENIED:
                   {
                     error = permission_denied;
                     information_dialog(instantiate(cannot_copy));
                     signal('CANNOT_COPY,installation_aborted);
                   }
                  'OS_ERROR:
                   {
                     error = os_error;
                     information_dialog(instantiate(cannot_copy));
                     signal('CANNOT_COPY,installation_aborted);
                   }
                  'WRITE_ERROR:
                   {
                     error = write_error;
                     information_dialog(instantiate(cannot_copy));
                     signal('CANNOT_COPY,installation_aborted);
                 }
               ]
            {
              remove_file(new_dll);
            } [ 'PERMISSION_DENIED,'OS_ERROR,'WRITE_ERROR: continue(); ]

            /****************************************************************
               Note the version number of the installed version...
             ****************************************************************/

            if (not((earlier_version(installer_version,"3.0.9.5.0"))))
              {
                {
                  final_version_no = windows_file_version(bin_dll);
                  final_version_no = " %final_version_no%";
                } [ 'OS_ERROR: final_version_no = ""; ]
              }
            else
              final_version_no = "";

            if (preferred!=new_dll)
              if (final_version_no=="")
                registration_comment = "%registration_comment%%target% %no_version_info%; ";
              else
                registration_comment = "%registration_comment%%target%%final_version_no%; ";

            extract(dll_list,bin_dll);
            extract(dll_list,new_dll);

            /****************************************************************
               If there are extra dlls in the system, ask to rename them...
             ****************************************************************/

            if (not(empty(dll_list)))
              {
                if (yesno_dialog(instantiate(auto_rename),TRUE,
                                 auto_rename_content,
                                 instantiate(auto_rename_help)))
                  { /* if ok to rename ... */
                    add_bin_to_path = true; /* since we are removing a DLL from their DLL search path, we need to do this! */
                    while (not(empty(dll_list)))
                      {
                        old_dll = first(dll_list);
                        dll_list = rest(dll_list);
                        old_dll_dir = directory_name(old_dll);
                        target_old = replace("%old_dll_dir%\%target_base%.OLD", "\\", "\");
                        move_file(old_dll,target_old); /* rename */
                        if (exists(old_dll))
                          signal('PERMISSION_DENIED,"");
                      } [ 'PERMISSION_DENIED:
                         {
                           error = permission_denied;
                           information_dialog(instantiate(cannot_rename));
                           signal('CANNOT_RENAME,installation_aborted);
                         }
                         'OS_ERROR:
                         {
                           error = os_error;
                           information_dialog(instantiate(cannot_rename));
                           signal('CANNOT_RENAME,installation_aborted);
                         }
                         'WRITE_ERROR:
                         {
                           error = write_error;
                           information_dialog(instantiate(cannot_rename));
                           signal('CANNOT_RENAME,installation_aborted);
                         } ]
                  }
                else
                  {                  /* if not ok to rename ... */
                    ui_action(instantiate(writing_dlls_txt));
                    if (old_dlls_txt=="")
                      { /* if file not started */
                        old_dlls_txt = "%oracle_home%\rsf_dlls.txt"; /* if not, delete any previous */
                        remove_file(old_dlls_txt); /* versions of this file */
                        modify("",instantiate(head_message),old_dlls_txt);
                      }
                    modify("","",old_dlls_txt);
                    modify("","-------------------------------------------------------------",
                           old_dlls_txt);
                    modify("",instantiate(header_message),old_dlls_txt);
                    modify("","",old_dlls_txt);

                    extract(version_list,new_str);     /* don't include these in the list */
                    extract(version_list,bin_str);

                    while (not(empty(version_list)))
                      {
                        current = first(version_list);
                        version_list = rest(version_list);
                        modify("",current,old_dlls_txt);
                      }
                    modify("","-------------------------------------------------------------",
                           old_dlls_txt);
                    information_dialog(instantiate(list_written));
                  } [ 'PERMISSION_DENIED,'WRITE_ERROR,'OS_ERROR:
                     /* can't modify old_dlls_txt */
                     information_dialog(instantiate(mod_problem),
                                        mod_content,
                                        instantiate(mod_help));
                    ]
              }
          }
      }

      /**********************************************************************
         Continue with the installation.  Modify Config...
       **********************************************************************/
/* LMurphy - done by winrsf71 */
/*
      if (set_rdbms71)
        {
           ui_action(instantiate(modifying_config));
           modify("RDBMS71",rdbms71,ora_config,"Oracle");
        }

      if (set_pro16)
        {
           ui_action(instantiate(modifying_config));
           modify("PRO16",pro16,ora_config,"Oracle");
        }
*/

      ui_action(instantiate(registering));
      register(current_product,registration_comment);
      if (member(selected_products,current_product))
        reference(current_product);
      reference(winrsf71, current_product);
    }

  /**************************************************************************
     Add BIN to path?...
   **************************************************************************/

  if (add_bin_to_path)
    {
      {
        environment_path = v6_translate("%%PATH%%");
      } [ 'OS_ERROR: environment_path = "";
         'UNBOUND_ENVIRONMENT_VARIABLE: environment_path = "";
        ]
      if (not(member(explode(environment_path,";"),oracle_home_bin)))
        {
          /******************************************************************
             If oracle_home\bin not in path, create autoexec.new, then ask to
             copy AUTOEXEC.NEW to AUTOEXEC.BAT.  First find DOS boot drive...
           ******************************************************************/

          ui_action(identifying_bt_drive);
          boot_drive = dos_boot_drive();

          if (earlier_version(dos_version(),"4"))
            {
              mapped_drives = dos_mapped_drives();
              extract(mapped_drives,"A");
              extract(mapped_drives,"B");
              bt_drive = single_selection_dialog(bt_drive_prompt,
                                                 mapped_drives,
                                                 bt_drive_content,
                                                 bt_drive_help);
            }

          autoexec_bat = "%boot_drive%:\AUTOEXEC.BAT";
          autoexec_new = "%boot_drive%:\AUTOEXEC.NEW";
          autoexec_old = "%boot_drive%:\AUTOEXEC.OLD";

          if (exists(autoexec_bat))
            copy_file(autoexec_bat,autoexec_new);
          else
            create_file(autoexec_new);

          {
            path_variable = "SET PATH";
            separator = " = ";
            autoexec_path = translate(path_variable,autoexec_new,separator);
          } [ 'UNBOUND_ENVIRONMENT_VARIABLE:
             {
               path_variable = "PATH";
               {
                 autoexec_path = translate(path_variable,
                                           autoexec_new,separator);
               } [ 'UNBOUND_ENVIRONMENT_VARIABLE:
                  {
                    separator = " ";
                    {
                      autoexec_path = translate(path_variable,autoexec_new,
                                                separator);
                    } [ 'UNBOUND_ENVIRONMENT_VARIABLE:
                       {
                         autoexec_path = "";
                         continue();
                       } ]
                  } ]
             } ]

          if (member(explode(autoexec_path,";"),oracle_home_bin))
            information_dialog(instantiate(pth_problem_prompt),
                               pth_problem_content,
                               instantiate(pth_problem_help));
          else
            {
              autoexec_path = "%oracle_home_bin%;%autoexec_path%";
              autoexec_path = implode(explode(autoexec_path,";"),";");
              autoexec_path = "%autoexec_path%;";
              len = length(autoexec_path);
              if (len > 127)
                additional_changes_help = grter_path_length;
              else
                additional_changes_help = "";

              modify(path_variable,autoexec_path,autoexec_new,separator);

              modify_autoexec = yesno_dialog(instantiate(mod_autoexec_prompt),TRUE,
                                             instantiate(mod_autoexec_content),
                                             instantiate("%mod_autoexec_help% %additional_changes_help%"));
              if (modify_autoexec)
                {
                  copy_file(autoexec_bat,autoexec_old);
                  move_file(autoexec_new,autoexec_bat);
                  signal('FAILURE,autoexec_was_changed);
                }
            }
        }
    }

  permit_retry_operations = FALSE;
}
