#!/usr/bin/env bash

ACTIVITYNAME="13.CGIActivity"
#note that this script excludes the junit5.jar

check() {
    SIZE=$(du -s | cut -f 1) # ensure folder size is under 2MB
    if [[ $SIZE -gt 2048 ]]; then abort "Submission too large: $SIZE > 2048 limit."; fi
    PWD=$(pwd) # ensure working directory name matches ACTIVITYNAME    
    if [[ ${PWD##*/} != $ACTIVITYNAME ]]; then abort "This script is meant to submit activity: \"$ACTIVITYNAME\" but is running from a folder named \"$PWD\", and these names do not match"; fi
    # make additional checks as needed here ...
}

main() {
    read -p "Enter your CSL username: " USERNAME
    if [[ $# -eq 0 ]]; then  # submit contents of current folder
	submit
    elif [[ $1 == -download ]]; then # retrieve archive or recent submission
	download
    elif [[ $1 == -feedback ]]; then # display score and feedback after grading
	feedback
    else # display usage message
        echo "Usage: submit [-download | -feedback]"
        echo "Run \"submit\" to record the contents of your working directory as the your most recent submission for activity $ACTIVITYNAME."
        echo "Run \"submit -download\" to retrieve an archive ofyour most recent submission as a file named recent.tar.gz."
        echo "Run \"submit -feedback\" to see your score and feedback for this and other activities that have been fully graded."
    fi
}

submit() {
    echo "Checking submission contents... "
    check
    echo "Transferring files (you may be prompted for your csl password and duo authentication for this)... "
    SUBMISSION=$(TZ=UTC+6 date +"%Y.%m.%d-%H.%M.%S")
    # exclude junit5.jar from all submissions
    # if hidden files other than . and .. exist, then make sure to copy them
    if ls ./.[^.]* > /dev/null 2>&1; then
	rsync -r --exclude junit5.jar ./{*,.[^.]*} --rsync-path="mkdir -p /p/course/cs400/activities/submissions/${USERNAME}/${ACTIVITYNAME} && rsync" ${USERNAME}@best-linux.cs.wisc.edu:/p/course/cs400/activities/submissions/${USERNAME}/${ACTIVITYNAME}/${SUBMISSION}/
    # otherwise avoid the error message stating that they don't exist
    else
	rsync -r --exclude junit5.jar * --rsync-path="mkdir -p /p/course/cs400/activities/submissions/${USERNAME}/${ACTIVITYNAME} && rsync" ${USERNAME}@best-linux.cs.wisc.edu:/p/course/cs400/activities/submissions/${USERNAME}/${ACTIVITYNAME}/${SUBMISSION}/
    fi
    if [[ $? -ne 0 ]]; then abort "File transfer failed."; fi    
    echo -e "\e[32mSUBMISSION RECEIVED:\e[0m Please use \"./submit -download\" to confirm that the contents of your most recent submission are correct."
}

abort() {
    echo -e "\e[31m$1\e[0m"
    exit 1
}

download() {
    if [[ -f recent.tar.gz ]]; then
	read -p "There is already a recent.tar.gz file in this directory, would you like to attempt to overwrite this file (y/n): " OVERWRITE
	if [[ $OVERWRITE = "y" || $OVERWRITE = "Y" ]]; then
	    rm -f recent.tar.gz
	else
	    echo "Aborting download."
	    exit 0
	fi	
    fi

    echo "Building and retrieving archive (you may be prompted for your csl password and duo authentication for this)... "
    EXITCODE=$(rsync ${USERNAME}@best-linux.cs.wisc.edu:~/recent.tar.gz . --rsync-path="tar -czf ~/recent.tar.gz -C /p/course/cs400/activities/submissions/${USERNAME}/${ACTIVITYNAME}/ \$(ls /p/course/cs400/activities/submissions/${USERNAME}/${ACTIVITYNAME}/ | sort -n | tail -1) && rsync")
    if [[ $EXITCODE -eq 0 && -f recent.tar.gz ]]; then
       echo -e "\e[32mDOWNLOAD SUCCESSFUL.\e[0m"
    else
	echo "Encountered problem downloading submission."
    fi
}

feedback() {
    ssh ${USERNAME}@best-linux.cs.wisc.edu "/p/course/cs400/public/feedback"
}

main "$@"
