proc rmi_mainProc {} {
global rmi env file_name is_win has_blt rmi_license 
global small_screen_hack tk_version tcl_platform

    if [info exists env(ICEM_CHADICE)] {
      set rmi(chadice) 1
      set rmi(Product) "CHADICE"
    } else {
      set rmi(chadice) 0
      set rmi(Product) "IC3M"
    }

#   if {![info exists rmi_license($rmi(Product))] || \
#       $rmi_license($rmi(Product)) == 0} {
#     set ansys_aiic3m_code 326
#     if [expr {[check_license_exists icem ic3m] || [check_license_exists ansys $ansys_aiic3m_code]}] {
#          mess "ICEM CFD Engineering license for $rmi(Product) obtained\n"
#          set rmi_license($rmi(Product)) 1
#     } else {
#          mess "No $rmi(Product) license obtained.\n" red
#          set rmi_license($rmi(Product)) 0
#          return
#     }
#   }
           set rmi_license($rmi(Product)) 1
    
    if {[info exists file_name(project_dir)] && ($file_name(project_dir) == "") } {
      mess "No project selected - select one project first.\n" red
      return
    }

    if [ winfo exists .rmi ] {
       wm deiconify .rmi
       raise .rmi
       return
    }
    
    wm iconify .v
    update
    set rmi(waitWindow) [ application_wait ]
    
    if ![ info exists is_win ] {
      set is_win 0
      set has_blt 0
      set small_screen_hack 0
      if [info exists tk_version] {
	compute_sizes
      }
    }
    
    if [ info exists tk_version ] {
      set rmi(Logo)   [ image create photo -file \
		$env(MED_ROOT)/lib/rmi/icemcfd.gif ]
      set rmi(Image)  [ image create photo -file \
        	$env(MED_ROOT)/lib/rmi/rmi.gif ]
      set rmi(Browse) [ image create photo -file \
      		$env(MED_ROOT)/lib/rmi/browse.gif ]
      set rmi(NoValveBitmap) [ image create bitmap -file \
      		$env(MED_ROOT)/lib/rmi/no_valve_motion.xbm -background "" ]
      set rmi(IntakeValveBitmap) [ image create bitmap -file \
      		$env(MED_ROOT)/lib/rmi/intake_valve_motion.xbm -background "" ]
      set rmi(ExhaustValveBitmap) [ image create bitmap -file \
      		$env(MED_ROOT)/lib/rmi/exhaust_valve_motion.xbm -background "" ]
      set rmi(TwoValveBitmap) [ image create bitmap -file \
      		$env(MED_ROOT)/lib/rmi/two_valve_motion.xbm -background "" ]
      set rmi(Alpha0Bitmap) [ image create bitmap -file \
      		$env(MED_ROOT)/lib/rmi/alpha0.xbm -background "" ]
      set rmi(Alpha1Bitmap) [ image create bitmap -file \
      		$env(MED_ROOT)/lib/rmi/alpha1.xbm -background "" ]
      set rmi(PlusBitmap) [ image create bitmap -file \
      		$env(MED_ROOT)/lib/rmi/plus.xbm -background "" ]
      set rmi(MinusBitmap) [ image create bitmap -file \
      		$env(MED_ROOT)/lib/rmi/minus.xbm -background "" ]
      set rmi(ValveMesh1)  [ image create photo -file \
        	$env(MED_ROOT)/lib/rmi/valve_mesh1.gif ]
      set rmi(ValveMesh1Small)  [ image create photo -file \
        	$env(MED_ROOT)/lib/rmi/valve_mesh1_small.gif ]
      if $rmi(chadice) {
        set rmi(ValveMesh2)  [ image create photo -file \
      		$env(MED_ROOT)/lib/rmi/valve_mesh3.gif ]
        set rmi(ValveMesh2Small)  [ image create photo -file \
      		$env(MED_ROOT)/lib/rmi/valve_mesh3_small.gif ]
      } else {
        set rmi(ValveMesh2)  [ image create photo -file \
      		$env(MED_ROOT)/lib/rmi/valve_mesh2.gif ]
        set rmi(ValveMesh2Small)  [ image create photo -file \
      		$env(MED_ROOT)/lib/rmi/valve_mesh2_small.gif ]
      }
      set rmi(ValveTip)  [ image create photo -file \
        	$env(MED_ROOT)/lib/rmi/valve_tip.gif ]
    }

    wm withdraw .
        
    set d [ form_init .rmi "ICEM CFD $rmi(Product)" "" nobuttons ]
    if { $d != "" } {
           
      set rmi(mainWindow) .rmi
      if $is_win {
        set rmi(configuration) [win_shortname $file_name(project_dir)]
      } else {
        set rmi(configuration) $file_name(project_dir)
      }
      set rmi(ConfigurationSelected) 1
      if {[info exists file_name(tetin)] && ($file_name(tetin) != "")} {
        set rmi(tetin_fn) [file tail $file_name(tetin)]
        set rmi(TetinSelected) 1
      } else {
        if { $rmi(tetin_fn) == "" } {
          set rmi(TetinSelected) 0
        }
      }

      form_frame $d.1 flat 1 ""			;# top frame 
      form_frame $d.2 flat 6 ""			;# bottom frame
      
      if $rmi(chadice) {
        $d.2 configure -width 750 -height 650
      } else {
        $d.2 configure -width 750 -height 580
      }
      pack propagate $d.2 0
      
      form_frame $d.2.1 ridge 3 ""		;# file (image)
      form_frame $d.2.2 ridge 3 ""		;# engine geometry
      form_frame $d.2.3 ridge 3 ""		;# load case
      form_frame $d.2.4 ridge 3 ""		;# engine meshing
      
      form_frame $d.1.1 ridge 3 ""		;# icem logo
      form_frame $d.1.2 flat 3 ""      		;# menu ( checkbutton + top file )
      form_frame $d.1.2.1 ridge 2 ""		;# checkbutton menu
      form_frame $d.1.2.2 sunken 2 ""		;# "file" or "meshing" menu
      
      rmi_CreateLogo $d.1.1
      rmi_CreateMenuMask $d.1.2.1
      
      rmi_CreateFileMenu $d.1.2.2
      $d.1.2.2 configure -width 328 -height 65
      pack propagate $d.1.2.2 0
      
      rmi_CreateEngineMeshingMenu $d.1.2.2     
      rmi_CreateEngineGeometryMenu $d.1.2.2
             
      rmi_CreateFormFile $d.2.1
      rmi_CreateFormEngineGeometry $d.2.2
      if $rmi(chadice) {
        rmi_CreateFormEngineMeshingChadice $d.2.4
      } else {
        rmi_CreateFormEngineMeshing $d.2.4
      }
      rmi_CreateMessageMask
      
      pack append $d.1.2 \
                  $d.1.2.1 { top fill expand } \
                  $d.1.2.2 { top fill expand }
      pack append $d.1 \
                  $d.1.1 { left } \
                  $d.1.2 { left fill expand }     
      pack append $d \
      		  $d.1 { top fill expand } \
      		  $d.2 { top fill expand }
      if $rmi(chadice) {
        form_finish +200+90 1 0
      } else {
        form_finish +200+150 1 0
      }
    }
    rmi_ChangeMask   
    wm protocol .rmi WM_DELETE_WINDOW "rmi_Quit"
    bind $rmi(mainWindow) <Map> { catch {wm deiconify .rmimess} }
    ###bind $rmi(mainWindow) <Unmap> { wm iconify .rmimess }
    
    set rmi(CursorBitmap) [$rmi(mainWindow) cget -cursor]
    set rmi(CursorFromBitmap) [list @$env(MED_ROOT)/lib/rmi/from.xbm black]
    set rmi(CursorToBitmap) [list @$env(MED_ROOT)/lib/rmi/to.xbm black]
    set rmi(CursorOKBitmap) [list @$env(MED_ROOT)/lib/rmi/ok.xbm black]
    if {$rmi(Platform) == ""} {
      set os $tcl_platform(os)
      if {$os == "IRIX"} { 
        set rmi(Platform) mips4
      } elseif {$os == "IRIX64"} {
        set rmi(Platform) mips64
      } elseif {$os == "AIX"} {
        set rmi(Platform) ibm64
      } elseif {$os == "SunOS"} {
        set rmi(Platform) sol64
      } elseif {$os == "OSF1"} {
        set rmi(Platform) alpha40
      } elseif {$os == "HP-UX"} {
        set rmi(Platform) hp11
      } elseif {$os == "Linux"} {
        set rmi(Platform) linux
      } else {
        set rmi(Platform) hp11
      }
    }
    
    rmi_ShowMessage "$rmi(Product) version 5.0.\n" blue
    
    rmi_GetNumberOfValves
}

