#! /bin/sh

# install_prereq: a script to install distribution-specific
# prerequisites

set -e

usage() {
	echo "$0: a script to install distribution-specific prerequisites"
	echo
	echo "$0                     : Shows this message"
	echo "$0 test                : Prints commands it is about to run"
	echo "$0 install             : Really install"
	echo "$0 install-unpackaged  : Really install unpackaged requirements"
	echo "$0 minimal             : Install only the packages necessary for build tool generation and a basic build"
}

# Basic build system:
PACKAGES_DEBIAN_BUILD="build-essential pkg-config autoconf-archive"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN_BUILD"
# Asterisk: basic requirements:
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libedit-dev libjansson-dev libsqlite3-dev uuid-dev libxml2-dev"
# Asterisk: for addons:
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libspeex-dev libspeexdsp-dev libogg-dev libvorbis-dev libasound2-dev portaudio19-dev libcurl4-openssl-dev xmlstarlet bison flex"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libpq-dev unixodbc-dev libneon27-dev libgmime-2.6-dev libgmime-3.0-dev liblua5.2-dev liburiparser-dev libxslt1-dev libssl-dev"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libmysqlclient-dev libbluetooth-dev libradcli-dev freetds-dev libjack-jackd2-dev bash libcap-dev"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libsnmp-dev libiksemel-dev libcorosync-common-dev libcpg-dev libcfg-dev libnewt-dev libpopt-dev libical-dev libspandsp-dev"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libresample1-dev libc-client2007e-dev binutils-dev libsrtp0-dev libsrtp2-dev libgsm1-dev doxygen graphviz zlib1g-dev libldap2-dev"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libcodec2-dev libfftw3-dev libsndfile1-dev libunbound-dev"
# Asterisk: for the unpackaged below:
PACKAGES_DEBIAN="$PACKAGES_DEBIAN wget subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_DEBIAN="$PACKAGES_DEBIAN bzip2 patch"

# Basic build system:
PACKAGES_RH_BUILD="make gcc gcc-c++ pkgconfig autoconf-archive"
PACKAGES_RH="$PACKAGES_RH_BUILD"
# Asterisk: basic requirements:
PACKAGES_RH="$PACKAGES_RH libedit-devel jansson-devel libuuid-devel sqlite-devel libxml2-devel"
# Asterisk: for addons:
PACKAGES_RH="$PACKAGES_RH speex-devel speexdsp-devel libogg-devel libvorbis-devel portaudio-devel libcurl-devel xmlstarlet bison flex"
PACKAGES_RH="$PACKAGES_RH postgresql-devel unixODBC-devel neon-devel gmime-devel lua-devel uriparser-devel libxslt-devel openssl-devel"
PACKAGES_RH="$PACKAGES_RH mysql-devel bluez-libs-devel radcli-devel freetds-devel jack-audio-connection-kit-devel bash libcap-devel"
PACKAGES_RH="$PACKAGES_RH net-snmp-devel iksemel-devel corosynclib-devel newt-devel popt-devel libical-devel spandsp-devel"
PACKAGES_RH="$PACKAGES_RH libresample-devel uw-imap-devel binutils-devel libsrtp-devel gsm-devel doxygen graphviz zlib-devel openldap-devel hoard"
PACKAGES_RH="$PACKAGES_RH codec2-devel fftw-devel libsndfile-devel unbound-devel"
# Asterisk: for the unpackaged below:
PACKAGES_RH="$PACKAGES_RH wget subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_RH="$PACKAGES_RH bzip2 patch"

