#!/bin/bash

if [ -z "$1" ]; then
    echo "Usage: $0 <destionation>"
    exit -1
fi

DEST="$1"

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $DIR
echo "Repo directory is $DIR"

git checkout master
git pull
git rev-parse HEAD > .tmp_id
if [ -d output ] && [ -f .head_id ] && diff -q  .head_id .tmp_id; then
    rm .tmp_id
    echo "No changes since last run"
    exit 0
fi

if [ ! -d ./virtenv ]; then
    python3.8 -m venv virtenv
fi

source virtenv/bin/activate

pip install --upgrade pip || exit -1
pip install -r requirements.txt || exit -1

rm -rf output
nikola build || exit -1

rsync -Pvrt --delete ./output/ $DEST

mv .tmp_id .head_id
