# Recolic CERNET proxy service:
# dssl.cun.io is not available in public Internet. So you have to use my proxy: cernet.cnm.cool.
# If variable is not set, use original version.
[ "$hust_submit_server_host" = "" ] && hust_submit_server_host=dssl.cun.io

function env_set() {
    sed -i -e "/^export $2=.*/d" ~/.bashrc
    echo "export $2=`readlink -e $1`" >> ~/.bashrc
}

[ -z ${STUID} ]   && echo "STUID must be set (RTFM)"   && exit
[ -z ${STUNAME} ] && echo "STUNAME must be set (RTFM)" && exit
echo "uploading public key..."
echo curl -F "id=${STUID}" -F "name=${STUNAME}" -F "file=@${HOME}/.ssh/id_rsa.pub" "https://$hust_submit_server_host/teach/api/uploads/pasetup"
curl -F "id=${STUID}" -F "name=${STUNAME}" -F "file=@${HOME}/.ssh/id_rsa.pub" "https://$hust_submit_server_host/teach/api/uploads/pasetup" -v ||
    (echo 'ERROR: Upload failed. Recolic proxy server is in HongKong and it"s slow, so you may try again.' ; exit 2)
sleep 5
echo "recover from remote git..."
git remote add hustpa "pa@$hust_submit_server_host:${STUID}-git"
git fetch hustpa
git branch -d pa0
git branch -d pa1
git branch -d pa2
git branch -d pa3
git branch -d pa4
git branch -d pa5
git checkout hustpa/pa0
git checkout -b pa0  hustpa/pa0
git checkout hustpa/pa1
git checkout -b pa1  hustpa/pa1
git checkout hustpa/pa2
git checkout -b pa2  hustpa/pa2
git checkout hustpa/pa3
git checkout -b pa3  hustpa/pa3
git checkout hustpa/pa4
git checkout -b pa4  hustpa/pa4
git checkout hustpa/pa5
git checkout -b pa5  hustpa/pa5
echo "set ENV ..."
env_set nemu NEMU_HOME
env_set nexus-am AM_HOME
env_set navy-apps NAVY_HOME
source ~/.bashrc
echo "done!"

