#!/bin/sh

##
# This script is to install Mateix
##

ROOTDOTFILE="/etc/.mateix"
BINFILE="/usr/bin"
SERVICEFILE="/etc/systemd/system"

## Colours
RED='\033[0;31m'
BLUE='\033[0;34m'
GREEN='\033[0;32m'
YELLOW="\033[1;33m"
NC='\033[0m'

echo $YELLOW
cat << "EOF"
---------------------------------------------------------------
 ___      ___       __  ___________  _______   __     ___  ___
|"  \    /"  |     /""\("     _   ")/"     "| |" \   |"  \/"  |
 \   \  //   |    /    \)__/  \\__/(: ______) ||  |   \   \  /
 /\\  \/.    |   /' /\  \  \\_ /    \/    |   |:  |    \\  \/
|: \.        |  //  __'  \ |.  |    // ___)_  |.  |    /\.  \
|.  \    /:  | /   /  \\  \\:  |   (:      "| /\  |\  /  \   \
|___|\__/|___|(___/    \___)\__|    \_______)(__\_|_)|___/\___|

---------------------------------------------------------------
EOF
echo ${NC}
echo "
This will install the Mateix tool in your system. This require
the root acess. Following are the tasks preformed.

${GREEN}[Task 1]${NC} Install all the dependencies.
${GREEN}[Task 2]${NC} Install the Mateix executables and services.
${GREEN}[Task 3]${NC} Create the dotfiles and config files.
"

echo "${YELLOW}---------------------------------------------------------------${NC}"

echo "\n${GREEN}- Executing Task 1${NC}\n"

sudo apt-get -y install inotify-tools

echo "\n${GREEN}✔ Task 1 Complete!${NC}\n"
echo "\n${GREEN}- Executing Task 2${NC}\n"

  if [ ! -f "${BINFILE}/mateix" ];
    then
      sudo wget -O "${BINFILE}/mateix" https://github.com/ramantehlan/mateix/raw/master/mateix
      sudo chmod +x "${BINFILE}/mateix"
  fi

  if [ ! -f "${BINFILE}/mateixWatch" ];
    then
      sudo wget -O "${BINFILE}/mateixWatch" https://raw.githubusercontent.com/ramantehlan/mateix/master/service/mateixWatch
      sudo chmod +x "${BINFILE}/mateixWatch"
  fi

  if [ ! -f "${SERVICEFILE}/mateix-watch.service" ];
    then
    sudo wget -O "${SERVICEFILE}/mateix-watch.service" https://raw.githubusercontent.com/ramantehlan/mateix/master/service/mateix-watch.service
    sudo chmod +rw "${SERVICEFILE}/mateix-watch.service"
    sudo systemctl daemon-reload
    sudo systemctl enable mateix-watch.service
  fi

echo "'${BINFILE}/mateix' created"
echo "'${BINFILE}/mateixWatch' created"
echo "'${SERVICEFILE}/mateix-watch.service' created"

echo "\n${GREEN}✔ Task 2 Complete!${NC}\n"
echo "\n${GREEN}- Executing Task 3${NC}\n"

if [ ! -d $ROOTDOTFILE ];
  then
    sudo mkdir $ROOTDOTFILE
    sudo touch "${ROOTDOTFILE}/syncList"
fi

echo "'${ROOTDOTFILE}' created"
echo "'${ROOTDOTFILE}/syncList' created"

printf "\n${GREEN}✔ Task 3 Complete!${NC}\n"