#!/bin/bash

# VARIABLES
DOWNLOAD_LINK="https://jovast.com/apps/jovast/install/download.php"
# DOWNLOAD_LINK="192.168.1.211/jovast/install/download.php"
INSTALL_DIR="/var/www/html"
HOME_DIR="/home/jovast"
BACKUPS_DIR="/home/jovast/backups"
SCRIPTS_DIR="/home/jovast/scripts"

# CHECK IF HOME DIRECTORY EXISTS
if [ -d $HOME_DIR ] 
then
    apt-get update
    printf "\nInstalling required packages...\n"
    apt install zip curl openssh-server apache2 mysql-server php libapache2-mod-php php-mysql php-zip php-xml php-xml php-mbstring php-gd php-mysql php-curl php-imagick php-bz2 phpmyadmin net-tools -y
else
    echo "Error: Required directory "$HOME_DIR" does not exist. Please create user jovast with home directory "$HOME_DIR"."
    exit
fi

# DOWNLOAD INSTALLATION PACKAGE FROM JOVAST.COM
printf "\nDownloading installer from www.jovast.com... \n"
wget $DOWNLOAD_LINK -O installer.zip
echo "Done";

# EXTRACT DOWNLOADED FILES
printf "\nExtracting installer.zip... \n"
unzip -o -qq installer.zip -d installer
echo "Done";

# UPDATING CONFIGURATION FILES
printf "\nUpdating apache, mysql and ssl configuration files... \n"
cp -R installer/etc/* /etc/
echo "Done";

# RESTART SERVICES
printf "\nRestaring apache2 and mysql services...\n"
service apache2 restart
service mysql restart
echo "Done";

# COPY HTML FOLDER FROM INSTALLER DIR TO WWW DIR
printf "\nCopying html folder from $HOME_DIR to $INSTALL_DIR... \n"
cp -R installer/html/* $INSTALL_DIR/
rm $INSTALL_DIR/index.html
echo "Done";

# COPY BACKUP, SCRIPTS AND LOGS FOLDER FROM INSTALLER TO HOME
printf "\nCopying backups, scripts and logs folder from installer to $HOME_DIR... \n"
cp -R installer/backups $HOME_DIR/
cp -R installer/scripts $HOME_DIR/
cp -R installer/logs $HOME_DIR/
echo "Done";

# CHANGE PERMISSIONS OF HOME AND WWW FOLDER
printf "\nChanging permissions of $INSTALL_DIR to 777... \n"
chmod -R 777 $INSTALL_DIR
chmod -R 777 *
echo "Done";

# CREATE CLIENT ID
printf "\nCreating client id: Client id must be unique please ask Jovast Informatics for client_id... \n"
read -p "Enter client id (One word small letters only without space): " CLIENT_ID
echo "<?php die() ?>
emedical=$CLIENT_ID" > $INSTALL_DIR/jovast/client.php
mv $INSTALL_DIR/jovast/clients/emedical $INSTALL_DIR/jovast/clients/$CLIENT_ID
sed -i -e "s/CLIENT=emedical/"CLIENT=$CLIENT_ID"/g" /home/jovast/scripts/jovast-remote-backup.sh
echo "Done";

# IMPORT MYSQL DATABASE
printf "\nCreating emedical database ...\n"
mysql -e "DROP DATABASE IF EXISTS emedical_$CLIENT_ID;"
mysql -e "CREATE DATABASE emedical_$CLIENT_ID CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
echo "Done";

printf "\nCreating emedical database user ...\n"
mysql < $HOME_DIR"/installer/database/user.sql"
echo "Done";

printf "\nImporting emedical database ...\n"
echo "USE emedical_$CLIENT_ID; source $HOME_DIR/installer/database/emedical.sql;" > $HOME_DIR/installer/database/import.sql
mysql < $HOME_DIR"/installer/database/import.sql"
echo "Done";

# ALLOW APACHE AND SSH THROUGH FIREWALL
printf "\nAllowing apache and ssh through firewall... \n"
ufw allow 'Apache Full'
ufw allow ssh
ufw allow 3000
ufw allow 4242
ufw allow 8042
ufw reload
echo "Done";

# INSTALLING CRONTAB
printf "\nInstalling backups and updates cron jobs... \n"

UPDATES_COMMAND="wget localhost/emedical/live/auto/index.php -O - >> /home/jovast/logs/auto.log"
UPDATES_JOB=" * * * * * $UPDATES_COMMAND"
cat <(fgrep -i -v "$UPDATES_COMMAND" <(crontab -l)) <(echo "$UPDATES_JOB") | crontab -

REMOTE_COMMAND="/home/jovast/scripts/jovast-remote-backup.sh >> /home/jovast/logs/jovast-remote-backup.log"
REMOTE_JOB=" 50 00 * * * $REMOTE_COMMAND"
cat <(fgrep -i -v "$REMOTE_COMMAND" <(crontab -l)) <(echo "$REMOTE_JOB") | crontab -

BACKUPS_HOURLY_COMMAND="/home/jovast/scripts/mysqldump-hourly.sh >> /home/jovast/logs/mysqldump-hourly.log"
BACKUPS_HOURLY_JOB=" 15 * * * * $BACKUPS_HOURLY_COMMAND"
cat <(fgrep -i -v "$BACKUPS_HOURLY_COMMAND" <(crontab -l)) <(echo "$BACKUPS_HOURLY_JOB") | crontab -

BACKUPS_DAILY_COMMAND="/home/jovast/scripts/mysqldump-daily.sh >> /home/jovast/logs/mysqldump-daily.log"
BACKUPS_DAILY_JOB=" 30 00 * * * $BACKUPS_DAILY_COMMAND"
cat <(fgrep -i -v "$BACKUPS_DAILY_COMMAND" <(crontab -l)) <(echo "$BACKUPS_DAILY_JOB") | crontab -

BACKUPS_MONTHLY_COMMAND="/home/jovast/scripts/mysqldump-monthly.sh >> /home/jovast/logs/mysqldump-monthly.log"
BACKUPS_MONTHLY_JOB=" 45 00 28,29,30 * * $BACKUPS_MONTHLY_COMMAND"
cat <(fgrep -i -v "$BACKUPS_MONTHLY_COMMAND" <(crontab -l)) <(echo "$BACKUPS_MONTHLY_JOB") | crontab -
echo "Done";

# CREATING SYMBOLIC LINK
printf "\nCreating emedical symbolic link... \n"
ln -s "/var/www/html/jovast" "/var/www/html/emedical"
echo "Done";

# REMOVE INSTALLER
rm -r *install*

printf "\nAll done! \neMedical installation completed! \nVisit http://localhost\n\n"