#!/bin/bash
if [ $# != 2 ];
then
    echo "Usage:$0 REPORT_NAME Email_List(comma-separated,no space)"
    exit 1;
fi

REPO_NAME=$1
EMAIL_LIST=$2
FROM="support@qbrik.in (Qbrik Support)" 
LOG_PATH=$BASE_PATH/reports/report_log;
ZIP_LOG=$BASE_PATH/reports/zip_log;

DATA_BACKUP="dataBackUp"
MASTER_DATA_BACKUP="masterDataBackUp"

if [ "$REPO_NAME" == "$DATA_BACKUP" ]
then 
   REPORT_FILE_NAME="dataBackUp";
elif [ "$REPO_NAME" == "$MASTER_DATA_BACKUP" ]
then 
   REPORT_FILE_NAME="masterDataBackUp";
fi

# Getting the last eod run date
GET_EOD_DATE=$(mysql $DB_NAME -u$DB_USER -p$DB_PASS -se "SELECT MAX(eod_last_completed_on) FROM EndOfDayCheck");
DATE=$(date -d $GET_EOD_DATE +%d-%m-%Y);
EODDATE=$(date -d $GET_EOD_DATE +%Y%m%d);
CURRDATE=$(date +%Y%m%d);
AS_OF_DATE=$EODDATE;

# Check EOD date lesser then current date

if [ $AS_OF_DATE -le $EODDATE ];
then
	# Execute the  for the EOD date
	cd $BASE_PATH
	if [ -f $BASE_PATH/reports/$REPORT_FILE_NAME.$AS_OF_DATE.csv ];
	then
                exit 1;
	fi
	echo "Generating $REPO_NAME for $AS_OF_DATE ...";
	time ./yiic reverseMigrationScript $REPO_NAME --asOfDate=$AS_OF_DATE --mailIds=$EMAIL_LIST >$LOG_PATH 2>&1;
	if [ $? -ne 0 ]; 
	then 
		echo "Error generating report.......";
		exit 1;
	fi
	rm -f $LOG_PATH; 
fi
echo ;
echo "DONE";