# Basic build system:
PACKAGES_SUSE_BUILD="make gcc gcc-c++ pkg-config autoconf-archive"
PACKAGES_SUSE="$PACKAGES_SUSE_BUILD"
# Asterisk: basic requirements:
PACKAGES_SUSE="$PACKAGES_SUSE libedit-devel libjansson-devel libuuid-devel sqlite3-devel libxml2-devel"
# Asterisk: for addons:
PACKAGES_SUSE="$PACKAGES_SUSE speex-devel speexdsp-devel libogg-devel libvorbis-devel portaudio-devel libcurl-devel xmlstarlet bison flex"
PACKAGES_SUSE="$PACKAGES_SUSE postgresql-devel unixODBC-devel libneon-devel gmime-devel lua-devel liburiparser-devel libxslt-devel libopenssl-devel"
PACKAGES_SUSE="$PACKAGES_SUSE libmysqlclient-devel bluez-devel freeradius-client-devel freetds-devel bash libcap-devel"
PACKAGES_SUSE="$PACKAGES_SUSE net-snmp-devel iksemel-devel libcorosync-devel newt-devel popt-devel libical-devel spandsp-devel"
PACKAGES_SUSE="$PACKAGES_SUSE imap-devel libsrtp2-devel libgsm-devel doxygen graphviz zlib-devel openldap2-devel"
PACKAGES_SUSE="$PACKAGES_SUSE codec2-devel fftw3-devel libsndfile-devel unbound-devel"
# Asterisk: for the unpackaged below:
PACKAGES_SUSE="$PACKAGES_SUSE wget subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_SUSE="$PACKAGES_SUSE bzip2 patch"

# Basic build system:
PACKAGES_ARCH_BUILD="make gcc pkg-config autoconf-archive"
PACKAGES_ARCH="$PACKAGES_ARCH_BUILD"
# Asterisk: basic requirements:
PACKAGES_ARCH="$PACKAGES_ARCH libedit jansson libutil-linux libxml2 sqlite"
# Asterisk: for addons:
PACKAGES_ARCH="$PACKAGES_ARCH speex speexdsp libogg libvorbis portaudio curl xmlstarlet bison flex"
PACKAGES_ARCH="$PACKAGES_ARCH postgresql-libs unixodbc neon gmime lua uriparser libxslt openssl"
PACKAGES_ARCH="$PACKAGES_ARCH libmariadbclient bluez-libs radcli freetds bash libcap"
PACKAGES_ARCH="$PACKAGES_ARCH net-snmp libnewt popt libical spandsp"
PACKAGES_ARCH="$PACKAGES_ARCH c-client binutils libsrtp gsm doxygen graphviz zlib libldap"
PACKAGES_ARCH="$PACKAGES_ARCH fftw libsndfile unbound"
# Asterisk: for the unpackaged below:
PACKAGES_ARCH="$PACKAGES_ARCH wget subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_ARCH="$PACKAGES_ARCH bzip2 patch"

# Basic build system:
PACKAGES_GENTOO_BUILD="sys-devel/make sys-devel/gcc dev-util/pkgconfig"
PACKAGES_GENTOO="$PACKAGES_GENTOO_BUILD"
# Asterisk: basic requirements:
PACKAGES_GENTOO="$PACKAGES_GENTOO dev-libs/libedit dev-libs/jansson sys-libs/e2fsprogs-libs dev-libs/libxml2 dev-db/sqlite"
# Asterisk: for addons:
PACKAGES_GENTOO="$PACKAGES_GENTOO media-libs/speex media-libs/speexdsp media-libs/libogg media-libs/libvorbis media-libs/portaudio net-misc/curl app-text/xmlstarlet sys-devel/bison sys-devel/flex"
PACKAGES_GENTOO="$PACKAGES_GENTOO dev-db/postgresql dev-db/unixODBC net-libs/neon dev-libs/gmime dev-lang/lua dev-libs/uriparser dev-libs/libxslt dev-libs/openssl"
PACKAGES_GENTOO="$PACKAGES_GENTOO virtual/libmysqlclient net-wireless/bluez net-dialup/radiusclient-ng dev-db/freetds app-shells/bash sys-libs/libcap"
PACKAGES_GENTOO="$PACKAGES_GENTOO net-analyzer/net-snmp dev-libs/iksemel sys-cluster/corosync dev-libs/newt dev-libs/popt dev-libs/libical media-libs/spandsp"
PACKAGES_GENTOO="$PACKAGES_GENTOO net-libs/c-client sys-devel/binutils net-libs/libsrtp media-sound/gsm media-libs/libilbc app-doc/doxygen sys-libs/zlib net-nds/openldap"
PACKAGES_GENTOO="$PACKAGES_GENTOO sci-libs/fftw media-libs/libsndfile net-dns/unbound"
# Asterisk: for the unpackaged below:
PACKAGES_GENTOO="$PACKAGES_GENTOO net-misc/wget dev-vcs/subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_GENTOO="$PACKAGES_GENTOO app-arch/bzip2 sys-devel/patch"