proc rmi_CreateIntakeLayers {} {
global rmi col

  if [info exists rmi(MaskFormIntakeLayersNumber)] {
    foreach item $rmi(MaskFormIntakeLayersNumber) {
      destroy $item
    }
  }
  
  set w $rmi(MaskFormIntakeLayers)
  set rmi(MaskFormIntakeLayersNumber) ""
  for { set i 1 } { $i <= $rmi(intake_number_valve) } { incr i } {
  
    form_radiobutton $w.$i "$i" rmi(current_intake_valve) $i "" "" "rmi_ResetLayers intake Intake"
    $w.$i configure -selectcolor $col($i) 
    pack append $w $w.$i { left padx 25 }
    lappend rmi(MaskFormIntakeLayersNumber) $w.$i
  }
  if { $rmi(intake_number_valve) < $rmi(current_intake_valve) } {
    set rmi(current_intake_valve) 1
  }
  rmi_ResetLayers intake Intake
}

proc rmi_CreateExhaustLayers {} {
global rmi col

  if [info exists rmi(MaskFormExhaustLayersNumber)] {
    foreach item $rmi(MaskFormExhaustLayersNumber) {
      destroy $item
    }
  }
  
  set w $rmi(MaskFormExhaustLayers)
  set rmi(MaskFormExhaustLayersNumber) ""
  for { set i 1 } { $i <= $rmi(exhaust_number_valve) } { incr i } {
  
    form_radiobutton $w.$i "$i" rmi(current_exhaust_valve) $i "" "" "rmi_ResetLayers exhaust Exhaust"
    $w.$i configure -selectcolor $col($i) 
    pack append $w $w.$i { left padx 25 }
    lappend rmi(MaskFormExhaustLayersNumber) $w.$i
  }
  if { $rmi(exhaust_number_valve) < $rmi(current_exhaust_valve) } {
    set rmi(current_exhaust_valve) 1
  }
  rmi_ResetLayers exhaust Exhaust
}

