#!/bin/bash
set -euo pipefail

VAULT_FILE="capture.md"
VAULT_PATH="$HOME/notebook"
FILE_IN_VIEW=""
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -f|--file)
    VAULT_FILE="$2"
    shift # past argument
    shift # past value
    ;;
    --viewing)
    FILE_IN_VIEW="$2"
    shift
    shift
    ;;
    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done

TO_PATH="${VAULT_PATH}/${VAULT_FILE}"

cd ${VAULT_PATH}
git pull -q

echo "" >> ${TO_PATH}
echo "##### `date`" >> ${TO_PATH}
if [ -n "${FILE_IN_VIEW}" ]; then
    echo "In file _${FILE_IN_VIEW}_" >> ${TO_PATH}
fi
for s in "${POSITIONAL[@]}" # restore positional parameters
do
    echo -n "${s} " >> ${TO_PATH}
done
echo "" >> TO_PATH

git add ${TO_PATH}
git commit -q -m "capture `date +%FT%T`"
git push -q 2>&1 > /dev/null