# Basic build system:
PACKAGES_NBSD_BUILD="gmake pkg-config"
PACKAGES_NBSD="$PACKAGES_NBSD_BUILD"
# Asterisk: basic requirements:
PACKAGES_NBSD="$PACKAGES_NBSD editline jansson sqlite3 libuuid libxml2"
# Asterisk: for addons:
PACKAGES_NBSD="$PACKAGES_NBSD speex speexdsp libogg libvorbis portaudio-devel curl bison flex"
PACKAGES_NBSD="$PACKAGES_NBSD postgresql10-client unixodbc neon gmime lua52 uriparser libxslt openssl"
PACKAGES_NBSD="$PACKAGES_NBSD mysql-client radiusclient-ng freetds bash"
PACKAGES_NBSD="$PACKAGES_NBSD net-snmp iksemel popt libical spandsp"
PACKAGES_NBSD="$PACKAGES_NBSD imap-uw srtp gsm doxygen graphviz libzip openldap-client"
PACKAGES_NBSD="$PACKAGES_NBSD codec2 fftw libsndfile unbound"
# Asterisk: for the unpackaged below:
PACKAGES_NBSD="$PACKAGES_NBSD wget subversion-base"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_NBSD="$PACKAGES_NBSD bzip2 patch"

# Basic build system:
PACKAGES_OBSD_BUILD="gmake"
PACKAGES_OBSD="$PACKAGES_OBSD_BUILD"
# Asterisk: basic requirements:
PACKAGES_OBSD="$PACKAGES_OBSD libxml sqlite3 e2fsprogs jansson"
# Asterisk: for addons:
PACKAGES_OBSD="$PACKAGES_OBSD speex speexdsp libogg libvorbis portaudio-svn curl xmlstarlet bison"
PACKAGES_OBSD="$PACKAGES_OBSD postgresql-client iodbc neon gmime lua%5.2 uriparser libxslt"
PACKAGES_OBSD="$PACKAGES_OBSD mariadb-client radcli freetds"
PACKAGES_OBSD="$PACKAGES_OBSD net-snmp iksemel popt libical spandsp"
PACKAGES_OBSD="$PACKAGES_OBSD c-client libsrtp gsm doxygen graphviz"
PACKAGES_OBSD="$PACKAGES_OBSD fftw3 libsndfile"
# Asterisk: for the unpackaged below:
PACKAGES_OBSD="$PACKAGES_OBSD wget subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_OBSD="$PACKAGES_OBSD bzip2"

# Basic build system:
PACKAGES_FBSD_BUILD="gmake pkgconf autoconf-archive"
PACKAGES_FBSD="$PACKAGES_FBSD_BUILD"
# Asterisk: basic requirements:
PACKAGES_FBSD="$PACKAGES_FBSD libedit jansson e2fsprogs-libuuid sqlite3 libxml2"
# Asterisk: for addons:
PACKAGES_FBSD="$PACKAGES_FBSD speex speexdsp libogg libvorbis portaudio curl xmlstarlet bison flex"
PACKAGES_FBSD="$PACKAGES_FBSD postgresql10-client unixODBC neon gmime26 lua52 uriparser libxslt openssl"
PACKAGES_FBSD="$PACKAGES_FBSD mysql57-client radcli freetds"
PACKAGES_FBSD="$PACKAGES_FBSD net-snmp iksemel corosync newt popt libical spandsp"
PACKAGES_FBSD="$PACKAGES_FBSD cclient libbfd libsrtp gsm libilbc doxygen graphviz libzip openldap-client libhoard"
PACKAGES_FBSD="$PACKAGES_FBSD codec2 fftw3 libsndfile unbound"
# Asterisk: for the unpackaged below:
PACKAGES_FBSD="$PACKAGES_FBSD wget subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_FBSD="$PACKAGES_FBSD bzip2 patch"

