project_rename

#!/bin/ksh
# $Header: $
# $Modtime:$
################################################################################
# COPYRIGHT.  HANG SENG BANK LIMITED 2013.  ALL RIGHTS RESERVED.
# NO PART OF THIS PUBLICATION MAY BE REPRODUCED, STORED IN A RETRIEVAL SYSTEM,
# OR TRANSMITTED, ON ANY FORM OR BY ANY MEANS, ELECTRONIC, MECHANICAL,
# PHOTOCOPYING, RECORDING, OR OTHERWISE, WITHOUT THE PRIOR WRITTEN PERMISSION
# OF HANG SENG BANK LIMITED.
#
# Filename      : projectx_rename (shell script file)
#
# Author        : Luffy Cao
#
# Abstract      : Check the Trade reports delivery signal of Bank level
#
# Modification History :
#
# Version       Date            Author          Description
#-------------  --------------  --------------- ------------------------------
# 1.0           26 Jun 2013     Luffy Cao       Initial Creation
#
##############################################################################
rootpath=$0
cd "${rootpath%/*}"
file=`echo $0 | cut -d'/' -f5`

# The report file (pdf, html, etc) to be renamed
param_src_file=$1

# The corresponding XML file containing information for renaming
param_xml_file=$2


#set up path
log_path=`awk NR==2 ../JobSch.ini | cut -f1`
raw_path=`awk NR==3 ../JobSch.ini | cut -f1`
report_path=`awk NR==17 ../JobSch.ini | cut -f1`

#set up log file and date file
log_file_1=$log_path/${file}_1.log
log_file_2=$log_path/${file}_2.log
date_file=$report_path/projectx_date.txt

echo "Start to execute "$file" ..."

#Create log file
createLogfile()
{
echo "" > $1
}


#xpath function for XML parsing
xpath()
{
echo "cat $1" | xmllint --shell $2 | sed '/^\/ >/d'
}

#Get content of tag from XML file
getContent()
{
 xmlcode=$1
 checktag=$2
 content=`xpath "//*[local-name()='outputDescriptor']/*[local-name()='$checktag']/text()" $xmlcode`
 echo $content
}

#Get value from content of tag in XML file
getValue()
{
 xmlcontent=$1
 checkstr=$2
 found=1
 count=2
 value=""
 while [ $found -ne 0 ]
 do
  valuetmp=`echo "$xmlcontent" |awk -F \/ '{ print $'$count' }' | sed -r "s/(\[|\]|')//g"`
  if [[ $valuetmp == $checkstr* ]]
  then
   value=$valuetmp
   found=0
  else if [ -z "$valuetmp" ]
    then break
    fi
  fi
  count=$(( $count + 1 ))
 done
 echo $value
}
#Get name
getName()
{
 bfrenm=$1
 afrenm=`echo "$bfrenm" |awk -F = '{ print $2 }'`
 echo $afrenm
}


#Compare strings
checkStr()

 str1=$1
    str2=$2
 if [ "$str1" = "$str2" ]
 then
  return 0
 else
  return 1
 fi
}

# check xml package name
isPackage()
{
 xmlsource=$1
 pkgname=$2
 strContent=$(getContent $xmlsource reportSearchPath)
 szpkg=$(getValue "$strContent" "package")
 finalpkgnm=$(getName "$szpkg" )
 #Compare
 checkStr "$pkgname" "$finalpkgnm"
 checkStrcode=`echo $?`
 return $checkStrcode
}

#Rename pdf file
getNewName()
{
 xmlsource=$1
 nName="Unknown Report (PROJECTX)"
 strContent=$(getContent $xmlsource reportViewSearchPath)
 #validRpt=$(getValue "$strContent" "folder")
 #vaildRpt2=$(getName "$validRpt")
 nametmp=$(getValue "$strContent" "reportView")
 #if [ "$vaildRpt2" = "Generated Reports" ]
 #then
  if [ -z "$nametmp" ]
  then
   echo "reportView not found."   
  else
   nName=$(getName "$nametmp")
  fi
 #fi
 echo $nName
}

#copy file and put it to /REPORT
moveFile()
{
 oldpdf=$1
 newpdf=$2
 ext=$3
 if [[ "$oldpdf" == *\.$ext ]]
 then mv $raw_path/$oldpdf $report_path/$newpdf
 fi
}

#create dummy control files
CreateControlFile()
{
 ctlname=$1".ctl"
 content=$2
 echo $content > $report_path/$ctlname
 
 #change group permissions
 [ -f $report_path/$ctlname ] && { chgFileGroupAccess $report_path/$ctlname; }
}

# Change File Group Access
chgFileGroupAccess ()
{
   outfilename=$1
   chgrp cogmap_jbsrun $outfilename
   chmod g+rw $outfilename
}

#Create execution control file
CreateEXEControlFile()
{
 ctlexename=$1".txt"
 content1=$2
 content2=$3
 echo $content1 > $report_path/$ctlexename
 echo $content2 >> $report_path/$ctlexename
}

#Get verDate
getVerDate()
{
 xmlsource=$1
 verdte="Unknown Date"
 verdte=`xpath "//*[local-name()='outputDescriptor']/*[local-name()='parameterValues']/*[local-name()='item']/*[local-name()='name'][text()='Ver Date']/../*[local-name()='value']/*[local-name()='item']/*[local-name()='display']/text()" $xmlsource`
 echo $verdte
 }

