#!/bin/bash
#
#==============================================================
#
#  Abs:  Copies xml files to mccelog "new" area to be moved
#        into mccelog
#
#  Name: xml2elog
#
#
# Created: 10-MAY-2009; Brobeck
#--------------------------------------------------------------
#Mods:
#
#
#---------------------------------------------------------------
DEST="cddev@mccelog:/nfs/slac/g/cd/mccelog/logxml/new/"
#DEST="cddev@mccelog:/tmp/ken/"
LIST="ans1 ans2 ans3 ans4 ans5"

#
# Help area
#
if [ "$1" = "-h" ] || [ "$1" = "-help" ]
  then
	printf "\n\n\tThis script will copy over XML files to mccelog.\n"
	printf "\tThe user is asked to provide file names on the \n"
	printf "\tcommand line or to type them in at a prompt. \n"

	printf "\n\tEX)\n"
 	printf "\n\txml2elog file1 file2 ...(up to 5 files -Only 1 .xml file)\n"
	printf "\txml2elog   ( you will be prompted for the file name(s) )\n\n"
        exit
fi

# Get the info
#
if [ "$1" = "" ]
  then
       printf "\n\tEnter up to 5 files separated by a space"
       printf "\n\n\tEnter filename(s)-> "
       read $LIST
  else
	ans1=$1
	ans2=$2
	ans3=$3
	ans4=$4
	ans5=$5
fi

#
# Make sure all the files exist and there is an .xml file
#
XMLEXIST=1
for x in $ans1 $ans2 $ans3 $ans4 $ans5
do
     if [ -e "$x" ]
       then
	 ISXMLFILE=`echo $x | awk -F / '{print $NF}' | grep ".xml" > /dev/null;echo $?`
	 if [ "$ISXMLFILE" = 0 ]
           then
	     XMLEXIST=0
         fi
       else
	 printf "\n\n\t$x does not exist...Exiting\n\n"
	 exit
     fi
done
 
#
# If no .xml file exit
#
if [ ! "$XMLEXIST" = 0 ]
  then
    printf "\n\n\tNo .xml file found....Exiting\n\n"
    exit
fi

# Must copy over the .xml file last as the mechanism on mccelog will
# fail if it processes the .xml file and cannot find the attachment.
#
for x in $ans1 $ans2 $ans3 $ans4 $ans5
do
    # copy over the .xml file last
    #
    ISXMLFILE=`echo $x | awk -F / '{print $NF}' | grep ".xml" > /dev/null;echo $?`
    if [ "$ISXMLFILE" = 0 ] 
      then
 	  XMLFILE=$x
      else   
          scp -q $x $DEST
    fi
done
#
# Copy the .xml file last
#
scp -q $XMLFILE $DEST


