#!/usr/bin/env bash

ACTIVITYNAME="06.RotationActivity"
#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
    # make additional checks as needed
}

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 a submission for this activity.'
        echo 'Run "submit -download" to retrieve an archive ofyour most recent submission in a file named recent.tar.gz.'
        echo 'Run "submit -feedback" to see your score and feedback for this activity after is has been graded.'
    fi
}

submit() {
    echo "Checking submission contents... "
    check
    echo "Transferring files (you may be prompted for your csl password for this)... "
    SUBMISSION=$(TZ=UTC+6 date +"%Y.%m.%d-%H.%M.%S")
    # 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 SUCCESSFUL:\e[0m checked and transferred."
}

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 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 "$@"
