#!/bin/tcsh -f
# $Id: smk_mkdir,v 1.2.4.9 2003/03/14 02:09:20 mhouyoux Exp $

   ### 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 $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
   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

   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
       if( ! -e $SMK_SPDPATH ) mkdir -p $SMK_SPDPATH
       if( ! -e $SMK_M6PATH  ) mkdir -p $SMK_M6PATH
       if( ! -e $SMK_EMISPATH ) mkdir -p $SMK_EMISPATH
       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
   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 $INVDIR/other $GE_DAT $REPOUT \
                 $REPOUT/programs $REPOUT/smkmerge/$SECTOR $INTERMED $LOGS \
                 $M3STAT_LOGS $SRGPRO_PATH $SMK_TMPDIR $SMK_METPATH $SMK_SPDPATH \
                 $SMK_M6PATH $SMK_EMISPATH $SMK_DATEPATH $OUTPUT $REPOUT/graphics $REPOUT/log_analyzer)

       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
   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
