#/bin/bash

if [ "$1" == "devel" ]; then
   INSTALL_DEVEL=1
else
   INSTALL_DEVEL=0
fi

# dir with meta informatou about installation
INSTALL_METADATA_DIRECTORY=".install"

WEBSITE_DOMAIN_FILE=$INSTALL_METADATA_DIRECTORY"/.website_domain"
WEBSITE_PARENT_FILE=$INSTALL_METADATA_DIRECTORY"/.website_parent"
DB_INITIALIZED_FILE=$INSTALL_METADATA_DIRECTORY"/.db_initialized"
THEME_SET_FILE=$INSTALL_METADATA_DIRECTORY"/.theme_set"
ADMIN_CREATED_FILE=$INSTALL_METADATA_DIRECTORY"/.admin_created"
USER_INSTALLED_FILE=$INSTALL_METADATA_DIRECTORY"/.user"


INSTALL_DIR_EXISTS=0
while [ "$INSTALL_DIR" == "" ]; do
   echo ""
   echo "Install directory: "
   read INSTALL_DIR

   if [ -d $INSTALL_DIR ]; then
      echo "Directory $INSTALL_DIR already exists."

      cd $INSTALL_DIR

      if [ -d ".git" ]; then
         # try to continue in installation when directory exists
         git remote -v |grep "minicms.git" > /dev/null 
         if [ $? -eq 0 ]; then
            echo " ... continuing in installation"
            INSTALL_DIR_EXISTS=1
         else
            echo " ... this directory does not contain installation of CMS! Use different directory."
            INSTALL_DIR=""
         fi
      fi

      cd - > /dev/null
   fi
   
   echo ""
done

# create installation directory for this instance of cms
if [ $INSTALL_DIR_EXISTS -eq 0 ]; then
   mkdir -p $INSTALL_DIR
fi

cd $INSTALL_DIR

# setup website info
if [ ! -e $WEBSITE_DOMAIN_FILE ]; then
   echo ""
   echo "Website domain: "
   read WEBSITE_DOMAIN
   echo ""
else
   WEBSITE_DOMAIN=`cat $WEBSITE_DOMAIN_FILE`
fi

if [ ! -e $WEBSITE_PARENT_FILE ]; then
   echo ""
   echo "Website parent (branch in minicms.git repository): "
   read WEBSITE_PARENT
   echo ""
else
   WEBSITE_PARENT=`cat $WEBSITE_PARENT_FILE`
fi

# fetch data from server
if [ ! -n "$(ls -A)" ]; then
   git clone git@binarity.cz:/opt/git/internal/cms/minicms.git -b $WEBSITE_PARENT .

   if [ ! $? -eq 0 ]; then
      echo ""
      echo " -------------------------------------------"
      echo "ERROR: cannot clone repository. Aborting"
      echo ""

      exit 2
   fi

   # ignore file permission changes
   git config core.fileMode false

else
   git branch |grep $WEBSITE_DOMAIN > /dev/null

   if [ $? -eq 0 ]; then
      echo "CMS already installed!"
      exit 1
   fi
fi

if [ ! -e $INSTALL_METADATA_DIRECTORY ]; then
   mkdir $INSTALL_METADATA_DIRECTORY
fi

# save variables
echo $WEBSITE_DOMAIN > $WEBSITE_DOMAIN_FILE
echo $WEBSITE_PARENT > $WEBSITE_PARENT_FILE

# setup permissions
chmod 0777 bootstrap/cache -R
chmod 0777 storage -R
chmod 0777 resources/views/visitor/$TEMPLATE_NAME -R
chmod 0777 modules/*
chmod 0777 public/uploads -R

# copy settings if neccesarry
if [ ! -e .env ]; then
   cp .env.example .env

   # edit config
   vim .env
fi

# create autoload files - no matter what
composer dump-autoload

# install dependencies - no matter what
if [ $INSTALL_DEVEL -eq 0 ]; then
   composer install --no-dev
else
   composer install
fi

# init database structure
if [ ! -e $DB_INITIALIZED_FILE ]; then
   # init, migrate and seed database
   php artisan db:init ./database/schema.sql &&
   php artisan migrate &&
   php artisan db:seed &&
   touch $DB_INITIALIZED_FILE

   if [ ! $? -eq 0 ]; then
      echo ""
      echo " ---------------------------------------------- "
      echo "ERROR: Database was not initialized. Try to fix this error and then rerun installation."
      echo ""
      exit 3
   fi
fi



# generate random key for ciphering 
php artisan key:generate

if [ ! -e $THEME_SET_FILE ]; then
   echo " --------------------------"
   echo " * Theme setup * "
   echo ""


   while [ "$THEME_NAME" == "" ]; do
      echo "Template name (use any of following): "
      for i in $(ls -d resources/views/visitor/*/); do
         basename `echo ${i%%/}`;
      done

      read THEME_NAME

      if [ ! -d resources/views/visitor/$THEME_NAME ]; then
         echo "Theme $THEME_NAME is not valid. Try again"
         THEME_NAME=""
      fi
   done


   # setup theme
   php artisan theme:set $THEME_NAME

   echo $THEME_NAME > $THEME_SET_FILE
fi

if [ ! -e $ADMIN_CREATED_FILE ]; then
   echo ""
   echo " ------------------------------ "
   echo " *  CREATE ADMIN USER  *"
   echo ""

   echo "New user email: "
   read EMAIL
   echo ""

   echo "Password: "
   read PASSWORD
   echo ""

   # install new user
   php artisan user:new $EMAIL $PASSWORD admin

   echo $EMAIL > $ADMIN_CREATED_FILE
fi

# create new branch in git
git checkout -b $WEBSITE_DOMAIN
#~ git push origin $WEBSITE_DOMAIN

# save info about user, who installed this source copy
echo $USER > $USER_INSTALLED_FILE

echo ""
echo ""
echo ""
echo ""
echo ""

echo "           INSTALLATION SUCCESSFULL."
echo ""

echo "Do not forget to install desired modules!"
echo " Use php artisan install-module <ModuleName> or php artisan install-all-modules"