#!/bin/csh -f

# Convert all xml files to env files
./Tools/xml2env
if ($status != 0) then
   echo "ccsm_getenv error "
   exit -1
endif

# Source the result of the env files - in the following order
foreach i (env_case.xml  env_build.xml env_run.xml env_mach_pes.xml env_test.xml )
  if ( -e $i ) then
    source $i:r || exit -1
    if ($status != 0) then
       echo "ccsm_getenv error: problem sourcing $i:r" 
       exit -2
    endif 
    rm $i:r    
    if ($status != 0) then
       echo "ccsm_getenv error: problem removing $i:r" 
       exit -3
    endif
  endif
end


# Source env_mach_specific 
source env_mach_specific
if ($status != 0) then
   echo "ccsm_getenv error: problem sourcing env_mach_specific"; exit -2
endif 

# Create env_derived and source it
set fname = env_derived
cat >! ${fname} << EOF
#!/bin/csh -f

# This file should NOT be modified by users

set MODELS     = (         cpl         atm         lnd         ice         ocn         glc          wav           rof)	  
set COMPONENTS = (   \$COMP_CPL   \$COMP_ATM   \$COMP_LND   \$COMP_ICE   \$COMP_OCN   \$COMP_GLC   \$COMP_WAV    \$COMP_ROF)  
set NTASKS     = ( \$NTASKS_CPL \$NTASKS_ATM \$NTASKS_LND \$NTASKS_ICE \$NTASKS_OCN \$NTASKS_GLC \$NTASKS_WAV  \$NTASKS_ROF) 
set NTHRDS     = ( \$NTHRDS_CPL \$NTHRDS_ATM \$NTHRDS_LND \$NTHRDS_ICE \$NTHRDS_OCN \$NTHRDS_GLC \$NTHRDS_WAV  \$NTHRDS_ROF) 
set ROOTPE     = ( \$ROOTPE_CPL \$ROOTPE_ATM \$ROOTPE_LND \$ROOTPE_ICE \$ROOTPE_OCN \$ROOTPE_GLC \$ROOTPE_WAV  \$ROOTPE_ROF) 
set PSTRID     = ( \$PSTRID_CPL \$PSTRID_ATM \$PSTRID_LND \$PSTRID_ICE \$PSTRID_OCN \$PSTRID_GLC \$PSTRID_WAV  \$PSTRID_ROF) 
set NINST      = (          1    \$NINST_ATM  \$NINST_LND  \$NINST_ICE  \$NINST_OCN  \$NINST_GLC  \$NINST_WAV   \$NINST_ROF)  
 
EOF
source ${fname}
cp -f ${fname} LockedFiles/${fname}.locked