# Basic build system:
PACKAGES_DBSD_BUILD="gmake pkgconf"
PACKAGES_DBSD="$PACKAGES_DBSD_BUILD"
# Asterisk: basic requirements:
PACKAGES_DBSD="$PACKAGES_DBSD libedit jansson e2fsprogs-libuuid sqlite3 libxml2"
# Asterisk: for addons:
PACKAGES_DBSD="$PACKAGES_DBSD speex speexdsp libogg libvorbis portaudio curl xmlstarlet bison flex"
PACKAGES_DBSD="$PACKAGES_DBSD postgresql10-client unixODBC neon gmime26 lua52 uriparser libxslt libressl"
PACKAGES_DBSD="$PACKAGES_DBSD mariadb101-client radcli freetds"
PACKAGES_DBSD="$PACKAGES_DBSD net-snmp iksemel corosync newt popt libical spandsp"
PACKAGES_DBSD="$PACKAGES_DBSD cclient binutils libsrtp gsm libilbc doxygen graphviz libzip openldap-client libhoard"
PACKAGES_DBSD="$PACKAGES_DBSD codec2 fftw3 libsndfile unbound"
# Asterisk: for the unpackaged below:
PACKAGES_DBSD="$PACKAGES_DBSD wget subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_DBSD="$PACKAGES_DBSD bzip2 patch"

KVERS=`uname -r`

case "$1" in
test)
	testcmd=echo
	;;
install)
	testcmd=''
	;;
install-unpackaged)
	unpackaged="yes"
	;;
minimal)
	# Overwrite the package lists and let the rest of the package handling
	# plumbing do its magic
	PACKAGES_ARCH="$PACKAGES_ARCH_BUILD"
	PACKAGES_DBSD="$PACKAGES_DBSD_BUILD"
	PACKAGES_DEBIAN="$PACKAGES_DEBIAN_BUILD"
	PACKAGES_FBSD="$PACKAGES_FBSD_BUILD"
	PACKAGES_GENTOO="$PACKAGES_GENTOO_BUILD"
	PACKAGES_NBSD="$PACKAGES_NBSD_BUILD"
	PACKAGES_OBSD="$PACKAGES_OBSD_BUILD"
	PACKAGES_RH="$PACKAGES_RH_BUILD"
	PACKAGES_SUSE="$PACKAGES_SUSE_BUILD"
	;;
'')
	usage
	exit 0
	;;
*)
	usage
	exit 1
	;;
esac

in_test_mode() {
	test "$testcmd" != ''
}

check_installed_debs() {
	for pack in "$@" ; do
		tocheck="${tocheck} ^${pack}$ ~P^${pack}$"
	done
	aptitude -F '%c %p' search $tocheck 2>/dev/null | awk '/^p/{print $2}' | grep -vF :
}

# parsing the output of yum is close to impossible.
# We'll use rpm and hope for the best:
check_installed_rpms() {
	for pack in "$@"
	do
		if ! rpm -q $pack >/dev/null 2>/dev/null
		then echo $pack
		fi
	done
}

check_installed_equery() {
	for pack in "$@"
	do
		# equery --quiet list $pack
		# is slower and
		# would require the optional app-portage/gentoolkit
		# /var/lib/portage/world would be the non-dep list
		pack_with_version=${pack/:/-} # replace a possible version with '-'
		if ! ls -d /var/db/pkg/${pack_with_version}* >/dev/null 2>/dev/null
		then echo $pack
		fi
	done
}