#write verdate txt file
writeDateFile()
{
 verdte=$1
 echo $verdte > $date_file
 
}

#housekeeping remove xml after renaming
removeXML()
{
 xmlfile=$1
 rm -f $xmlfile
}

#Get log file name
getLogFileName()
{
 reminder=`echo "\`date +%m\`%2" | bc`
 if [ $reminder -ne 0 ]
 then
  echo "1"
 else
  echo "2"
 fi
 
}

#Reset log file if now month
tryResetLog()
{
 resetLog=1
 logfile=$1
 logNum=$2
 if [ -f $logfile ]
 then
 modMonth=`echo \`ls -l $logfile\` | awk '{print $6}'`
 nowMonth=`echo \`date +%b\``

  if [ "$modMonth" = "$nowMonth" ]
  then
   resetLog=0
  fi
 else
  resetLog=0
  createLogfile $1
 fi
 
 if [ $resetLog -eq 1 ]
 then
  rm -f $logfile         
  createLogfile $1
 fi
}

getExtension()
{
 source=$1
 getext=`echo "$source" |awk -F \. '{ print $2 }'`
 [ "$getext" = "mht" ] && { getext="xls" ;}
 echo $getext
 
}

getDataRdyStus()
{
 xmlsource=$1
 getDataRdyStus="Unknown Status"
 getDataRdyStus=`xpath "//*[local-name()='outputDescriptor']/*[local-name()='parameterValues']/*[local-name()='item']/*[local-name()='name'][text()='Data Ready Status']" $xmlsource`
 [ -z $getDataRdyStus ] && { status="N"; } || { status="Y" ;}
 echo $status

 
}

# for f in `ls $raw_path/*.xml`
# do

 starttime=`date`
 xmlfilename=`basename $param_xml_file`
 LogFileNum=$(getLogFileName)
 
 if [ $LogFileNum = 1 ]
 then 
  tryResetLog $log_file_1 $LogFileNum 
  LogFileName=$log_file_1
 else 
  tryResetLog $log_file_2 $LogFileNum 
  LogFileName=$log_file_2
 fi


 if [ ! -f $param_xml_file ]; then
  echo "$param_xml_file not exists." | tee -a $LogFileName
  exit 1
 fi

 pdffilepath=$(getContent $param_xml_file fileName)

  
  echo "************************************"  | tee -a $LogFileName
  echo "Date: "$starttime       | tee -a $LogFileName
  echo "Parsing XML: "$xmlfilename     | tee -a $LogFileName
  echo "Original PDF: "$pdffilepath    | tee -a $LogFileName
  
  # xml reture error code
  xmlretcode=`echo $?`
  
  if [ $xmlretcode -ne 0 ]
  then 
    
    echo "Fail to parse xml file "$xmlfilename    | tee -a $LogFileName
    exit $xmlretcode
  else
    echo "Start Parse XML file "$xmlfilename   | tee -a $LogFileName
    isPackageretcode=1
    isPackage $param_xml_file "ProjectX"
    isPackageretcode=`echo $?`
    echo "isPackageretcode:$isPackageretcode --new added "  | tee -a $LogFileName
    if [ $isPackageretcode -eq 0 ]
    then
     echo "File belongs to PROJECTX"     | tee -a $LogFileName
     newName=$(getNewName $param_xml_file)
     if [[ $newName == *"Unknown Report (PROJECTX)"* ]]
     then
      echo "$newName, File skipped."    | tee -a $LogFileName
     else
      fileext=$(getExtension $pdffilepath)
      fullnewName=$newName"."$fileext
      echo "Parsed New File Name: "$fullnewName  | tee -a $LogFileName      
      rptVerDte=$(getVerDate $param_xml_file)
      echo "Report Version Date: "$rptVerDte   | tee -a $LogFileName
      #if [[ -z $rptVerDte || "$rptVerDte" = "Unknown Date" ]]
      #then
       #echo "Invalid Date String: [" $rptVerDte "]" | tee -a $LogFileName
      #else
       echo "Output Date String: [" $rptVerDte "]"  | tee -a $LogFileName
       #moveFile $pdffilepath $fullnewName $fileext
       
       #if [[ "$pdffilepath" == *\.$fileext ]]; then
        mv $raw_path/$pdffilepath $report_path/$fullnewName
        echo "Copy XML File to report parth." | tee -a $LogFileName
        ret1=`echo $?`
        if [ $ret1 -ne 0 ]; then
          echo "Failed to move $raw_path/$pdffilepath $report_path/$fullnewName" | tee -a $LogFileName
          exit 1
        fi
       #fi
       
       # change group
       [ -f $report_path/$fullnewName ] && { chgFileGroupAccess $report_path/$fullnewName; }
       
       # Create dummy control file
       CreateControlFile $newName ""
       
       # Create execution control file for LN
       #CreateEXEControlFile $newName "Y" $rptVerDte
       #writeDateFile $rptVerDte
       removeXML $param_xml_file       
      #fi
     fi
    else
     echo "File does not belong to PROJECTX"    | tee -a $LogFileName
    fi
  fi
#done

# reture error code
# retcode=`echo $?`
# if [ $retcode -ne 0 ]
# then
        # echo "Fail to execute "$file         | tee -a $LogFileName
        # exit $retcode
# fi

echo "Successful execution of "$file        
exit 0

你可能感兴趣的:(project)