#!/bin/csh -f
# $Id: smk_mkdir,v 1.2.4.9 2003/03/14 02:09:20 mhouyoux Exp $
# 4/8/11 Change detection of IMD_ROOT to create if missing in order to compensate for being dependant on other variables now <beidler.james@epa.gov>

   ### Set umask to be group writable, world read and execute
   umask 2
   set errstat = 0

   ### Make input and output director(ies) #######
   if ( -e $DAT_ROOT ) then
       if( ! -e $INVDIR/ ) mkdir -p $INVDIR
       if( ! -e $INVDIR/$SECTOR ) mkdir -p $INVDIR/$SECTOR
#       if( ! -e $INVDIR/other ) mkdir -p $INVDIR/other
       if( ! -e $GE_DAT ) mkdir -p $GE_DAT
       if( ! -e $GE_DAT/speciation/$CASE ) mkdir -p $GE_DAT/speciation/$CASE
       if( ! -e $REPOUT ) mkdir -p $REPOUT
       if( ! -e $REPOUT/programs ) mkdir -p $REPOUT/programs
       if( ! -e $REPOUT/smkmerge/$SECTOR ) mkdir -p $REPOUT/smkmerge/$SECTOR
       if( ! -e $REPOUT/graphics ) mkdir -p $REPOUT/graphics
       if( ! -e $REPOUT/log_analyzer ) mkdir -p $REPOUT/log_analyzer
       if (! -e $PREMERGED ) mkdir -p $PREMERGED
   else
       echo "ERROR: The SMOKE main data path does not exist!"
       echo "       Please check to make sure that the following"
       echo "       path is valid:"
       echo "       $DAT_ROOT"
       set errstat = 1
   endif

   # 4/8/11 Because the intermediate root is now variable for each project and case, try to detect and create it
   if ( ! -e $IMD_ROOT) mkdir -p $IMD_ROOT

#   if ( -e $IMD_ROOT ) then
       if( ! -e $INTERMED )  mkdir -p $INTERMED
       if( ! -e $LOGS   ) mkdir -p $LOGS  
       if( ! -e $M3STAT_LOGS   ) mkdir -p $M3STAT_LOGS  
       if( ! -e $SRGPRO_PATH ) mkdir -p $SRGPRO_PATH
       if( ! -e $SMK_TMPDIR ) mkdir -p $SMK_TMPDIR  
#       if( ! -e $SMK_METPATH ) mkdir -p $SMK_METPATH  # C. Allen, 8 Aug 2014: This is an old relic of MOBILE6 processing from many years ago. No longer needed
#       if( ! -e $SMK_SPDPATH ) mkdir -p $SMK_SPDPATH  # C. Allen, 8 Aug 2014: This is an old relic of MOBILE6 processing from many years ago. No longer needed
#       if( ! -e $SMK_M6PATH  ) mkdir -p $SMK_M6PATH  # C. Allen, 8 Aug 2014: This is an old relic of MOBILE6 processing from many years ago. No longer needed
#       if( ! -e $SMK_EMISPATH ) mkdir -p $SMK_EMISPATH  # C. Allen, 8 Aug 2014: This is an old relic of MOBILE6 processing from many years ago. No longer needed
       if( ! -e $SMK_DATEPATH ) mkdir -p $SMK_DATEPATH
 #  else
 #     echo "ERROR: The SMOKE intermediate data path does not exist!"
 #      echo "       Please check to make sure that the following"
 #      echo "       path is valid:"
 #      echo "       $IMD_ROOT"
 #      set errstat = 1
 #  endif
   
   if ( -e $OUT_ROOT ) then
       if( ! -e $OUTPUT ) mkdir -p $OUTPUT

       # Create directory for inline files
       if ($MRG_POINT == Y) then
            if( ! -e $OUTPUT/$SECTOR ) mkdir -p $OUTPUT/$SECTOR
       endif

   else
       echo "ERROR: The SMOKE output data path does not exist!"
       echo "       Please check to make sure that the following"
       echo "       path is valid:"
       echo "       $OUT_ROOT"
       set errstat = 1
   endif

   if ( $errstat > 0 ) then
       exit( $errstat )
   endif

   ### Check permissions on output directories ####
   set user = `whoami`
   foreach dir ( $INVDIR $INVDIR/$SECTOR $GE_DAT $REPOUT \
                 $REPOUT/programs $REPOUT/smkmerge/$SECTOR $INTERMED $LOGS \
                 $M3STAT_LOGS $SRGPRO_PATH $SMK_TMPDIR \
                 $SMK_DATEPATH $OUTPUT $REPOUT/graphics $REPOUT/log_analyzer)
		 # $SMK_M6PATH $SMK_EMISPATH $SMK_METPATH $SMK_SPDPATH $INVDIR/other 

       set line   = ( `/bin/ls -ld $dir` )
       set owner  = $line[3]
       set check  = ( `echo $line | grep $user` )
       if ( $status == 0 ) then

           set permis = ( `echo $line | grep drwxrw` )
           if( $status != 0 ) then
               chmod ug+w $dir
           endif

       else

           set permis = ( `echo $line | grep drwxrw` )
           if ( $status != 0 ) then
               echo "NOTE: Do not have write permission for directory"
               echo "      $dir"
               echo "      Check with user $owner for write permissions"
           endif

       endif

   end

#   ## Check length of directory names with constrained lengths
#   #  C. Allen, 8 Aug 2014: SMK_M6PATH is no longer used
#   if ( $?SMK_M6PATH ) then
#      set cnt = `echo $SMK_M6PATH | wc -c`
#      @ cnt = $cnt - 1
#      if ( $cnt > 200 ) then
#         echo "NOTE: your SMK_M6PATH is constrained to 200 characters"
#         echo "      but yours is $cnt characters."
#         echo "      If you are going to run MOBILE6, then you"
#         echo "      must reselect an EDSS_ROOT and ESCEN values"
#         echo "      to ensure that SMK_M6PATH is small enough."
#      endif
#   endif

   cd $ASSIGNS
