#Takes all .pdf's from pdf_dir and converts them to .png's at png_dir
pdf_to_png(){

    # $1=pdf_dir, $2=png_dir
    echo -n "Working: ["
    for file in ${1}/*.pdf ; do

        pdf_name=${file##*/}
        convert -density 50 -trim -fuzz 1% $file ${2}/${pdf_name%.pdf}.png
        echo -n "#"

    done

    echo -n "]"
    echo " "

}

setup(){

    # $1 : public_html directory, ${2} : target directory

    if [[ -e ${1} ]] ; then

        aplot=${1}/autoplotter
        if [[ -e ${aplot}/${2} ]] ; then
            echo "Updating ${2} in AutoPlotter"
        else
            echo "Adding ${2} to AutoPlotter..."
            mkdir ${aplot}/${2}
            mkdir ${aplot}/${2}/pdfs
            mkdir ${aplot}/${2}/pngs
            mkdir ${aplot}/${2}/txts
            curl https://raw.githubusercontent.com/jkguiang/AutoPlotter/master/index.php > ${aplot}/${2}/index.php
            curl https://raw.githubusercontent.com/jkguiang/AutoPlotter/master/favicon.ico > ${aplot}/${2}/favicon.ico
            echo "Added ${2}."
        fi
        
    else
        echo "Public html directory not found, please log in to your UAF account to complete set up."
        exit 0

    fi
}

updt(){

    # $1 : target directory, $2 : origin directory

    if [[ -e ${1} ]] ; then

        if [[ -e ${1}/pdfs ]] ; then
            
            rm -rf ${1}/pdfs/* ${1}/pngs/* ${1}/txts/*

            found=false

            echo "Looking for pdfs..."
            if [[ -n "$(ls ${2}/*.pdf 2>/dev/null)" ]] ; then
                found=true
                for file in ${2}/*.pdf ; do
                    cp ${file} ${1}/pdfs
                done

                pdf_to_png ${1}/pdfs ${1}/pngs
            fi
            echo "Looking for pngs..."
            if [[ -n "$(ls ${2}/*.png 2>/dev/null)" ]] ; then
                found=true
                for file in ${2}/*.png ; do
                    cp ${file} ${1}/pngs
                done
            fi
            echo "Looking for txts..."
            if [[ -n "$(ls ${2}/*.txt 2>/dev/null)" ]] ; then
                for file in ${2}/*.txt ; do
                    cp ${file} ${1}/txts
                done
            fi

            if [[ "$found" = true ]] ; then
                chmod -R 755 ${1}/pdfs
                chmod -R 755 ${1}/pngs
                chmod -R 755 ${1}/txts
                chmod -R 755 ${1}
            else
                echo "Error: no .pdf or .png files found"
                rm -rf ${1}
                exit 0
            fi
            

        fi

    else
        echo "Error: ${1} not found."
        exit 0
    fi
}

html=/home/users/${USER}/public_html


if [[ -e ${1} ]] ; then
    if [[ -d ${1} ]] ; then
        to_orig=$(cd "$(dirname "$1")"; pwd -P)
        targ=$(basename ${1})
        orig=${to_orig}/${targ}
        if [[ -e ${1}/.aplot ]] ; then
            while true; do
                echo "Overwrite ${targ}? (y/n): "
                read resp
                if [[ "${resp}" == "n" ]] ; then
                    echo "Please enter a new target directory name, then hit ENTER: "
                    read new_name
                    targ=${new_name}
                    break
                elif [[ "${resp}" == "y" ]] ; then
                    targ=$(cat ${1}/.aplot)
                    break
                fi
            done
        fi
        if [[ ${2} ]] ; then
            touch ${1}/.aplot
            echo ${2} > ${1}/.aplot
            targ=${2}
        fi
        if [[ ! -e ${html}/autoplotter ]] ; then
            mkdir ${html}/autoplotter
            curl https://raw.githubusercontent.com/jkguiang/AutoPlotter/master/main.php > ${html}/autoplotter/index.php
            curl https://raw.githubusercontent.com/jkguiang/AutoPlotter/master/main.js > ${html}/autoplotter/main.js
            curl https://raw.githubusercontent.com/jkguiang/AutoPlotter/master/favicon.ico > ${html}/autoplotter/favicon.ico
            curl https://home.cern/sites/home.web.cern.ch/files/image/update-for_the_public/2015/01/cms.jpg > ${html}/autoplotter/cms.jpg
        fi
        setup ${html} ${targ}
        updt ${html}/autoplotter/${targ} ${orig} 
        chmod -R 755 ${html}/autoplotter/
        echo "Finished. Navigate to the link below to view plots:"
        echo "uaf-8.t2.ucsd.edu/~${USER}/autoplotter/${targ}"
        exit 0
    else
        echo "${1} is not a directory."
        exit 0
    fi
else
    if [[ "${1}" == "rm" ]] ; then
        if [[ "${2}" != "" ]] ; then
            if [[ ! -e ${html}/autoplotter/${2}  ]] ; then
                echo "No entry named ${2} exists in ${html}/autoplotter."
                exit 0
            else
                rm -rf ${html}/autoplotter/${2}
                echo "Removed ${2}."
                exit 0
            fi
        else
            echo "Usage: aplot rm <target_directory>"
            exit 0
        fi
    else
        if [[ ${1} ]] ; then
            echo "Error: ${1} does not exit."
        else
            echo "Default Usage: aplot <origin_directory> <optional_target>"
        fi
        exit 0
    fi
fi