#!/usr/bin/env bash

BOOK_DIR="$(cat $HOME/.Books/bookdir)"

if [[ $1 == 'FORKED' ]]; then
  sleep 60
  while (ps -ef | grep -v grep | grep manuskript &>/dev/null); do
    cd $BOOK_DIR
    git add .
    git commit -m "$(date +'%Y-%m-%d %H:%M:%S') autosave."
    sleep 60
  done
  exit
fi

if (ps -ef | grep -v grep | grep manuskript &>/dev/null); then
  echo "Manuskript is already running."
  exit
fi


screen -d -m nohup $MANUSKRIPT_BIN &>/dev/null &
screen -d -m nohup $0 FORKED &>/dev/null &
