#!/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

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
# follow the directions for by-hand installation in INSTALL.
#
EOF

if [ "$1" = "-n" ]
then
    do=echo
    echo "# Run this without -n, as root, for actual installation.\n#"
else
    do=""
fi

if [ "$do" = "" -a `id -u` != 0 ]
then
    echo "#You must be running as root for your installer to do its thing."
    exit 1
fi

inst=""
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
    installer=unknown
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 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)
	    $install sys-libs/libcap sys-libs/libseccomp
	    $install sys-devel/bison net-misc/pps-tools
	    ;;
	yum|dnf)
	    $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
	    $install libcap-devel libseccomp-devel 	# build
	    $install openssl-devel			# build
	    $install libcap2 openssl-libs
	    ;;
	zypper)
	    $install gcc6 gcc6-info gcc6-locale	# build
	    $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
}

# Main sequence
daemon
tools
ntpviz

echo ""
echo "# Done."
