#!/bin/bash

echo -e "   _  __                __\n  | |/ /___ _____ ___  / /___ _\n  |   / __ \`/ __ \`__ \/ / __ \`/\n /   / /_/ / / / / / / / /_/ /\n/_/|_\__,_/_/ /_/ /_/_/\__,_/\n\nROSVITA Installer v0.1\n"

echo "Before installing ROSVITA, please read the ROSVITA license agreement:"
echo "https://docs.xamla.com/rosvita/downloads/LICENSE"
read -p "Press ENTER to continue or CTRL+C to cancel"
echo ""
echo "In case of any problems with the installation please visit: https://discuss.xamla.com/"
echo ""

echo "Step 1:"
echo "======="
echo "Docker will be installed if not already there, press CTRL+C to cancel"
read -p "Press ENTER to continue and follow the instructions on screen"

if [[ $(docker -v | grep -c 'Docker version') > 0 ]]; then
   echo "Docker is already installed."
else
   echo "Starting installation of Docker.."
   sudo apt-get remove docker docker-engine docker.io
   sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
   curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
   sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
   sudo apt-get update
   sudo apt-get install -y docker-ce
   if [[ $(sudo docker -v | grep -c 'Docker version') > 0 ]]; then
      echo "Docker CE has been installed successfully."
   else
      echo "Docker installation failed. Please try again."
      exit 1
   fi
   echo "Adding user to group docker.."
   sudo usermod -aG docker $USER
fi
echo ""

echo "Step 2:"
echo "======="
echo "The ROSVITA image will be downloaded from Docker Hub, press CTRL+C to cancel"
read -p "Press ENTER to continue"
echo "Getting ROSVITA from Docker Hub.."
sudo docker pull xamla/rosvita:latest
if [[ $(echo $?) != 0 ]]; then
   echo "Could not download latest ROSVITA image. Please check your internet connection and disk space."
   echo "If the problem does not go away, we are happy to help you at http://discuss.xamla.com."
   exit 1
fi
if [[ $(sudo docker images | grep -c 'xamla/rosvita') == 0 ]]; then
   echo "Could not download ROSVITA image. Please check your internet connection and disk space."
   echo "If the problem does not go away, we are happy to help you at http://discuss.xamla.com."
   exit 1
else
   echo "ROSVITA image has been downloaded successfully."
   echo "Use 'docker rmi <image-id>' to remove old images and save disk space."
fi
echo ""

echo "Step 3 and 4:"
echo "============="
echo "Creating scripts '/opt/rosvita/rosvita_start' and '/opt/rosvita/rosvita_stop' for starting and stopping ROSVITA.."
cd /opt
if [[ $(ls | grep -x rosvita | wc -l) == 0 ]]; then
   sudo mkdir rosvita
   sudo chown -R $USER /opt/rosvita/
   cd rosvita
   wget -q https://docs.xamla.com/rosvita/downloads/rosvita_start
   sudo chmod a+x rosvita_start
   wget -q https://docs.xamla.com/rosvita/downloads/rosvita_stop
   sudo chmod a+x rosvita_stop
else
   cd rosvita
   if [[ $(ls | grep -x rosvita_start | wc -l) == 0 ]]; then
      wget -q https://docs.xamla.com/rosvita/downloads/rosvita_start
      sudo chmod a+x rosvita_start
   else
      echo "ROSVITA start script already exists."
   fi
   if [[ $(ls | grep -x rosvita_stop | wc -l) == 0 ]]; then
      wget -q https://docs.xamla.com/rosvita/downloads/rosvita_stop
      sudo chmod a+x rosvita_stop
   else
      echo "ROSVITA stop script already exists."
   fi
fi
if [[ $(echo $PATH | grep /opt/rosvita | wc -l) == 0 ]]; then
   echo "Add path /opt/rosvita to your PATH variable in ~/.bashrc"
   echo 'export PATH=$PATH:/opt/rosvita' >> ${HOME}/.bashrc
fi
source ${HOME}/.bashrc
echo ""

echo "Step 5:"
echo "======="
echo "Creating desktop icon for starting ROSVITA.."
cd ${HOME}/Desktop
if [[ $(ls | grep -x ROSVITA.desktop | wc -l) > 0 ]]; then
   echo "ROSVITA desktop icon already exists."
else
   cd /opt/rosvita
   wget -q https://docs.xamla.com/rosvita/downloads/rosvita_icon.png
   cd ${HOME}/Desktop
   wget -q https://docs.xamla.com/rosvita/downloads/ROSVITA.desktop
   sudo chmod a+x ROSVITA.desktop
fi
echo "Finished."
echo ""

echo "Step 6:"
echo "======="
if [[ $(groups | grep docker | wc -l) > 0 ]]; then
   echo "To start ROSVITA simply double click the desktop icon."
   echo "If you have any trouble or would like to give us feedback, feel free to visit: https://discuss.xamla.com/"
else
   echo "IMPORTANT:"
   echo "The computer will be restarted now to make the addition to the docker group effective."
   echo "Afterwards you can start ROSVITA by simply double clicking the desktop icon."
   echo "If you have any trouble or would like to give us feedback, feel free to visit: https://discuss.xamla.com/"
   read -p "Press ENTER to restart computer"
   sudo reboot
fi

