#!/bin/bash

##
## check if THE_FILE triggers various flags
##

THE_FILE=/tmp/testfiletoday:`/bin/date | /bin/sed "s,  *, ,g" | /bin/cut -d" " -f4`

pauseScript()
{
   echo -en "\nHit return to continue "
   read RESP
   return
} # end pauseScript() #

quitScript()
{
   echo -en "\nHit return to EXIT "
   read RESP
   exit
} # end quitScript() #

createTempFile()
{
   /bin/touch ${THE_FILE}.f 2> /dev/null
}

createTempDir()
{
   /bin/mkdir -p ${THE_FILE}.d 2> /dev/null
}

createTempLink()
{
   /bin/mkdir -p ${THE_FILE}.d 2> /dev/null
   /bin/ln -s ${THE_FILE}.d ${THE_FILE}.l 2> /dev/null
}

removeAllTempFiles()
{
   /bin/rm -rf ${THE_FILE}.* 2> /dev/null
}

listAllTempFiles()
{
   /bin/ls -l ${THE_FILE}.* 2> /dev/null
}


# -e FILE: FILE exists
# -f FILE: FILE exists and is a regular file
# -d FILE: FILE exists and is a directory
# -h FILE: FILE exists and is a symbolic link (same as -L)
# -L FILE: FILE exists and is a symbolic link (same as -h)
# 
# -r FILE: FILE exists and read permission is granted
# -w FILE: FILE exists and write permission is granted
# -x FILE: FILE exists and execute (or search) permission is granted

createTempDir
createTempFile
createTempLink

echo -e "\n\n ----------------------------------\n"
for fff in ${THE_FILE}.f ${THE_FILE}.d ${THE_FILE}.l ${THE_FILE}.x ; do
   echo -e "\nTesting ${fff}"

   if [ -e "${fff}" ]; then
      echo "  exists"
      if [ -L "${fff}" ]; then
         echo "  is a symbolic link"
      else
         echo "  not a symbolic link"
      fi
      if [ -f "${fff}" ]; then
         echo "  is a regular file"
      else
         echo "  not a regular file"
      fi
      if [ -d "${fff}" ]; then
         echo "  is a directory"
      else
         echo "  not a directory"
      fi
   else
      echo "  does not exist"
   fi


done

removeAllTempFiles




