#!/bin/sh -e

##########################################################################
#   Script description:
#       Bootstrap a SPCM head node.
#       Install SPCM and advise user to run cluster-setup after reboot
#
#       This script should have zero dependencies, as it will be run on
#       pristine OS installs
#
#   Arguments:
#       None
#
#   History:
#   Date        Name        Modification
#   2018-10-01  Jason Bacon Begin
##########################################################################

usage()
{
    printf "Usage: $0\n"
    exit 1
}


##########################################################################
#   Function description:
#       Pause until user presses return
##########################################################################

pause()
{
    local junk
    
    printf "Press return to continue..."
    read junk
}


##########################################################################
#   Main
##########################################################################

if [ $# != 0 ]; then
    usage
fi

if [ `whoami` != root ]; then
    printf "$0 must be run by root.\n"
    exit 1
fi

if [ `uname` = FreeBSD ]; then
    pkg install -y auto-admin
    auto-pkg-latest --no-reboot --yes

    if [ ! -e /usr/ports/CHANGES ]; then
	auto-ports-checkout
    fi
    
    # ca_root_nss needed for github certs
    pkg install -y spcm
    cat << EOM

Run "cluster-node-manager" and choose option 1, "Configure head node", to get
started.

EOM
    
elif [ -e /etc/redhat-release ]; then

    yum install -y gcc-gfortran gcc-c++ gcc java-openjdk8-headless
    if [ ! -e /usr/pkg/bin/sbmake ]; then
	curl -O --insecure https://acadix.biz/joyent-pkgsrc
	sh joyent-pkgsrc
    fi
    if ! which cluster-setup; then
	cd /usr/pkgsrc/wip/spcm
	bmake clean 
	bmake install
	cd $save_cwd
    fi
    auto-update-system --defaults

    cat << EOM

The system will now reboot.

Run 'cluster-setup head' following reboot.

EOM
    pause
    shutdown -r now

else

    printf "Unsupported operating system.\n"
    exit 1

fi
