#!/bin/bash

######################################################################
#
# AUTHOR: Kurt Schwehr, schwehr@ri.cmu.edu
# START DATE: Nov-1999
#
# $Id: pioToWeb.bash,v 1.18 1999/12/01 19:34:51 schwehr Exp $
#
# PROJECT: mvacs SSI
# 
# DESCRIPTION: Convert vicar images to 
#
# INPUTS: see help
#
# ENV VARIABLES USED:
#
# OUTPUT: jpgs and text files to Kelly Zito and Ash to put on the web.
#
# Notes: Designed to be called by either a luser or a MCP (master 
#		control program).
#
#	PIO == Public Infomation Outreach  (or something)
#
# See help for more.
#
######################################################################

echo ------------------------------------------------------------
echo --- Welcome to pioToWeb.bash
echo ------------------------------------------------------------

declare -ri EXIT_FAILURE=1
declare -ri EXIT_SUCCESS=0


declare -ri TERSE=1
declare -ri TRACE=4
declare -ri VERBOSE=8
declare -ri BOMBASTIC=16

if [ -z "$SSI_VERBOSITY" ]; then
    declare -i debugLevel=4
else
    declare -i debugLevel=$SSI_VERBOSITY
fi

# Twisted way to get down to the fundamental script name.
tmp=${0##/*/}
tmp=${tmp%%.bash}
tmp=${tmp##*.}
tmp=${tmp##*/}
declare -r SCRIPT_NAME=$tmp



# $1 is the level to compare against debugLevel
# $2 is the string to echo to stdout.
DebugEcho()
{
    declare -ir val=$1
    if [ "$debugLevel" -ge "$val" ]; then
	#echo $2
	echo "${SCRIPT_NAME}(`printf "%02d" $1`) $2"
    fi
}

#DebugEcho $TERSE     "Terse is on"
#DebugEcho $TRACE     "Trace is on"
DebugEcho $VERBOSE   "Verbose is on"
DebugEcho $BOMBASTIC "Bombastic is on"

DebugEcho $TERSE "debugLevel           = $debugLevel"


######################################################################
# Global variables and constants
######################################################################

if [ -z "$SSI_SOL_UNKNOWN" ]; then
    declare -r SSI_SOL_UNKNOWN=sol_unknown
    DebugEcho $TRACE "setting SSI_SOL_UNKNOWN      = $SSI_SOL_UNKNOWN"
fi

if [ -z "$SSI_TOP_DIR" ]; then
    declare -r SSI_TOP_DIR=/proj/ops/ssi/mort7
    export SSI_TOP_DIR
    DebugEcho $TRACE "setting top dir = $SSI_TOP_DIR"
fi

if [ -z "$SCRIPT_DIR" ]; then
    declare -r SCRIPT_DIR=${SSI_TOP_DIR}/bin
    export SCRIPT_DIR
    DebugEcho $TRACE "setting script dir = $SSI_SCRIPT_DIR"
fi

if [ -z "$BIN_DIR" ]; then
    declare -r BIN_DIR=${SSI_TOP_DIR}/bin/`uname`
    export BIN_DIR
    DebugEcho $TRACE "setting bin dir = $SSI_BIN_DIR"
fi

if [ -z "$SSI_DATA_TOP_DIR" ]; then
    declare -r SSI_DATA_TOP_DIR=${SSI_TOP_DIR}/data
    export SSI_DATA_TOP_DIR
    DebugEcho $TRACE "setting data top dir = $SSI_DATA_TOP_DIR"
fi


if [ -z "$SSI_VAP_IMAGE_DIR" ]; then
    declare -r SSI_VAP_IMAGE_DIR=${SSI_DATA_TOP_DIR}/vap/images
    export SSI_VAP_IMAGE_DIR
    DebugEcho $TRACE "setting vap img dir = $SSI_VAP_IMAGE_DIR"
fi


if [ -z "$SSI_STATUS_DIR" ]; then
    declare -r SSI_STATUS_DIR=${SCRIPT_DIR}/status
    DebugEcho $TRACE "setting status dir = $SSI_STATUS_DIR"
fi

SetStatus()
{
    echo -n "pioToWeb    -- $1" > $SSI_STATUS_DIR/pioToWeb.status
    DebugEcho $VERBOSE "SetStatus to $1"
}

SetStatus "starting up"

if [ -z "$SSI_PIO_QUEUE_DIR" ]; then
    declare -r SSI_PIO_QUEUE_DIR=${SSI_TOP_DIR}/queues/toPio
    DebugEcho $TRACE "ssi stereo queue dir = $SSI_PIO_QUEUE_DIR"
fi

if [ ! -d $SSI_PIO_QUEUE_DIR ]; then
    echo "ERROR: Queue directory does not exist: "
    echo     $SSI_PIO_QUEUE_DIR
    echo
    echo 
    exit $EXIT_FAILURE
fi

# Where we send data out to.
if [ -z "$SSI_WEB_QUEUE_DIR" ]; then
    declare -r SSI_WEB_TO_WORLD_QUEUE_DIR=${SSI_TOP_DIR}/queues/webToWorld
    DebugEcho $TRACE "ssi web to world queue dir = $SSI_WEB_TO_WORLD_QUEUE_DIR"
fi

# Where we send data out to.
if [ -z "$SSI_WEB_QUEUE_DIR" ]; then
    declare -r SSI_WEB_QUEUE_DIR=${SSI_TOP_DIR}/queues/toWeb
    DebugEcho $TRACE "ssi web queue dir = $SSI_WEB_QUEUE_DIR"
fi

if [ ! -d $SSI_WEB_QUEUE_DIR ]; then
    echo "ERROR: Queue directory does not exist: "
    echo     $SSI_PIO_QUEUE_DIR
    echo
    echo 
    exit $EXIT_FAILURE
fi


DebugEcho $VERBOSE  ""
DebugEcho $TRACE    "Image Extension      = $ext"
DebugEcho $VERBOSE  "SSI_TOP_DIR          = $SSI_TOP_DIR"
DebugEcho $VERBOSE  "SSI_DATA_TOP_DIR     = $SSI_DATA_TOP_DIR"
DebugEcho $VERBOSE  "SSI_PIO_QUEUE_DIR = $SSI_STEREO_QUEUE_DIR"
DebugEcho $VERBOSE  ""
DebugEcho $VERBOSE  "SCRIPT_DIR           = $SCRIPT_DIR"
DebugEcho $VERBOSE  "BIN_DIR              = $BIN_DIR"

declare -i sleepTime=30	# Default how long to sleep between checks, in seconds.

#declare -r imageFileNamePattern="[sr]*_*[mlr].$ext"	# Would be nice to say img,cal.
declare -r imageFileNamePattern="[sr]*_*[mlr].[ic][ma][gl]"  # Would be nice to say img,cal.

declare -r rm=/bin/rm	# GNU rm seems to segfault a lot on IRIX 6.5???
declare -r gnuDate=${BIN_DIR}/gnu_date # Use the GNU date program.

declare -r vicGetLabelEntries=${BIN_DIR}/vicGetLabelEntries
declare -r getLabel=${BIN_DIR}/vicGetLabelEntries
declare -r vicar2jpg=${BIN_DIR}/vicar2jpg
declare -r vicar2tiff=${BIN_DIR}/vicar2tiff
declare -r check_vicar=${BIN_DIR}/check_vicar

declare -r hose=${BIN_DIR}/hose

###############################################
# Go to the queue dir so it is out working dir.

# This will keep the path length down with the for loop.
cd $SSI_PIO_QUEUE_DIR

DebugEcho $VERBOSE  "FIX: write the PID to $SSI_STATUS_DIR  write MCP.pid"

######################################################################
# Email logging
######################################################################
#
# This section will send an email out to an address.
# The goal is to use procmail to build a web page that shows the runs
# that occur.
#
# FIX: do we want to spawn this off to the background in case the
# mail gets slow to send?

#declare -r RECIPIENT_1=schwehr@mvacs.ucla.edu
#declare -r RECIPIENT_2=zbinden@mvacs.ucla.edu

#declare -ar recipients=( schwehr@mvacs.ucla.edu zbinden@mvacs.ucla.edu )
declare -ar recipients=( schwehr@mvacs.ucla.edu )


HOSTNICK=`uname -n`

for person in "${recipients[@]}"
do
   DebugEcho $VERBOSE  ""
   DebugEcho $VERBOSE  Sending mail notification to $person

# NOTE: these must be hard tabs!!
cat <<- EOF # | mail $person
	PROGRAM     MCP -- Master Control Program
	ARGS        $0 $1 $2 $3 $4 $5 $6
	USER        $USER
	NODE        `uname -n`
	DOMAINNAME  `domainname`
	OS          `uname`
	OSREL       `uname -r`
	PROCESSOR   `uname -p`
	Hst`nslookup $HOSTNICK | grep -i name`
	PWD         `pwd`
EOF

done # for


######################################################################
# GENERIC HELPER FUNCTIONS
######################################################################

# This script may use stuff not implimented in bash 1.x
checkBashVersion()
{
    declare -ir minBashVersion=2
    declare -ir bashMajorVersion=${BASH_VERSION%%.*}

    if [ $minBashVersion -gt $bashMajorVersion ]; then
	echo "*** ERROR:"
	echo "  Found bash version $BASH_VERSION."
	echo "  Need version ${minBashVersion}.x or higher."
	exit $EXIT_FAILURE
    fi
    DebugEcho $VERBOSE  "  Bash version okay."
}

# Just for safety, let's make sure we are in a known environment.
# If not, then someone will probably have to do some tweaking.
checkOS()
{
    declare -r okOSName=SunOS
    declare -r OSName=`uname`
    if [ $okOSName != $OSName ]; then
	echo "*** WARNING:"
	echo "  Found OS: $OSName"
	echo "  Expected: $okOSName"
	echo "  CONTINUE AT YOUR OWN RISK"
    fi

    declare -r okOSrev="5.7"
    declare -r OSrev=`uname -r`
    if [ $okOSrev != $OSrev ]; then
	echo "*** WARNING:"
	echo "  Found OS rev: $OSrev"
	echo "  Expected: $okOSrev"
	echo "  CONTINUE AT YOUR OWN RISK"
    fi
    DebugEcho $VERBOSE  "  OS checks out okay."
}

######################################################################
# Work Functions
######################################################################

declare -r PAUSE_FILE="pause.rtc"
declare -r RESUME_FILE="resume.rtc"
declare -r STOP_FILE="stop.rtc"

checkRuntimeControl()
{
    if [ -e "$STOP_FILE" ]; then
	$rm -f "$STOP_FILE"
	echo "Found stop file... EXITING NOW!"
	SetStatus "Stopped/done.  Found stop.rtc"
	exit $EXIT_SUCCESS
    fi

    if [ -e "$PAUSE_FILE" ]; then
	$rm -f "$PAUSE_FILE"
	DebugEcho $TRACE "Found pause file.  Waiting for $RESUME_FILE"

	# && ! -e "$STOP_FILE"
	while [ ! -e "$RESUME_FILE"  ]; do
	    DebugEcho $BOMBASTIC "Sleeping 10 seconds"
	    SetStatus "Paused.  Waiting for resume.rtc"
	    sleep 10
	done
	$rm -f $RESUME_FILE
	DebugEcho $TRACE "Resuming"
	SetStatus "Resuming"

	if [ -e "$STOP_FILE" ]; then
	    $rm -f "$STOP_FILE"
	    echo "Found stop file... EXITING NOW!"
	    SetStatus "Stopped/done.  Found stop.rtc"
	    exit $EXIT_SUCCESS
	fi
    fi
}

########################################
#
# Make sure a directory exists.
#
createVerifyDir()
{
    declare -r newDir=$1
    # FIX: do we want to remove this echo?
    #echo " createVerifyDir ( $newDir )"
    if [ ! -d $newDir ]; then
	DebugEcho $VERBOSE "Creating $newDir"
	mkdir $newDir
    fi
    if [ ! -d $newDir ]; then
	echo ls -l $newDir
	ls -l $newDir
	echo ""
	echo "ERROR: Bugger off.. $newDir is not a directory!"
	exit $EXIT_FAILURE
    else
	chgrp sci.ssi $newDir
	# Could be done better with a umask of 002 for read write.
	chmod g+s $newDir
	chmod o-w $newDir
	chmod g+w $newDir
    fi
}


# Outputs data to $1 file.
createImageTxtFile ()
{
	  # FIX: switch based on RAC vrs SSI image.
	  echo "Automatically generated image comments (KDS)" > $1 # $outTxt
	  #$getLabel --long $image IMAGE_ID INSTRUMENT_NAME FILTER_NAME \
	  #  INSTRUMENT_AZIMUTH INSTRUMENT_ELEVATION >> ${outTxt}.old
	  # Soon we are gonna run past the max line length...
	  $getLabel --long $image \
	    APPLICATION_PACKET_NAME AZIMUTH_FOV COMMAND_DESC \
	    ELEVATION_FOV FILTER_NAME FIRST_LINE FIRST_LINE_SAMPLE \
	    IMAGE_ID IMAGE_TIME IMAGE_TYPE INSTRUMENT_FOCAL_LENGTH_COUNT \
	    INSTRUMENT_ID INSTRUMENT_MODE_ID INSTRUMENT_NAME LAMP_STATE_ID \
	    LANDER_INSTRUMENT_AZIMUTH LANDER_INSTRUMENT_ELEVATION \
	    LOCAL_TRUE_SOLAR_TIME MVACS_INSTRUMENT_AZIMUTH \
	    MVACS_INSTRUMENT_ELEVATION MVACS_INSTRUMENT_POSITION \
	    N1 N2 N3 PLANET_DAY_NUMBER REQUEST_ID REQUEST_NAME \
	    SPACECRAFT_CLOCK_START_COUNT \
	    TELEMETRY_MVACS_INST_POSITION \
	    >> $1 # $outTxt
	  $getLabel --long $image \
	    INSTRUMENT_DEPLOYMENT_STATE \
	    >> $1 # $outTxt
}



# Watch the queue for new images, and start the processing of them.
update ()
{
    # Set to non-0 when we have data ready for web server scripts.
    declare NotifyWebScripts=0

    SetStatus "Scanning directory"
    DebugEcho $VERBOSE  "Scanning queue dirctory: "
    DebugEcho $VERBOSE  "  Number of new image pairs:"
    echo "Images remaining: `ls -1 $imageFileNamePattern  2>/dev/null | wc -l`"


    # Remove unused images... null, dark, post null strip.
    # The web does not use them.
    for image in *_*[npd].img
    do
      DebugEcho $VERBOSE "rm of $image... not using null, dark, post null"
      $rm $image
    done

    for image in $imageFileNamePattern
    do
      checkRuntimeControl
      echo "----------------    $image   ----------------"
      if [ -f $image ]; then
	$check_vicar $image
	if [ 0 != $? ]; then
	  DebugEcho $BOMBASTIC "Not an okay vicar file yet($leftImage)"
        else
	  SetStatus "processing $image"

	  # Mark that we have things to send to the web page
	  NotifyWebScripts=1

	  imageRoot=${image%%.*}

	  # The joy of error checking.
	  tmpSol=`$vicGetLabelEntries $image PLANET_DAY_NUMBER`
	  echo $tmpSol
	  if [ "-1" = $tmpSol ]; then
	    outputDir="$SSI_SOL_UNKNOWN"
	  elif [ "-9.99e+31" = $tmpSol ]; then
	    outputDir="$SSI_SOL_UNKNOWN"
	  elif [ "-9.99e+31 " = $tmpSol ]; then
	    outputDir="$SSI_SOL_UNKNOWN"
	  else
	    tmpSol=${tmpSol%.*}	# Strip from the . to end for printf
	    outputDir=`printf "sol%03d" $tmpSol`
	  fi

	  DebugEcho $BOMBASTIC "outputDir = $outputDir"


	  createVerifyDir ${SSI_VAP_IMAGE_DIR}/jpg/${outputDir}
	  createVerifyDir ${SSI_VAP_IMAGE_DIR}/tif/${outputDir}

	  # Fill this in next to bmarcial's jpgs that he is creating.
	  # His are based off of Bob Reid's calibrated images.
	  # These are for rough cut, immediate images.
	  $vicar2jpg  --quality 99 -i 8 $image \
	    ${SSI_VAP_IMAGE_DIR}/jpg/${outputDir}/ames-${image}.jpg
	  $vicar2tiff -i 8 $image \
	    ${SSI_VAP_IMAGE_DIR}/tif/${outputDir}/ames-${image}.tif

	  (cd ${SSI_VAP_IMAGE_DIR}/jpg; \
	    $rm ames-latest.jpg; \
	    ln -s ${outputDir}/ames-${image}.jpg ames-latest.jpg )

	  # FIX: Ignore the top 8 pixels.
	  # Dave Page suggested doubling to 16 pixels and requested
	  # the jpgs be of the maximum quality.
	  #$vicar2jpg -i 16 --quality 99 $image ${SSI_WEB_QUEUE_DIR}/${imageRoot}.jpg
	  # Must confer with peter smith on jpg quality.
	  $vicar2jpg -i 16 $image ${SSI_WEB_QUEUE_DIR}/${imageRoot}.jpg
	  $vicar2jpg -i 16 $image \
		${SSI_WEB_TO_WORLD_QUEUE_DIR}/${imageRoot}.jpg
	    

	  # This is gonna be slow...
	  declare outTxt1=${SSI_WEB_QUEUE_DIR}/${imageRoot}.txt
	  declare outTxt2=${SSI_WEB_TO_WORLD_QUEUE_DIR}/${imageRoot}.txt
	  $rm -f $outTxt1
	  $rm -f $outTxt2

	  createImageTxtFile $outTxt1
	  createImageTxtFile $outTxt2

	  $rm $image
	fi
      else
	DebugEcho $TRACE "WARNING: $image is not a file"
      fi
      
    done

    DebugEcho $BOMBASTIC "NotifyWebScripts = $NotifyWebScripts"

    if [ 0 != $NotifyWebScripts ]; then
         # Send a message to the web team
	 SetStatus "Hose to live.mars.ucla.edu"
	 #$hose live.mars.ucla.edu 11145 --out echo "newImages"
	 DebugEcho $TRACE "hose 128.97.15.83 11145 "
	 $hose 128.97.15.83 11145 --out echo "$SSI_TOP_DIR"
    fi

    SetStatus "done with update"
}


######################################################################
# Main code - starts here
######################################################################

DebugEcho $VERBOSE  ""
DebugEcho $VERBOSE  "Paranoid Mode:"
checkBashVersion
checkOS

#
# Command line options
#
case "$1" in

u | 'update')
    DebugEcho $VERBOSE  "update:"
    DebugEcho $VERBOSE 
    update
    ;;

uc | 'update-continuous')
    DebugEcho $VERBOSE  "update-continuous:"
    DebugEcho $VERBOSE 

    if [ $# -eq 2 ]; then
	sleepTime=$2
    fi
    DebugEcho $VERBOSE  "Time between update starts = $sleepTime (seconds)"
    if [ 1 -gt $sleepTime ]; then
	DebugEcho $VERBOSE  "ERROR: must specify a sleep time of 1 or more seconds."
	exit 1
    fi

    declare -i startTime
    declare -i curTime
    declare -i deltaTime

    DebugEcho $VERBOSE  "Setting up startTime"
    startTime=`$gnuDate \+\%s`
    startTime=$startTime-$sleepTime

    while [ 1 ]; do

        # Look for responce from the lusers who control me.
        checkRuntimeControl

	DebugEcho $VERBOSE  ""
	startTime=`$gnuDate \+\%s`
	update
	curTime=`$gnuDate \+\%s`
	deltaTime=$curTime-$startTime

	if [ $deltaTime -lt $sleepTime ]; then
	    deltaTime=$sleepTime-$deltaTime
	    DebugEcho $VERBOSE  ""
	    DebugEcho $VERBOSE  "sleeping for $deltaTime seconds..."
	    SetStatus "Idle but running"
	    sleep $deltaTime
	fi

    done
    ;;

*)
    echo ""
    echo "usage: $0 {update|update-continuous|clean-data}"
    #echo "usage: $0 {update|update-continuous|clean|clean-links|clean-nuke}"
    echo ""
    echo "  update      -- check for new files; update symbolic links & queue"
    echo "  update-continuous [optionalNumSeconds]"
    echo "              -- run update every x seconds"
    echo "                 Defaults to $sleepTime seconds"
    echo "  u"
    echo "  uc [optNumSec]"
    exit $EXIT_FAILURE
    ;;

esac



######################################################################
# END OF FILE

#
# $Log: pioToWeb.bash,v $
# Revision 1.18  1999/12/01 19:34:51  schwehr
# Added latest image to jpg dir.
#
# Revision 1.17  1999/12/01 00:00:13  schwehr
# Moved hose to AFTER the data has been processed!!!!!
# 	Was notifying before the data was ready!
# Now removes null, post, and dark images from the toPio queue with rm
#
# Revision 1.16  1999/11/29 21:13:54  schwehr
# Added hose message to the web scripts with Anil.
#
# Revision 1.15  1999/11/25 02:32:43  schwehr
# Improved checkRuntimeControl to catch stop.rtc sooner.
#
# Revision 1.14  1999/11/24 21:13:42  schwehr
# Removed old image extension variable.
# Added sorting by sol for vap/images/{jpg,tif}
# 	Added SSI_SOL_UNKNOWN
# 	Added createVerifyDir, but chgrp now sets sci.ssi
#
# Revision 1.13  1999/11/24 02:41:33  schwehr
# Now creates txt files for webToWorld.
#
# Revision 1.12  1999/11/22 21:16:49  schwehr
# Added deployed state to .txt file.
# Remove old comments.
#
# Revision 1.11  1999/11/21 21:16:58  schwehr
# Fixed a typo of quality.
# Added N1 N2 N3 to the items put in the txt file.
# Corrected a SetStatus call.
# Print a warning on not a file in update.
#
# Revision 1.10  1999/11/20 01:28:11  schwehr
# DebugEcho is more terse.
# Comment out unused image extension.
# Added webToWorld queue handling.
# Went back on jpg quality until Peter Smith is in the loop.
#
# Revision 1.9  1999/11/20 00:39:11  schwehr
# Added changes requested by Dave Page for better image quality.
#
# Revision 1.8  1999/11/19 01:33:30  schwehr
# Switched SSI_VAP_IMAGE_JPG_DIR to point one level up and loose the
# 	image type.
# Now produce tif in addition to jpg.
# Perhaps we should separate out by sol###????
#
# Revision 1.7  1999/11/18 22:50:14  schwehr
# Accidentially put SetStatus after exit...  oops.
#
# Revision 1.6  1999/11/18 22:35:50  schwehr
# Now writes ames- image .jpg images to vap/images/jpg.
# Added status reporting.
#
# Revision 1.5  1999/11/17 01:04:40  schwehr
# Switch to new labels for Louise.
#
# Revision 1.4  1999/11/12 06:43:39  schwehr
# Fixed argument for check_vicar.
#
# Revision 1.3  1999/11/11 21:24:45  schwehr
# Updated to the new vicGetLabelEntry with the -l option.
# Added new possible labels for the new EDR SYS v2.2
# Now looks for both img and cal images.
#
# Revision 1.2  1999/11/11 01:37:22  schwehr
# Added the rm of the process image.
#
# Revision 1.1  1999/11/11 01:35:39  schwehr
# First working version of the web script.  Writes a bunch of labels to a
# text file and writes a jpg.
#
#
######## Split from MCP.bash
# Revision 1.11  1999/11/08 21:43:22  schwehr
# Added runtime control.
