#!/bin/sh
#
# buildprep - prepare your system for a cvs-fast-export source build.
#
# Use the -n option to dry-run this command, showing what would be done
# without actually doing it
#
# This script swiped from NTPsec.
#
# SPDX-FileCopyrightText: Copyright Eric S. Raymond <esr@thyrsus.com>
# SPDX-License-Identifier: BSD 2-Clause

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

OS=$(uname -s)

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

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

cat <<EOF
# Preparing your system for cvs-fast-export source build...
# This script presently knows about:
#   Ubuntu (and POP_OS), Fedora using yum, Fedora using dnf, Gentoo.
#
# It has some stub code for other systems; please fill that in if you
# can and send us an MR.
#
# 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."
    echo " #You may want to run an update from your package index first."
    echo "#"
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 -y install"
elif dnf --version >/dev/null 2>&1
then
    installer=dnf
    install="$do $installer -y install"
elif apt-get --version >/dev/null 2>&1
then
    installer=apt
    install="$do apt-get install -qy"
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"
elif  apk --version >/dev/null 2>&1
then
    # Alpine Linux, musl rather than glibc
    installer=apk
    install="$do $installer add"
elif command -v pacman
then
    # Arch Linux
    installer=pacman
    install="$do $installer -S --needed --noconfirm"
elif [ "$OS" = "NetBSD" ]
then
    if pkgin -v
    then
        # NetBSD binary package installer
        installer=pkgin
        install="$do $installer install"
    else
        echo "## Looks like a NetBSD system"
        echo "## You need to setup pkgin"
        echo "## The last page of install disk has a check-box to do it"
        echo "## But you don't get that option on a Raspberry Pi."
        echo "## For the Pi, do something like:"
        echo "## pkg_add ftp://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/earmv7hf/8.0/All/pkgin-0.9.4nb8.tgz"
        echo "## Adjust the version and arch to match your setup."
        exit 1
    fi
elif [ "$OS" = "FreeBSD" ]
then
    if pkg -v
    then
        # FreeBSD binary package installer
        installer=pkg
        install="$do $installer install"
    fi
else
    echo "# ERROR: Package manager unidentified - Unsupported operating system"
    exit 1
fi
echo "# Your package installer is ${installer}."
echo ""

main () {
    # Prerequisites to build the daemon: bison, pps-tools, service libraries
    case $installer in
    apk)
        echo "Not yet supported" >&2
        exit 1
        ;;
    apt)
        # tzdata needs to be installed explicitly so it won't try an interactive
        # configuration when pulled as a dependency.  This package doesn't care
        # whether your Python is 2.x or 3.x.
        $install tzdata
        $install make grep sed gcc bison flex python3 git rcs cvs pylint cppcheck shellcheck asciidoctor
        ;;
    emerge)
        echo "Not yet supported" >&2
        exit 1
        ;;
    pacman)
        $install tzdata
        $install make grep sed gcc bison flex python git rcs cvs \
        python-pylint cppcheck shellcheck
        ;;
    pkgin)
        echo "Not yet supported" >&2
        exit 1
        ;;
    pkg)
	$install gmake                # GNU Make.
	$install bison                #
	$install python3              # /usr/local/bin/python3 -> python3.9
	# $install cppcheck           # Broken on 14.0-RELEASE-p4 - devel/cppcheck
	$install hs-ShellCheck-0.9.0
	$install cvs                  # Source code control
	# $install pylint             # Didn't need this
	echo "FreeBSD Notes:"
	echo "    Use gmake to build as make won't work"
	echo "    gmake check  # Lots fails as cppcheck is missing/broken"
	echo ""
        ;;
    yum)
        echo "Not yet supported" >&2
        exit 1
        ;;
    dnf)
        $install tzdata
        $install make grep sed gcc bison flex rcs cvs pylint cppcheck ShellCheck
        ;;
    yast)
        echo "Not yet supported" >&2
        exit 1
        ;;
    zypper)
        $install timezone
        $install make grep sed gcc bison flex rcs cvs python3-pylint cppcheck ShellCheck
        ;;
    esac
}

doc () {
    # prerequisites to build documentation
    case $installer in
    apk)
        echo "Not yet supported" >&2
        exit 1
        ;;
    apt)
        $install asciidoctor
        ;;
    emerge)
        echo "Not yet supported" >&2
        exit 1
        ;;
    pacman)
        $install asciidoctor
        ;;
    pkgin)
        echo "Not yet supported" >&2
        exit 1
        ;;
    pkg)
	$install asciidoc             # Need a2x to install man pages
        ;;
    yum)
        echo "Not yet supported" >&2
        exit 1
        ;;
    dnf)
        $install asciidoctor
        ;;
    yast|zypper)
        $install asciidoc 'rubygem(asciidoctor)'
        ;;
    esac
}

# Main sequence
main

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

echo ""
echo "# Done."

# end
