

DESC="
# restore state of archieved service
# if _prod exists on server then
#    - exit
# else
#    - unzip the last state (the one with greater date)
#    - start service
--------------
"

PROD_FOLDER=_prod
SSH_PEM=~/aws/hope.pem
LAST_STATE_FOLDER=last_state/

echo "$DESC"

# check if _prod exists
PROD_EXISTS=""
ssh ubuntu@palladineve.com -i $SSH_PEM "test -d $PROD_FOLDER"
PROD_EXISTS=`echo $?` # 1 if missing, 0 if exists
echo "PROD_EXISTS: $PROD_EXISTS (1 if missing, 0 if exists)"

if [ $PROD_EXISTS -eq 1 ]; then
    echo "No _prod folder on server"
    echo "restoring state"
    # find PROD_BCK_ created the latest
    LATEST_ARCHIVE=$(ssh ubuntu@palladineve.com -i $SSH_PEM "ls -t | grep PROD_BCK_ | head -1")
    # restore state
    ssh ubuntu@palladineve.com -i $SSH_PEM "tar -xzvf $LATEST_ARCHIVE"
    ssh ubuntu@palladineve.com -i $SSH_PEM "rm $LATEST_ARCHIVE"
    ssh ubuntu@palladineve.com -i $SSH_PEM "cd $PROD_FOLDER && sudo docker compose up -d"


else
    echo "prod folder exists"
    echo "service probably running"
    exit 1
fi


