/* Copyright (c) Oracle Corporation 1995.  All Rights Reserved */

/*******************************************************************************
  NAME
    toolsins.idk - External script for OIS Products' Installation scripts
    
  DESCRIPTION
    This external script is a Library of Routines (like Functions/Procedures).
    Each Routine is a block of code that is being used by more than one
    Installer script.  Each routine is uniquely identified by a global variable
    that needs to be set by the product script calling this script, and a roman
    numeral (for ease of locating the desired Routine).

	Following is a list of all the routines in this Library:
	I.   INSTALL_DEPENDENCIES
	     Register, and Reference the product
	     (used in all ins scripts)
	II.  INSTALL_ENVIRONMENT_VARIABLES
	     Install all the components of the PARENT
	     (used by Parent scripts only)
	III. REGISTER_REFERENCE_PRODUCT
	     Register, and Reference the product
	IV.  INSTALL_COMPONENTS
	     Install all the components of the PARENT
	     (used by Parent scripts only)
	V.   REMOVE_DIRECTORIES    
	     Remove all the directories created by OIS while installing third party product

  OWNER
	Athma Raman

  MODIFIED	MM/DD/YY	REASON
  arajagop  05/09/96    Created and Made OIS reference file 	
********************************************************************************/
{
/********************************************************************
I.		Install dependencies based on the dependency_list created.
     The variables dep_prod_list and dep_instal_cond must be set before calling this script.	
*********************************************************************/
	if (OIS_EXECUTE_SCRIPT == "INSTALL_DEPENDENCIES")
	{  
  		temp_prod_list = dep_prod_list;
  		temp_cond_list = dep_instal_cond;
  		while(not(empty(temp_prod_list)))
  		{
  			{dep_prod = first(temp_prod_list);}
  			['EMPTY_LIST:break();]	
  			{dep_cond = first(temp_cond_list);}
  			['EMPTY_LIST:break();]
			if (dep_cond == "install")
			{
				install(dep_prod);
			}
			temp_prod_list = rest(temp_prod_list);
			temp_cond_list = rest(temp_cond_list);
  		}
  		return (1);
	}
/*********************************************************************/

/********************************************************************
II	     This script binds the environment settings
	The variables file, group, varlist and varval must be set before calling this script.
*****************************************************************************/

	if (OIS_EXECUTE_SCRIPT == "INSTALL_ENVIRONMENT_VARIABLES")
	{
		temp_list = varlist;
		temp_value = vallist;
		temp_app_list = append_list;
		temp_appd_list = append_delimiter_list;
		while(not(empty(varlist)))
		{
			{
				variable = first(temp_list);
			}
			['EMPTY_LIST:break();]
			{
				value = first(temp_value);
			}
			['EMPTY_LIST:break();]
			{
				append_flag = first(temp_app_list);
			}
			['EMPTY_LIST:
				append_flag = "false";
				continue();
			]
			{
				implode_explode_delimeter = first(temp_appd_list);
				if (implode_explode_delimeter == "")
					implode_explode_delimeter = ";"; /* ; is default  */
			}
			['EMPTY_LIST:
				implode_explode_delimeter = ";";
				continue();
			]
			if (append_flag != "true")
			{
				if (value != "NOMODIFY")
				{
					modify(variable,value,file,group);
				}
			}
			else
			{
				if (value != "NOMODIFY")
				{
					ini_path_var = value;
					ini_path_list = explode(ini_path_var, implode_explode_delimiter);
					{
		        			ini_path_var = translate(ini_var, file, group); /* File contains ini_var ?? */
         					paths_in_ini_var = explode(ini_path_var, implode_explode_delimiter);
					}	
	    			[															/* file doesn't have ini_var, add it to file */
	    			'OS_ERROR,'INVALID_FILE_NAME,'FILE_NOT_FOUND,
	        			'PERMISSION_DENIED,'UNBOUND_ENVIRONMENT_VARIABLE:
					{
	        				continue(); 
					}
					]
					while(not(empty(ini_path_list)))
					{
						ini_path_to_add = first(ini_path_list);
						if(not(member(paths_in_ini_var, ini_path_to_add)))
						{
							add(paths_in_ini_var, ini_path_to_add);							/* ini_var does not have the required path, add it to ini_var */
						}
						ini_path_list = rest(ini_path_list);
					}
					ini_path_var = implode(paths_in_ini_var, implode_explode_delimiter);
					modify(variable,ini_path_var,file,group);
				}
			}
			temp_list = rest(temp_list);
			temp_value = rest(temp_value);
			temp_app_list = rest(temp_app_list);
			temp_appd_list = rest(temp_appd_list);
		}
		return (1);
	}
/*********************************************************************/

/********************************************************************
III.		Register, and Reference the product
     The variables dep_prod_list and dep_instal_cond must be set before calling this script.	
*********************************************************************/
	if (OIS_EXECUTE_SCRIPT == "REGISTER_REFERENCE_PRODUCT")
	{  
  		temp_prod_list = dep_prod_list;
  		temp_cond_list = dep_instal_cond;  
  		while(not(empty(temp_prod_list))) 
  		{	
  			{dep_prod = first(temp_prod_list);}	
  			['EMPTY_LIST:break();]
  			{dep_cond = first(temp_cond_list);}
			['EMPTY_LIST:break();]

			if (dep_cond == "install")
			{
				reference(dep_prod,current_product);
			}
			{
				temp_prod_list = rest(temp_prod_list);
			}
			['EMPTY_LIST:continue();]
			{
				temp_cond_list = rest(temp_cond_list);
			}
			['EMPTY_LIST:continue();]
  		}
 	 	return (1);
	}
/*********************************************************************/

/********************************************************************
IV.		Install all the components of the PARENT
*********************************************************************/
	if (OIS_EXECUTE_SCRIPT == "INSTALL_COMPONENTS")
	{
		if(parent_in_control)                  
		{
			/*********************                                          
			Install child products
			**********************/
			ui_product(instantiate(display_product_label));
			ui_action(instantiate(install_components));

			while(not(empty(child_products_to_install)))
			{
				install(first(child_products_to_install));
				child_products_to_install = rest(child_products_to_install);
			}
			/*********************/
		}
		else
		{
			while(not(empty(selected_components_list)))
			{
				install(first(selected_components_list));
				selected_components_list = rest(selected_components_list);
			}
		}
		return(0);
	}

/********************************************************************/

/********************************************************************
V.   REMOVE_DIRECTORIES    
     Remove all the directories created by OIS while installing third party product
This script attempts to delete a directory recursively 
The name of the directory to be deleted should be passed in as deldir 
********************************************************************/
	if (OIS_EXECUTE_SCRIPT == "REMOVE_DIRECTORIES")
	{
 		 directorify(deldir);

		 delfstack = list(deldir);
		 deldstack = list();

  		/* In this loop, remove all the files in the directories */
  		while (not(empty(delfstack)))
   		{
      			deldir = first(delfstack);
		        delfstack = rest(delfstack);

			/* Add this directory to the front of a list of directories that need 
			 to be deleted
			 Reasoning : The root of the tree is at the very end of the list -
			 All of the children of a directory will be deleted before we get to it */
		      	deldstack = cons(deldir, deldstack); 

     			tdf = files("%deldir%\*");
     			tdd = directories("%deldir%\*");

     			/* Remove the files in the directory */  
     			while(not(empty(tdf)))
      			{
        			{ remove_file(first(tdf)); } ['default:continue();]
			        tdf = rest(tdf);
  			}
  
    			while(not(empty(tdd)))
      			{
        			add(delfstack, first(tdd));
        			tdd = rest(tdd);
      			}
   		}

		/* In this loop, remove the directories starting at the deepest subdirs and 
		moving up to the root */ 
  		while(not(empty(deldstack)))
   		{
	     		deldir = first(deldstack);
     			deldstack = rest(deldstack);
     			{ remove_directory(deldir); } ['default : continue(); ]
   		}    
		return(0); 
	}
/* 
   The following variables need to be set before calling this script 
   dll_source_files -> list of dll file names for which the version to be checked
   dll_destination_files -> list of dll file names in which name the dll_source_files will be copied
   There should be a one to one corrsopondance between theses directories
*/
  
	if (OIS_EXECUTE_SCRIPT == "DLL_VERSION_CHECK")
	{  
	      	product_directory = implode(list(product_home,
        	              product_location(current_product)),"\");
	      	pathify(product_directory);
                no_version = "0.0.0.0.0";
      		while (not (empty(dll_source_files)))
      		{
         		/* Set up the filenames and versions */
         		current_destination_file = first(dll_destination_files);
		        current_source_file = implode(list(product_directory,
                                            file_name(current_destination_file)),
					    "\");	
		 	pathify(current_source_file);
			{
 		 		source_file_version = windows_file_version(current_source_file);
			}
			['OS_ERROR:
			{
				source_file_version = no_version;
				continue();
			}
			]  			

		        {
           			if ((not(exists(current_destination_file)))
            			|| (windows_earlier_version(current_destination_file,
                                        source_file_version)))
             				signal('FILE_NOT_FOUND);
			}	 
			[ 
				'FILE_NOT_FOUND: continue();
				'DEFAULT: copy_file(current_source_file,current_destination_file);
			]

		        dll_source_files = rest(dll_source_files);
         		dll_destination_files = rest(dll_destination_files);

		}
   		return(0);
	}

/****************************** 10/10/95 ***********************************************/

}