proc rmi_ChangeMask {} {
global rmi

  foreach item $rmi(MaskForm) {
    pack forget $item
  }
  foreach item $rmi(MaskFileSettings) {
    pack forget $item
  }
  foreach item $rmi(MaskFileCasename) {
    pack forget $item
  }
  foreach item $rmi(MaskFile) {
    pack forget $item
  }
  foreach item $rmi(MaskEngineMeshing) {
    pack forget $item
  }
  foreach item $rmi(MaskEngineGeometry) {
    pack forget $item
  }

  switch $rmi(Mask) {
    1 {
      foreach item $rmi(MaskFileSettings) {
        pack append $rmi(FrameFileSettings) $item { left fillx expand }
      }
      foreach item $rmi(MaskFileCasename) {
        pack append $rmi(FrameFileCasename) $item { left fillx expand }
      }
      pack $rmi(FrameFile) $rmi(FrameFileSettings) $rmi(FrameFileCasename) \
  		       -side top -expand 1 -fill x
      pack $rmi(MaskFormFile) -side top -expand 1 -fill x
    }
    2 {
      foreach item $rmi(MaskEngineGeometry) {
        pack append $rmi(FrameEngineGeometry) $item { left fillx expand }
      }
      rmi_IsTetinSelected
      set num 0
      if { $rmi(intake_motion) == "1" } { set num 1 }
      if { $rmi(exhaust_motion) == "1" } {  set num 2 }
      if { $rmi(intake_motion) == "1" && $rmi(exhaust_motion) == "1" } { set num 3 }
      rmi_SetMotion $num GrayOut
      pack $rmi(MaskFormEngineGeometry) -side top -expand 1 -fill x
    }
    3 {
      foreach item $rmi(MaskEngineMeshing) {
        pack append $rmi(FrameEngineMeshing) $item { left fillx expand }
      }
      if !$rmi(chadice) {
        rmi_CreateIntakeLayers
        rmi_CreateExhaustLayers
        set num 0
        if { $rmi(intake_motion) == "1" } { set num 1 }
        if { $rmi(exhaust_motion) == "1" } { set num 2 }
        if { $rmi(intake_motion) == "1" && $rmi(exhaust_motion) == "1" } { set num 3 }
        rmi_SetMotion $num GrayOut
      }
      rmi_CreateFieldProcessors $rmi(HpcButton)
      pack $rmi(MaskFormEngineMeshing) -side top -expand 1 -fill x
    }
  }
  rmi_XviewEnd
}  


