#!/bin/sh
#
# buildprep - prepare your system for an NTPsec source build.
#
# Use the -n option to dry-run this command, showing what would be done
# without actually doing it

# Set the defaults
DRYRUN="no"
NTPVIZ="no"
DOC="no"

# Loop through option flags
for optflag in "$@"
do
	case "$optflag" in
	 -h|--help)
		cat <<EOF
$0 - prepare your system for an NTPsec source build

  Options:
    -h --help    Show usage information and exit
    -n --dry-run Only show what would be done instead of doing it
       --ntpviz  Install dependencies for ntpviz tool
       --doc     Install dependencies for building documentation
    -a --all     Install all possible dependencies
EOF
		exit 0
		;;
	 -n|--dry-run)
		DRYRUN="yes"
		;;
	 --ntpviz)
		NTPVIZ="yes"
		;;
	 --doc)
		DOC="yes"
		;;
	 -a|--all)
		NTPVIZ="yes"
		DOC="yes"
		;;
	 *)
		echo "# WARNING: Unknown argument: $optflag"
		echo "#"
		;;
	esac
done

cat <<EOF
# Preparing your system for ntpsec source build...
# This script presently knows about:
#   CentOS, Debian, Fedora, Gentoo, SLES and Ubuntu
# If you are running something else, such as macOS or Solaris, please
# read the source for this buildprep script to get an idea of what packages
# are required.
#
EOF

if [ "$DRYRUN" = "yes" ]
then
	do=echo
	echo "# Run this without -n|--dry-run, as root, for actual installation.\n#"
else
	do=""
	if [ "$(id -u)" != 0 ]
	then
		echo "# ERROR: You must be running as root for your installer to do its thing."
		echo "# ERROR: If you just wish to see what would be done, use the -n option."
		exit 1
	fi
fi

if emerge --version 2>/dev/null
then
    installer=emerge
    install="$do $installer -q y"
elif yum version 2>/dev/null
then
    installer=yum
    install="$do $installer install"
elif dnf --version >/dev/null 2>&1
then
    installer=dnf
    install="$do $installer install"
elif apt-get --version >/dev/null 2>&1
then
    installer=apt
    install="$do apt-get install -y"
elif zypper -h >/dev/null 2>&1
then
    # OpenSUSE prefers zypper over yast
    installer=zypper
    install="$do $installer install -y"
elif yast -h >/dev/null 2>&1
then
    installer=yast
    install="$do $installer --install"
else
    echo "# ERROR: Package manager unidentified - Unsupported operating system"
    exit 1
fi
echo "# Your package installer is ${installer}."
echo ""

# In order to have a single point of truth about prerequisite package names,
# these package name lists have been *removed* from INSTALL.

# Build time vs. run time:
# Build dependencies are marked.  You'll need them all when building from a
# repository copy; the unmarked (run-time) dependencies are information for
# packagers.  Under Gentoo, all dependencies are build dependencies.

# Notes on optional packages:
# libdnssd is optional for ntpd. Support for mDNS Service Discovery registration
# libcap (under Linux) enables dropping root and is not strictly required.

daemon () {
    # Prerequisites to build the daemon: bison, pps-tools, service libraries
    case $installer in
	apt)
	    $install build-essential			# Build environment
	    $install bison libssl-dev			# build
	    $install libcap-dev libseccomp-dev		# build
	    $install libavahi-compat-libdnssd-dev	# optional build
	    $install libssl1.0.0 libcap2 pps-tools
	    ;;
	emerge)
						# Build environment included!
	    $install sys-libs/libcap sys-libs/libseccomp
	    $install sys-devel/bison net-misc/pps-tools
	    ;;
	yum|dnf)
	    $do $installer groupinstall "Development Tools" 	# Build environment
	    $install bison openssl-devel 		# build
	    $install libcap-devel libseccomp-devel	# build
	    $install pps-tools-devel			# build
	    $install avahi-compat-libdns_sd-devel	# optional build
	    $install libcap openssl-libs pps-tools
	    ;;
	yast)
	    echo "# SLES versions 12 and earlier do not have pps-tools"
	    $install basis-devel 			# Build environment
	    $install libcap-devel libseccomp-devel 	# build
	    $install openssl-devel			# build
	    $install libcap2 openssl-libs
	    ;;
	zypper)
	    $install -t pattern devel_basis		# Build environment
	    $install bison				# build
	    $install libcap-devel libseccomp-devel	# build
	    $install openssl-devel			# build
	    echo "# SLES versions 12 and earlier do not have pps-tools"
	    $install pps-tools-devel			# build
	    $install pps-tools
	    $install libcap2 openssl-libs
	    ;;
    esac
}

tools () {
    # Prerequisites for the client Python tools: python extensions
    case $installer in
	apt)
	    $install python-dev
	    ;;
	yum|dnf|yast|zypper)
	    $install python-devel
	    ;;
    esac
}

ntpviz () {
    # Prerequisites to use ntpviz: gnuplot and liberation fonts
    case $installer in
	apt)
	    distro=`lsb_release -i -s`
	    if [ "$distro" = "Ubuntu" ]
	    then
		echo "# Looks like an Ubuntu system"
		$install gnuplot5
	    else
		echo "# Looks like a generic Debian system"
		$install gnuplot
	    fi
	    $install fonts-liberation
	    ;;
	emerge)
	    $install sci-visualization/gnuplot
	    $install media-fonts/liberation-fonts
	    ;;
	yum|dnf)
	    $install gnuplot
	    $install liberation-fonts-common.noarch
	    $install liberation-mono-fonts.noarch
	    $install liberation-narrow-fonts.noarch
	    $install liberation-sans-fonts.noarch
	    $install liberation-serif-fonts.noarch
	    ;;
	yast|zypper)
	    $install gnuplot liberation-fonts
	    ;;
    esac
}

doc () {
	# prerequisites to build documentation
	case $installer in
	 apt)
		$install asciidoc
		;;
	 emerge)
		$install app-text/asciidoc
		;;
	 yum|dnf)
		echo "# Please check that your asciidoc is at least 8.6.0"
		echo "# You may need to enable EPEL repositories"
		$install asciidoc
		;;
	 yast|zypper)
		$install asciidoc
		;;
	esac
}

# Main sequence
daemon
tools

if [ "$NTPVIZ" = "yes" ]
then
	ntpviz
else
	echo ""
	echo "# Skipping ntpviz dependencies [--ntpviz]"
fi

if [ "$DOC" = "yes" ]
then
	doc
else
	echo ""
	echo "# Skipping documentation dependencies [--doc]"
fi

echo ""
echo "# Done."
