#!/usr/bin/env bash

PANILUX_JUST_UPDATE=$1
PANILUX_EL7=$2
SHOW_SPINNER=$3
DEFAULT_YES=$4

if
  [[ ${PANILUX_JUST_UPDATE} ]] &&
    [[ ${PANILUX_JUST_UPDATE} -eq '1' ]]
then
  PANILUX_JUST_UPDATE=1
else
  PANILUX_JUST_UPDATE=0
fi

if
  [[ ${PANILUX_EL7} ]] &&
    [[ ${PANILUX_EL7} -eq '1' ]]
then
  PANILUX_EL7=1
else
  PANILUX_EL7=0
fi

if
  [[ ${SHOW_SPINNER} ]] &&
    [[ ${SHOW_SPINNER} -eq '0' ]]
then
  SHOW_SPINNER=0
else
  SHOW_SPINNER=1
fi

if
  [[ ${DEFAULT_YES} ]] &&
    [[ ${DEFAULT_YES} -eq '1' ]]
then
  DEFAULT_YES=1
else
  DEFAULT_YES=0
fi

export INSTALLER_LANG="en"

export TEXT_FAIL="FAIL"
export TEXT_OK="OK  "
export TEXT_INSTALLED="INSTALLED"
export TEXT_DONE="DONE"
export TEXT_ADDED="ADDED"
export TEXT_FOUND="FOUND"
export TEXT_Error="Error"

export TEXT_Restarting="Restarting"
export TEXT_Starting="Starting"
export TEXT_Stopping="Stopping"
export TEXT_Updating_CentOS="Updating Operating System... "
export TEXT_YUM_cleaning="Cleaning Package Manager caches... "

export TEXT_Installation_aborted="Installation aborted."
export TEXT_Already_downloaded="Already downloaded"
export TEXT_Panilux_is_supported_Centos_6="Panilux is supported Centos 6"
export TEXT_Panilux_is_supported_Centos_6_and_7="Panilux is supported Centos 6 and 7"
export TEXT_Panilux_is_supported_Only_64bit_servers="Panilux is supported only 64bit servers"
export TEXT_Welcome_to_Panilux_installer="Welcome to Panilux installer"
export TEXT_Please_answer_Yes_or_No="Please answer [Y]es or [N]o."
export TEXT_Do_you_wish_to_install_Panilux_Y_or_N="Do you wish to install Panilux? Y or N"
export TEXT_Panilux_is_already_installed="Panilux is already installed. All files will be deleted. Do you wish to install Panilux? Y or N"

if [[ ${PANILUX_JUST_UPDATE} -eq 1 ]]; then
  export TEXT_YUM_configuring="Reconfiguring Package Manager... "
  export TEXT_Adding_Repository="Checking Repository:"
  export TEXT_Installing="Checking"
  export TEXT_Installing_libraries="Updating libraries"
else
  export TEXT_YUM_configuring="Configuring Package Manager... "
  export TEXT_Adding_Repository="Adding Repository:"
  export TEXT_Installing="Installing"
  export TEXT_Installing_libraries="Installing libraries"
fi

export TEXT_Updating_time="Updating time..."
export TEXT_Creating_Panilux_environment="Creating Panilux environment..."
export TEXT_Downloading_latest_Panilux="Downloading latest Panilux..."
export TEXT_Cleaning_Panilux_files="Cleaning Panilux files..."
export TEXT_Extracting_Panilux_files="Extracting Panilux files"
export TEXT_Disabling_SELINUX="Disabling SELINUX..."
export TEXT_Stopping_iptables="Stopping iptables..."
export TEXT_Creating_Panilux_services="Creating Panilux services..."
export TEXT_Creating_certificates="Creating certificates (this may take 3 min)..."
export TEXT_Checking_Panilux="Checking Panilux..."
export TEXT_Starting_Panilux="Starting Panilux..."
export TEXT_Configuring_PHP_ini="Configuring PHP.ini..."
export TEXT_Configuring_Mail_service="Configuring Mail service..."
export TEXT_Configuring_MySQL_service="Configuring MySQL service..."
export TEXT_Configuring_Mongo_service="Configuring Mongo service..."
export TEXT_Configuring_Mongo_user="Configuring Mongo user..."
export TEXT_Panilux_is_installed_successfuly="Panilux is installed successfuly"
export TEXT_Please_visit_Panilux_Panel_to_finalize_your_installation="Reboot your server and after that visit Panilux Panel to finalize your installation"

export TEXT_was_not_detected="was not detected"
export TEXT_Release_is="CentOS version"
export TEXT_This_platform_is_no_longer_supported="This platform is no longer supported!"
export TEXT_Exiting="Exiting"
export TEXT_Unable_to_determine_distribution_type="Unable to determine distribution type. Please send the contents of /etc/redhat-release to support@panilux.com"
export TEXT_Yum_was_not_detected="Yum was not detected. Contact your provider for support."

export TEXT_Installing_Apache_mod_spdy="Installing Apache mod_spdy... "
export TEXT_Installing_Apache_mod_pagespeed="Installing Apache mod_pagespeed... "
export TEXT_Installing_WordPress_CLI="Installing WordPress CLI... "

export TEXT_Adding_Panilux_to_sudoers="Adding Panilux to sudoers"
export TEXT_Configuring_Network="Configuring Network..."

export TEXT_Creating_NGINX_dhparam="Creating NGINX dhparam(2048bit)..."
export TEXT_Cant_activate_quota_on_xfs_disk_Please_reboot_server_before_proceeding_to_setup="Can't activate quota on XFS disk. Please, reboot server before proceeding to setup."
export TEXT_Important_Warning="Important Warning"

if [[ ! $RANDOM ]]; then
    RANDOM=$(date +%s):
fi

bash <(curl -Ss "https://update.panilux.com/download/install_panilux_core.sh?_random={$RANDOM}") ${PANILUX_JUST_UPDATE} ${PANILUX_EL7} ${SHOW_SPINNER} ${DEFAULT_YES}
