#!/bin/bash
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

usage() {
        cat << EOF
Usage:  preconfig
	[-a] Install all relevant packages. Exlcluding glusterfs and ovirt
	[-s] Install Samba and CTDB packages
	[-t] Install server cli tools
	[-v] Verbose
	[-z] Istall ZFS packages
	[-f] Do not prompt for install confirmation
	[-h] Display this message
	
EOF
        exit 0
}
status(){
package=$1
rpm -qa | grep -q $package || echo "$package failed to install" >> /var/log/preconfig.log
}
checkupdates(){
yumtmp=/tmp/yum-check-update
yum=/usr/bin/yum
$yum check-update >> $yumtmp
yumstatus=$?
hostname=$(/bin/hostname)
case $yumstatus in
0)
        echo "All packages are up to date"
        echo "Remove Old Kernels"
        package-cleanup -y --oldkernels --count=1
        ;;
*)
        number=$(cat $yumtmp | egrep '(.i386|.x86_64|.noarch|.src)' | wc -l)
        updates=$(cat $yumtmp | egrep '(.i386|.x86_64|.noarch|.src)')
        echo -e "There are $number updates available on host $hostname\n"
        #echo $updates
	echo -e "Installing: updates"
        $yum update $FORCE $QUIET_FLAG
	echo -e "Reboot in 5 seconds\nRerun the same command after reboot to finish install $(basename "$0") $@"
	sleep 5 
	reboot
        ;;
esac
rm -f /tmp/yum-check-update
}

ARCH=$(arch)
SAMBA_FLAG=no
TOOLS_FLAG=no
ZFS_FLAG=no
FORCE=""
QUIET_FLAG="-q"
SKIP_UPDATES="no"

while getopts 'afg:hstuvz' OPTION; do
        case ${OPTION} in
        a)
                SAMBA_FLAG=yes
                TOOLS_FLAG=yes
                ZFS_FLAG=yes
                ;;
        f)
                FORCE="-y"
		QUIET_FLAG=""
                ;;
        h)
                usage
                ;;
        s)
                SAMBA_FLAG=yes
                ;;
        t)
                TOOLS_FLAG=yes
                ;;
	u)
		SKIP_UPDATES=yes
		;;
        z)
                ZFS_FLAG=yes
                ;;
        esac
done
if [ $# -eq 0 ];then
        usage
fi
CENTOS_MAJOR_VERSION=$(cat /etc/centos-release | awk '{print $4}' | cut -d . -f 1)
CENTOS_MINOR_VERSION=$(cat /etc/centos-release | awk '{print $4}' | cut -d . -f 2)
zfsrepo=http://download.zfsonlinux.org/epel/zfs-release.el$"CENTOS_MAJOR_VERSION"_$"CENTOS_MINOR_VERSION".noarch.rpm
repo="http://images.45drives.com"

if [ -e /var/log/preconfig.log ];then
        rm -f /var/log/preconfig.log
        touch /var/log/preconfig.log
else
        touch /var/log/preconfig.log
fi
rpm -qa | grep -qw yum-utils || yum install yum-utils $FORCE $QUIET_FLAG
##check for updates
if [ "$SKIP_UPDATES" == "no" ];then
	checkupdates
	echo "Installing: Required Packages.."
	rpm -qa | grep -qw kernel-devel-$(uname -r) || yum install kernel-devel $FORCE $QUIET_FLAG
	rpm -qa | grep -qw epel || yum install epel-release $FORCE $QUIET_FLAG
	rpm -qa | grep -qw pciutils-3 || yum install pciutils $FORCE $QUIET_FLAG
	rpm -qa | grep -qw vim-enhanced || yum install vim $FORCE $QUIET_FLAG
	rpm -qa | grep -qw ntp || yum install ntp $FORCE $QUIET_FLAG
fi
##disable selinux
sed -ie 's/enforcing/disabled/g' /etc/selinux/config

if [ "$ZFS_FLAG" == "yes" ];then
        echo "Installing: ZFS"
        rpm -qa | grep -qw zfs-release || yum install $zfsrepo $FORCE $QUIET_FLAG
        rpm -qa | grep -qw zfs-dkms || yum install zfs $FORCE $QUIET_FLAG
        if [ ! -d /etc/rc.modules ];then
                touch /etc/rc.modules
        fi
        zmod=$(cat /etc/rc.modules | grep "modprobe zfs" | awk 'NR==1')
        if [ "$zmod" != "modprobe zfs" ];then
                echo "modprobe zfs" >> /etc/rc.modules
        fi
        if [ ! -x /etc/rc.modules ];then
                chmod +x /etc/rc.modules
        fi
        systemctl enable zfs-import-cache.service
        systemctl enable zfs-mount.service
fi
if [ "$SAMBA_FLAG" == "yes" ];then
	echo "Installing: CTDB - SAMBA - SAMBA GFS VFS"
        rpm -qa | grep -qw ctdb || yum install ctdb $FORCE $QUIET_FLAG
        rpm -qa | grep -qw samba-common-4 || yum install samba-common $FORCE $QUIET_FLAG
        rpm -qa | grep -qw samba-4 || yum install samba $FORCE $QUIET_FLAG
        rpm -qa | grep -qw samba-winbind-clients || yum install samba-winbind-clients $FORCE $QUIET_FLAG
        rpm -qa | grep -qw samba-client || yum install samba-client $FORCE $QUIET_FLAG
fi
if [ "$TOOLS_FLAG" == "yes" ];then
	echo "Installing: Server CLI tools"
	rpm -qa | grep -qw python-pip || yum install python2-pip $FORCE $QUIET_FLAG
        rpm -qa | grep -qw ansible || yum install ansible $FORCE $QUIET_FLAG
        rpm -qa | grep -qw 45drives-tools || yum install https://github.com/45Drives/tools/releases/download/v1.1/45drives-tools-1.1-2.el7.noarch.rpm $FORCE $QUIET_FLAG
	pip list --format=columns 2> /dev/null | grep -qw pip || pip install --upgrade pip 	
fi

echo -e "All Done...\nVerifying Install..."
status kernel-devel-$(uname -r)
status epel
status pciutils-3
status vim
status ntp
if [ "$ZFS_FLAG" == "yes" ];then
        status zfs-release
        status zfs
        zmount=$(systemctl status zfs-mount | awk NR==2'{print $4}' | cut -d ';' -f 1)
        zimport=$(systemctl status zfs-import-cache | awk NR==2'{print $4}' | cut -d ';' -f 1)
        if [ "$zmount" != "enabled" ];then
                "zfs-mount.service was not enabled" >> /var/log/preconfig.log
        fi
        if [ "$zimport" != "enabled" ];then
                "zfs-import-cache.service was not enabled" >> /var/log/preconfig.log
        fi
fi
if [ "$SAMBA_FLAG" == "yes" ];then
        status ctdb
        status samba-common-4
        status samba-4
        status samba-winbind-clients
        status samba-client
fi
if [ "$TOOLS_FLAG" == "yes" ];then
        status ansible
        status 45drives-tools
fi

state=$(cat /var/log/preconfig.log | wc -l)
case $state in
0)
        echo -e "${GREEN}SUCCESS${NC}"
        echo "Reboot required before continuing setup. Rebooting automatically in 5 seconds"
	sleep 5
	reboot

        ;;
*)
        echo -e "${RED}FAILURE${NC}"
        echo -e "Problems during installation: $state"
        cat /var/log/preconfig.log
        echo -e "Rerun this script or manually try to install missing packages individually\nNOTE if installing the ZFS packages use \"yum install zfs\" rather than \"yum install zfs-dkms\""
        ;;
esac