check_installed_pacman() {
	for pack in "$@"
	do
		if ! pacman -Q --explicit $pack >/dev/null 2>/dev/null
		then echo $pack
		fi
	done
}

check_installed_pkgs() {
	for pack in "$@"
	do
		if [ `pkg_info -a | grep $pack | wc -l` = 0 ]; then
		echo $pack
		fi
	done
}

check_installed_fpkgs() {
	for pack in "$@"
	do
		if [ `pkg info -a | grep $pack | wc -l` = 0 ]; then
		echo $pack
		fi
	done
}

check_installed_zypper() {
	for pack in "$@"
	do
		if ! zypper se -ixnC $pack >/dev/null 2>/dev/null
		then echo $pack
		fi
	done
}

handle_debian() {
	if ! [ -x "$(command -v aptitude)" ]; then
		apt-get install -y aptitude
	fi
	extra_packs=`check_installed_debs $PACKAGES_DEBIAN`
	$testcmd aptitude update
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd aptitude install -y $extra_packs
	fi
}

handle_linuxmint() {
	# Store temporary variable to contain list of missing packages
	missing_packages=""

	# Check installed packages for Mint
	for package in $PACKAGES_DEBIAN; do
		if [ "$package" = "libgmime-2.6-dev" ] || [ "$package" = "libsrtp0-dev" ]; then
			continue
		fi

		missing_package_check=$(apt list --installed 2>/dev/null | grep -c $package)

		if [ "$missing_package_check" -eq 0 ]; then
			echo "This package is missing: "$package
			missing_packages="${missing_packages} $package"
		else
			echo "Package "$package" is present."
		fi
	done

	# Update the packages on our local system
	echo "==========================="
	echo "Updating your system's packages"
	echo "==========================="
	$testcmd apt update

	# Inform user of packages to be installed
	echo "==========================="
	echo "The following packages will be installed on your system:"
	echo $missing_packages
	echo "==========================="

	if [ "$missing_packages" != "" ] ; then
		$testcmd apt -m install -y $missing_packages
	fi
}

handle_rh() {
	extra_packs=`check_installed_rpms $PACKAGES_RH`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd yum install --skip-broken --assumeyes $extra_packs
	fi
}

handle_gentoo() {
	extra_packs=`check_installed_equery $PACKAGES_GENTOO`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd emerge $extra_packs
	fi
}

handle_arch() {
	extra_packs=`check_installed_pacman $PACKAGES_ARCH`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd pacman -S --asexplicit --noconfirm $extra_packs
	fi
}

handle_nbsd() {
	extra_packs=`check_installed_pkgs $PACKAGES_NBSD`
	if [ x"$extra_packs" != "x" ] ; then
		if [ -z "$PKG_PATH" ] ; then
			# see NetBSD Problem Report #48177
			export PKG_PATH="http://cdn.NetBSD.org/pub/pkgsrc/packages/$(uname -s)/$(uname -p)/$(uname -r)/All"
		fi
		$testcmd pkg_add $extra_packs
	fi
}

handle_obsd() {
	extra_packs=`check_installed_pkgs $PACKAGES_OBSD`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd pkg_add $extra_packs
	fi
}

handle_fbsd() {
	extra_packs=`check_installed_fpkgs $PACKAGES_FBSD`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd pkg install -y $extra_packs
	fi
}

handle_dbsd() {
	extra_packs=`check_installed_fpkgs $PACKAGES_DBSD`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd pkg install -y $extra_packs
	fi
}

handle_SUSE() {
	extra_packs=`check_installed_zypper $PACKAGES_SUSE`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd zypper install --no-confirm $extra_packs
	fi
}

