#!/usr/bin/env bash

SCRIPT_DIR=$(dirname $(realpath $0))

lightbrown="\033[1;38;5;94m"
darkbrown="\033[38;5;95m"

echo -e "$lightbrown    📚  running the Books! init script 📚 $darkbrown"

al="alias Books=\". $HOME/.Books/booksenv.sh $SCRIPT_DIR\""
if [[ $(grep -c "$al" ~/.bash_aliases) -eq 0 ]]; then
  echo $al >> ~/.bash_aliases
fi

col='export LS_COLORS="$LS_COLORS:*.msk=1;38;5;95"'
if [[ $(grep -c $col ~/.bash_aliases) -eq 0 ]]; then
  echo $col >> ~/.bash_aliases
fi

mv "$SCRIPT_DIR/.Books" "$HOME/.Books"
echo "$SCRIPT_DIR" > $HOME/.Books/bookdir

########## Install screen, git & pandoc #############

if type yum &>/dev/null; then
  pac='yum'
elif type apt &>/dev/null; then
  pac='apt'
else
  echo -e "$darkbrown You're Linuxing wrong."
  exit
fi

for i in git screen pandoc; do
  type $i &>/dev/null || echo -e "$darkbrown Installing $i" && sudo $pac install -y $i
done


########## Install manuskript ###############

function reresource() {
  rm -rf "$HOME/.Books/manuskript/resources/$1"
  ln -s "$HOME/.Books/$1" "$HOME/.Books/manuskript/resources/$1"
}

cd $HOME/.Books && git clone https://github.com/olivierkes/manuskript.git
if [[ $? -ne 0 ]]; then
  cd $HOME/.Books/manuskript && git pull
fi

rm -rf "$HOME/.Books/manuskript/sample-projects"
reresource themes
reresource backgrounds
reresource dictionaries
rm $SCRIPT_DIR/backgrounds
ln -s "$HOME/.Books/backgrounds" "$SCRIPT_DIR/backgrounds"
# ln -s "$HOME/.Books/manuskript/bin/manuskript" "$SCRIPT_DIR/manuskript"


#################  Install Prosegrinder pandoc templates ############

echo -e "$darkbrown Installing Pandoc Templates"
cd $HOME/.Books && git clone https://github.com/prosegrinder/pandoc-templates.git
if [[ $? -ne 0 ]]; then
  cd $HOME/.Books/pandoc-templates && git pull
fi

#################  Install LanguageTool ############

# if type "languagetool" &>/dev/null; then
# fi
echo -e "$darkbrown Installing LanguageTool"
sudo snap install languagetool

# if pip show language_tool_python &>/dev/null; then
# fi
echo -e "$darkbrown Installing LanguageTool Python"
sudo pip install language_tool_python

# #################  Install viu ############

# if [[ ! -e "$HOME/.Books/viu" ]]; then
#   curl -o "$HOME/.Books/viu" "https://github.com/atanunq/viu/releases/download/v1.4.0/viu" && chmod +x "$HOME/.Books/viu"
# fi

# cd $HOME/.Books

##################### Add the password to the git config
cd $SCRIPT_DIR

rou=$(git config --get remote.origin.url | sed 's|https://||g; s/Books/myBooks/g')
rm -rf $SCRIPT_DIR/.git # Get rid of the Books git project, create a new one for YOUR writing
git init

if [[ $(echo $rou | egrep -c [^:]*:[^@]*@ ) -eq 0 ]]; then
  echo -e '\n*******************************************************\n'
  echo -n 'Enter the git username: '
  read username
  echo -n "Enter the git password for $username: "
  stty -echo
  read password
  stty echo
  newurl="https://$username:$password@$rou"
  git config --replace-all remote.origin.url "$newurl"
  echo
  #sed -i "s/https:\/\/git.grant-stuart.com/https:\/\/gordon:$answer@git.grant-stuart.com/g; s/https:\/\/git.grant-stuart.com/\&/g" $SCRIPT_DIR/.git/config
fi

echo -e "$lightbrown Edit your Author details."
echo -e "$darkbrown Press any key to begin."
read -n1
nano $HOME/.Books/frontmatter.yml || vi $HOME/.Books/frontmatter.yml

# sed -i "s%WIBBLE%$SCRIPT_DIR%g" $HOME/.Books/booksenv.sh
touch $HOME/.Books/initted
