#!/bin/env bash
# Install cPanel/WHM on Clean CentOS               (c) 2015 SolidNetwork, Inc.
# ----------------------------------------------------------------------------
# This shell script is a wrapper script to install cPanel/WHM on a clean setup
# of CentOS.
#
# @author       Adam Brenner   <adam@netops.me>
# @version      1.0
# @date         07/2015

# Global Variables
VERSION=1.0
TITLE="cPanel/WHM Installer for CentOS"
REPO="https://netsec.netops.me/_source/cpanel/install"
SOURCE=(${REPO}/latest.tgz ${REPO}/latest.tgz.md5)
STMPDIR=""

##
# Run a generic command and check its return code for non-zero.
#
function runCommand() {

    # Runs the command
    $1

    local exitStatus=$?
    if [ $exitStatus -ne 0 ]; then
        # exitStatus of 126 is permission problem
        # exitStatus of 127 is command not found (check path)
        local error="FAILED: $1 produced exit code $exitStatus"
        echo $error
        exit $exitStatus
    fi

    return $exitStatus
}

##
# Download Files
#
function getSource() {

    for i in ${SOURCE[@]}; do
        runCommand "curl -O -s ${i}"
    done;
}

##
# Checksum Downloads
#
function verifySource() {

    runCommand "md5sum --check --quiet *.md5"
}

##
# Clean Up Secure TMP
#
function cleanUp() {

    if [ -d "${STMPDIR}" ] ; then
        runCommand "rm -rf ${STMPDIR}"
    fi
}

##
# Pre Flight steps for dealing with packages. This includes downloading
# and performing m5 check sums.
#
function preFlight() {
    # Create a secure working location using mkstemp(3)
    # TODO use fifo (named pipes)
    STMPDIR=$(runCommand "mktemp -d --suffix=.netops")
    runCommand "cd ${STMPDIR}"

    getSource
    verifySource

}

##
# Install
#
function build() {

    # Extract and Install
    runCommand "tar -xzf latest.tgz"

    # Disable Selinux for this session
    #runCommand "setenforce 0"
    # Disable Selinux after reboots
    runCommand "sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config"

    # Add defaults to system
    runCommand "cp -R root etc /";

    # Install cPanel
    runCommand "cd /home";
    runCommand "curl -s -o latest -L http://httpupdate.cpanel.net/latest";
    runCommand "sh latest | tee /root/cpanel-install-netops.log";

}

##
# Help Menu
#
function helpMenu() {
    cat << END

  Usage: $0 [OPTIONS]
Version: $VERSION
 Author: SolidNetwork, Inc.
    Web: SolidNetSupport.com || SolidNetDC.com

    General Commands:
    ---------------------------------------------------------------------------
    -h,--help       : Print this help screen
    -v,--version    : Print this program's version

    Program Commands:
    ---------------------------------------------------------------------------
    -i,--install    : This will install cPanel/WHM on the server.

END

}

case "$1" in
    --install|-i)
        preFlight
        build
        cleanUp
        exit $?
    ;;
   --version|-v)
        echo ${TITLE} ${VERSION}
        exit $?
   ;;
   *)
        helpMenu
        exit $?
   ;;
esac
exit 0