install_unpackaged() {
	# Only install libresample if it wasn't installed via package
	if ! test -f /usr/include/libresample.h; then
		echo "*** Installing libresample ***"
		svn co https://svn.digium.com/svn/thirdparty/libresample/trunk libresample-trunk
		cd libresample-trunk
		./configure
		make all install
		cd ..
	fi

	# Only install libsrtp2 if it wasn't installed via package
	if ! test -f /usr/include/srtp/srtp.h; then
		if ! test -f /usr/include/srtp2/srtp.h; then
			echo "*** Installing libsrtp2 ***"
			wget -O - http://github.com/cisco/libsrtp/archive/v2.tar.gz | zcat | tar -xf -
			cd libsrtp-2
			./configure --enable-openssl
			make shared_library install
			cd ..
			if test -d /etc/ld.so.conf.d; then
				echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local.conf
			else # for example: Slackware 14.2
				echo "/usr/local/lib" > /etc/ld.so.conf
			fi
			/sbin/ldconfig
		fi
	fi

	if ! test -f /usr/include/pjlib.h; then
		echo "PJProject not installed, yet. Therefore, please, run"
		echo "./configure --with-pjproject-bundled"
	fi
}

if in_test_mode; then
	echo "#############################################"
	echo "## $1: test mode."
	echo "## Use the commands here to install your system."
	echo "#############################################"
elif test "${unpackaged}" = "yes" ; then
	install_unpackaged
	exit 0
fi

OS=`uname -s`
unsupported_distro=''

# A number of distributions we don't (yet?) support.
if [ "$OS" != 'Linux' -a "$OS" != 'NetBSD' -a "$OS" != 'OpenBSD' -a "$OS" != 'FreeBSD' -a "$OS" != 'DragonFly' ]; then
	echo >&2 "$0: Your OS ($OS) is currently not supported. Aborting."
	exit 1
fi

if [ -f /etc/mandrake-release ]; then
	unsupported_distro='Mandriva'
fi

if [ -f /etc/slackware-version ] || ([ -f /etc/os-release ] && . /etc/os-release && [ "$ID" = "slackware" ]); then
	echo >&2 "$0: Your distribution (Slackware) is currently not supported. Aborting. Try manually:"
	# libedit requires a newer version than Slackware 14.2, for example Slackware-current
	# or you build it manually: <http://thrysoee.dk/editline/>
	echo >&2 "$0: # slackpkg install make gcc pkg-config libedit util-linux sqlite libxml2 patch wget"
	# required for libjansson
	echo >&2 "$0: # ./contrib/scripts/install_prereq install-unpackaged"
	exit 1
fi

if [ "$unsupported_distro" != '' ]; then
	echo >&2 "$0: Your distribution ($unsupported_distro) is currently not supported. Aborting."
	exit 1
fi

# The distributions we do support:
if [ -r /etc/debian_version ]; then
	if [ -f /etc/linuxmint/info ]; then
		handle_linuxmint
	else
		handle_debian
	fi
elif [ -r /etc/redhat-release ]; then
	handle_rh
elif [ -f /etc/SuSE-release -o -f /etc/novell-release ]; then
	handle_SUSE
elif [ -f /etc/os-release ] && . /etc/os-release && [ "$ID" = "opensuse" ]; then
	handle_SUSE
elif [ -r /etc/arch-release ]; then
	handle_arch
elif [ -f /etc/os-release ] && . /etc/os-release && [ "$ID_LIKE" = "archlinux" ]; then
	handle_arch # $ID=arch
elif [ -f /etc/gentoo-release ]; then
	handle_gentoo
elif [ -f /etc/os-release ] && . /etc/os-release && [ "$ID" = "gentoo" ]; then
	handle_gentoo
elif [ "$OS" = 'NetBSD' ]; then
	handle_nbsd
elif [ "$OS" = 'OpenBSD' ]; then
	handle_obsd
elif [ "$OS" = 'FreeBSD' ]; then
	handle_fbsd
elif [ "$OS" = 'DragonFly' ]; then
	handle_dbsd
fi

if ! in_test_mode; then
	echo "#############################################"
	echo "## $1 completed successfully"
	echo "#############################################"
fi
