#!/bin/bash

LOCKFILE="/var/lock/$(basename "$0").lock"
exec 200>"$LOCKFILE"
flock -n 200 || { echo "Another instance is already running." && exit ; }

# check if we're running this on a cPanel server
if ! [[ -f '/usr/local/cpanel/version' ]]; then
  echo "Not a cPanel server; exitting!"
fi

# enable ioncube
/usr/sbin/whmapi1 set_tweaksetting key='phploader' value='ioncube'

cpanel_internal_php="$(find /usr/local/cpanel/3rdparty/php/ -name php | grep bin)"
softaculous_cpanel_path='/usr/local/cpanel/whostmgr/docroot/cgi/softaculous/cli.php'
tmp_installer_path='/usr/src//install.sh'
softaculous_install_cron='/etc/cron.d/softaculous_install'

if ! [[ -f "${softaculous_cpanel_path}" ]] || ! "${cpanel_internal_php}" "${softaculous_cpanel_path}" -v ; then
  wget http://files.softaculous.com/install.sh -O "${tmp_installer_path}"
  bash "${tmp_installer_path}" --nocpphp --quick
  rm -f "${tmp_installer_path}"
  if ! "${cpanel_internal_php}" "${softaculous_cpanel_path}" -v; then
    echo "Softaculous Install failed!"
  else
    if [[ -f "${softaculous_install_cron}" ]]; then
      rm -f "${softaculous_install_cron}"
    fi
    echo "Softaculous Install Success!!"
  fi
fi
