#!/bin/bash


#  Creates and possibly FTP's a Ready Report
#  usage:  rdy [F] <mvl name>
#  Example:
#  $>rdy F 888.MVL
#  F signifies FTP. Without it, prints to std out.


shopt -s -o nounset

declare MVL_NAME
declare UPLOAD

if [ $# -eq 1 ] ; then
    MVL_NAME="$1"
elif [ $# -eq 2 ] ; then
    if [ "$1" != "F" ] ; then
        exit 2;
    else
        UPLOAD="true"
        MVL_NAME="$2"
    fi
else
    exit 2
fi

#echo $MVL_NAME
#exit

declare SEP="/"
declare FTP_BIN="transfer"
declare SCH_DIR="/var/sch"
declare MVL_DIR=".mvl"
declare RDY_DIR=".rdy"
declare -r RDY_EXT=".RDY"
declare REMOTE_RDY_DIR="TransactionLogs"
declare LOCAL_MEDIA_DIR #="/mnt/hd/media"   # should be overridden
declare HEAD=$(basename $MVL_NAME .MVL)

# changeble info / in mirror.cfg file
declare HOSTIPADDRESS
declare HOSTMODE=MirrorList
declare CLTUSERPASSWORD=adtec,408adtec2231
declare FTPCLIENTPASV=passive
declare MIRRORLISTFILE="LIST.MVL"
declare HOSTTIMER=60
declare FTPTIMEOUT=10
declare FTPDATATIMEOUT=45 
declare DONOTREPLACEMPEG

declare CFG_FILE="/etc/sysconfig/mirror.cfg"
test -f $CFG_FILE && . $CFG_FILE

test -f "/etc/env.global" && . "/etc/env.global"

if [[ ${HOSTIPADDRESS} != *:* ]]; then
    HOSTIPADDRESS="${HOSTIPADDRESS}:21"
fi

#setup local media dir
declare TMP
declare TMP=${MEDIAPATH%%[:|,]*}   # strip left to either the comma or colon, eventually to ve expanded
if [ -d "$TMP" ]; then             # if path exists then assign.
   LOCAL_MEDIA_DIR=$TMP
fi

#echo $LOCAL_MEDIA_DIR

declare RDY_DIR="${LOCAL_MEDIA_DIR}${SEP}$RDY_DIR${SEP}"

declare MVL_FILES_TO_GET="${RDY_DIR}.mvl_get.${MVL_NAME}"
declare MVL_FILES_GOT="${RDY_DIR}.mvl_got.${MVL_NAME}"
declare MVL_FILES_MISSING="${RDY_DIR}.mvl_missing.${MVL_NAME}"
declare MVL_FILES_TO_DEL="${RDY_DIR}.mvl_del.${MVL_NAME}"
declare MVL_NAME="${LOCAL_MEDIA_DIR}${SEP}${MVL_DIR}${SEP}${MVL_NAME}"
declare LOC_LIST=${RDY_DIR}loc_list
declare EXCESS
declare EXCESS_FORMATTED=${RDY_DIR}excess_formatted

if [ ! -e "$MVL_NAME" ] ; then  #MVL does not exist
   exit 3          
fi

rm -rf $EXCESS_FORMATTED

declare -rx SCH_XML="/var/local/lib/dpi/cfg/scheduler.xml" # sched file. Used to pull HEAD
declare -rx SCH_XSL="/usr/local/lib/mirror/scheduler.xsl"
HEADCHAN=$(xsltproc $SCH_XSL $SCH_XML)

declare HW=$(/sbin/ifconfig eth0 | grep -o "..:..:..:..:..:.." | tr -d :)
declare IPA=$(/sbin/ifconfig eth0 | grep -o "inet addr:[0-9.]*" | cut -d: -f2 | tr . "\n")
declare HEX_IP=$(for OCT in $IPA; do printf "%2X" $OCT; done | tr " " "0")
declare DATE_TIME=$(date "+%w %m %d %y %H %M %S")    
 
find $LOCAL_MEDIA_DIR/* -maxdepth 0 -type f -printf "%f\n" | sort -f > $LOC_LIST
sort -f -o $MVL_FILES_TO_GET $MVL_FILES_TO_GET
#declare EXCESS=$(join -v 1 -i $LOC_LIST $MVL_FILES_TO_GET) # > $EXCESS_NAME
if [ ! -e $EXCESS_FORMATTED ]; then 
    EXCESS=$(join -v 1 -i $LOC_LIST $MVL_FILES_TO_GET)    
    for FILE in $EXCESS; do
        find "${LOCAL_MEDIA_DIR}/${FILE}" -printf "%f %12s %TY-%Tm-%Td %TH:%TM\n\r" >> $EXCESS_FORMATTED
    done
    
fi

function rdy_for_channel () {
    # $1 is the channel
    declare RDY_NAME="${RDY_DIR}${1}${HEAD}.RDY"
    declare SCH=$(find ${SCH_DIR}${SEP} -name *${1}${HEAD}.SCH -printf "%p\n")

    if [ "$UPLOAD" == "true" ]; then
        exec 3<&1                      # save stdin
        exec 1>$RDY_NAME               #redirect stdin to RDY file
        >$RDY_NAME
    fi 

    echo -e "${HOSTNAME}\015"
    echo -e "${HEX_IP}\015"
    echo -e "${HW//:/ }\015"
    echo -e "${DATE_TIME}\015"
    echo -e "00000000 00000 00000000 00000 00000000 00000 00000000 00000\015"
    echo -e "00000 00000 00000 00000 00000 00000 00000 00000\015"
    find ${MVL_NAME} -printf "%f %12s %TY-%Tm-%Td %TH:%TM\r\n"
    echo -e  "\015"
    cat $MVL_FILES_MISSING
    echo -e "\015"
    echo -e "// Unmanaged Media Files\015"
    cat ${EXCESS_FORMATTED}
    echo -e "\015"
    echo -e $(df ${LOCAL_MEDIA_DIR} | grep -v "Filesystem")"\015"
    echo -e "\015"
    echo -e "// Schedule Files\015"
    for FILE in $SCH; do
        find "$FILE" -printf "%f %12s %TY-%Tm-%Td %TH:%TM\r\n"
    done
    echo -e "\015"
 

    if [ "$UPLOAD" == "true" ]; then
        exec 1<&3                      # restore stdin
        exec 3<&-                       # close file desc
        tnftp -Vi -u ftp://${CLTUSERPASSWORD/,/:}@${HOSTIPADDRESS}${SEP}${REMOTE_RDY_DIR}${SEP}${1}${HEAD}.RDY ${RDY_NAME} > /dev/null
    fi 

}

    
for LINE in ${HEADCHAN[*]}; do
   #echo $LINE
   if [[ $LINE == ??$HEAD ]]; then
      rdy_for_channel ${LINE%*???}   
   fi
done

exit 0
