2020-12-04 18:20:10 -05:00
#!/bin/sh
# Is similar to an autoconf configure script, but is written by hand
usage() {
printf "Usage: %s [options]\\n\\n" "$0"
printf " --prefix=/usr/local Prefix for all files\\n"
printf " --bindir=PREFIX/bin Prefix for binaries\\n"
printf " --sbindir=PREFIX/sbin Prefix for system binaries\\n"
printf " --mandir=PREFIX/share/man Prefix for manpages\\n"
2020-12-09 10:15:15 -05:00
printf " --man8dir=MANDIR/man8 Prefix for section 8 manpages\\n"
2020-12-04 18:20:10 -05:00
printf " --gopherroot=/var/gopher Path to gopher root\\n"
2021-01-02 20:09:34 -05:00
printf " --sysconfig=/etc/sysconfig Path to sysconfig directory\\n"
printf " --default=/etc/default Path to 'default' configuration directory\\n"
2020-12-04 18:20:10 -05:00
printf " --launchd=/Library/LaunchDaemons Path to launchd for MacOS\\n"
2021-01-02 20:09:34 -05:00
printf " --haikusrv=/boot/common/settings/network/services Path to services directory in Haiku\\n"
printf " --systemd=/lib/systemd/system Path to systemd directory when using systemd listener\\n"
2020-12-04 18:20:10 -05:00
printf " --os=autodetected Your target OS, one of linux, mac, haiku, netbsd, openbsd or freebsd\\n"
2020-12-22 19:48:42 -05:00
printf " --listener=somelistener Program to recieve and pass network requests; one or more of systemd, inetd, xinetd, comma-seperated, or autodetect, mac or haiku (parameter required, mac/haiku required on respective OSes)\\n"
2020-12-04 18:20:10 -05:00
printf " --hostname=autodetected Desired hostname for gophernicus to identify as\\n"
}
# Set values for each option
while [ "$#" -gt 0 ] ; do
opt="${1%%=*}"
opt="${opt##--}"
value="${1##*=}"
case "${opt}" in
prefix) PREFIX="${value}"; shift ;;
bindir) BINDIR="${value}"; shift ;;
sbindir) SBINDIR="${value}"; shift ;;
docdir) DOCDIR="${value}"; shift ;;
mandir) MANDIR="${value}"; shift ;;
2020-12-09 10:15:15 -05:00
man8dir) MAN8DIR="${value}"; shift ;;
2020-12-04 18:20:10 -05:00
gopherroot) GOPHERROOT="${value}"; shift ;;
sysconfig) SYSCONFIG="${value}"; shift ;;
default) DEFAULTCONF="${value}"; shift ;;
os) OS="${value}"; shift ;;
launchd) LAUNCHD="${value}"; shift ;;
haikusrv) HAIKUSRV="${value}"; shift ;;
2021-01-02 20:09:34 -05:00
systemd) SYSTEMD="${value}"; shift ;;
listener) LISTENERS="${value}"; shift ;;
2020-12-04 18:20:10 -05:00
hostname) HOSTNAME="${value}"; shift ;;
help) usage; exit 0 ;;
*) usage; exit 2 ;;
esac
done
# Set default values
: ${PREFIX:=/usr/local}
: ${BINDIR:=${PREFIX}/bin}
: ${SBINDIR:=${PREFIX}/sbin}
: ${DOCDIR:=${PREFIX}/share/doc}
: ${MANDIR:=${PREFIX}/share/man}
2020-12-09 10:15:15 -05:00
: ${MAN8DIR:=${MANDIR}/man8}
2020-12-04 18:20:10 -05:00
: ${GOPHERROOT:=/var/gopher}
: ${SYSCONFIG:=/etc/sysconfig}
: ${DEFAULTCONF:=/etc/default}
: ${LAUNCHD:=/Library/LaunchDaemons}
: ${HAIKUSRV:=/boot/common/settings/network/services}
2021-01-02 20:09:34 -05:00
: ${SYSTEMD:=/lib/systemd/system}
2020-12-04 18:20:10 -05:00
: ${CC:=cc}
2020-12-22 19:48:42 -05:00
: ${HOSTCC:=${CC}}
2020-12-04 18:20:10 -05:00
: ${CFLAGS:=-O2}
: ${HOSTNAME:=autodetect}
# Check for a compiler that actually works
printf "checking for working compiler... "
cat > conftest.c <<EOF
#include <stdlib.h>
int main() { return 0; }
EOF
if ! ${CC} -o conftest conftest.c; then
printf "no\\n"
exit 1
else
printf "${CC}\\n"
fi
# Autodetect the OS
2020-12-22 19:48:42 -05:00
if [ -z "${OS}" ]; then
2020-12-04 18:20:10 -05:00
# If it can't find uname, it needs to be manually specified
printf "checking for uname... "
if ! UNAME="$(command -v uname)"; then
printf "please provide OS in options\\n"
exit 1
else
printf "%s\\n" "${UNAME}"
fi
# If it can, it presses on
printf "checking for OS... "
case "$(${UNAME})" in
Linux) OS=linux ;;
Haiku) OS=haiku
INSTALL_HAIKU="install-haiku" ;;
Darwin) OS=mac
INSTALL_OSX="install-osx" ;;
NetBSD) OS=netbsd ;;
OpenBSD) OS=openbsd ;;
FreeBSD) OS=freebsd ;;
*) printf "unknown, pressing on anyway\\n" ;;
esac
printf "%s\\n" "${OS}"
fi
# Checks for an install command and falls back to install-sh
printf "checking for install... "
if ! INSTALL="$(command -v install)"; then
printf "install-sh"
INSTALL=build-aux/install-sh
else
2020-12-26 17:34:40 -05:00
# Check it has required features (*cough* macos)
mkdir testconf
touch testfile
2020-12-28 18:18:55 -05:00
${INSTALL} -t testconf testfile 2>&1 >/dev/null || INSTALL=build-aux/install-sh
2020-12-26 17:34:40 -05:00
rm testconf/testfile
2020-12-28 18:18:55 -05:00
${INSTALL} -T testfile testconf/testfile 2>&1 >/dev/null || INSTALL=build-aux/install-sh
2020-12-26 17:34:40 -05:00
rm -r testconf testfile
2020-12-04 18:20:10 -05:00
printf "%s" "${INSTALL}"
fi
printf "\\n"
2021-01-02 20:09:34 -05:00
listeners="$(echo ${LISTENERS} | tr ',' ' ')"
2020-12-22 19:48:42 -05:00
for listener in ${listeners}; do
# Check for listener validity and autodetect if required
# Checks that take place:
# mac OS = mac listener (both ways)
# haiku OS = haiku listener (both ways)
# systemd listener = linux OS
printf "checking for listener... "
if [ -z "${listener}" ]; then
printf "not given\\n"
exit 1
elif [ "${listener}" = "mac" ] && [ "${OS}" != "mac" ]; then
printf "mac listener only valid with macos\\n"
exit 1
elif [ "${listener}" = "haiku" ] && [ "${OS}" != "haiku" ]; then
printf "haiku listener only valid with haiku\\n"
exit 1
elif [ "${listener}" = "systemd" ] && [ "${OS}" != "linux" ]; then
printf "systemd listener only valid with linux\\n"
exit 1
elif [ "${listener}" = "autodetect" ]; then
# OS-specific listeners
case "${OS}" in
mac)
2021-01-02 20:09:34 -05:00
listener=mac
2020-12-22 19:48:42 -05:00
printf "mac\\n"
break ;;
haiku)
2021-01-02 20:09:34 -05:00
listener=haiku
2020-12-22 19:48:42 -05:00
printf "haiku\\n"
break ;;
esac
2020-12-04 18:20:10 -05:00
2020-12-22 19:48:42 -05:00
if [ -d "/lib/systemd/system" ] ; then
2021-01-02 20:09:34 -05:00
listener=systemd
2020-12-22 19:48:42 -05:00
printf "systemd\\n"
fi
2020-12-04 18:20:10 -05:00
2020-12-22 19:48:42 -05:00
printf "checking for inetd... "
2021-01-02 20:09:34 -05:00
if command -v update-inetd && [ "${listener}" = "autodetect" ]; then
listener=inetd
2020-12-22 19:48:42 -05:00
printf "inetd\\n"
fi
2020-12-04 18:20:10 -05:00
2020-12-22 19:48:42 -05:00
printf "checking for xinetd... "
2021-01-02 20:09:34 -05:00
if XINETD="$(command -v xinetd)" && [ "${listener}" = "autodetect" ]; then
listener=xinetd
2020-12-22 19:48:42 -05:00
printf "xinetd\\n"
fi
2020-12-04 18:20:10 -05:00
2020-12-22 19:48:42 -05:00
# Ensure we detected something
if [ "${listener}" = "autodetect" ]; then
printf "unable to autodetect, please manually specify\\n"
exit 1
fi
elif [ "${OS}" = "haiku" ] && [ "${listener}" != "haiku" ]; then
printf "only haiku listener supported on haiku\\n"
2020-12-04 18:20:10 -05:00
exit 1
2020-12-22 19:48:42 -05:00
elif [ "${OS}" = "mac" ] && [ "${listener}" != "mac" ]; then
printf "only mac listener supported on mac\\n"
exit 1
else
printf "%s\\n" "${listener}"
2020-12-04 18:20:10 -05:00
fi
2020-12-22 19:48:42 -05:00
# Act accordingly based on whichever listener we are given
case "${listener}" in
systemd)
INSTALL_SYSTEMD="install-systemd"
2021-01-02 20:39:14 -05:00
UNINSTALL_SYSTEMD="uninstall-systemd"
INSTALL_MSG_SYSTEMD='$(MAKE) install-msg-systemd' ;;
2020-12-22 19:48:42 -05:00
xinetd)
INSTALL_XINETD="install-xinetd"
UNINSTALL_XINETD="uninstall-xinetd"
2021-01-02 20:39:14 -05:00
XINETD_CONF="/etc/xinetd.d/gophernicus"
INSTALL_MSG_XINETD='$(MAKE) install-msg-xinetd' ;;
2020-12-22 19:48:42 -05:00
inetd)
INSTALL_INETD="install-inetd"
INETD_CONF="/etc/inetd.conf"
printf "checking for update-inetd... "
if ! UPDATE_INETD="$(command -v update-inetd)"; then
printf "not found\\n"
INSTALL_INETD_MANUAL="install-inetd-manual"
UNINSTALL_INETD_UPDATE="uninstall-inetd-manual"
else
printf "%s\\n" "${UPDATE_INETD}"
INSTALL_INETD_UPDATE="install-inetd-update"
UNINSTALL_INETD_UPDATE="uninstall-inetd-update"
fi
2021-01-02 20:39:14 -05:00
INSTALL_MSG_INETD='$(MAKE) install-msg-inetd'
2020-12-22 19:48:42 -05:00
;;
2021-01-02 20:39:14 -05:00
mac)
INSTALL_OSX="install-osx"
UNINSTALL_OSX="uninstall-osx"
INSTALL_MSG_OSX='$(MAKE) install-msg-osx' ;;
haiku)
INSTALL_HAIKU="install-haiku"
UNINSTALL_HAIKU="uninstall-haiku"
INSTALL_MSG_HAIKU='$(MAKE) install-msg-haiku' ;;
2020-12-22 19:48:42 -05:00
*) printf "The listener %s is not offically supported; continuing anyway.\\n" "${listener}" ;;
esac
done
2020-12-04 18:20:10 -05:00
# Try to detect hostname
2020-12-22 19:48:42 -05:00
printf "checking current hostname... "
2020-12-04 18:20:10 -05:00
if [ "${HOSTNAME}" = "autodetect" ]; then
HOSTNAME="$(hostname)"
# If no hostname then we couldn't autodetect
if [ $? != 0 ] || [ -z "${HOSTNAME}" ]; then
printf "unable to detect hostname\\n"
exit 1
fi
fi
printf "%s\\n" "${HOSTNAME}"
# Use libwrap when it is avaliable
printf "checking for libwrap... "
cat > conftest.c <<EOF
#include <tcpd.h>
int main() {}
EOF
if ${CC} -o conftest -lwrap conftest.c 2>/dev/null; then
2020-12-09 10:15:15 -05:00
LIBWRAP="-DHAVE_LIBWRAP -lwrap"
2020-12-04 18:20:10 -05:00
printf "yes"
else
LIBWRAP=
printf "no, but program will still work"
fi
printf "\\n"
# Check and use SHM if avaliable
printf "checking for ipcrm (SHM management)... "
if ! IPCRM="$(command -v ipcrm)"; then
printf "not found"
else
printf "%s" "${IPCRM}"
CLEAN_SHM="clean-shm"
fi
printf "\\n"
# Trying to autodetect make
printf "checking for make... "
if ! MAKE="$(command -v make)"; then
printf "not found, please pass MAKE=/path/to/make to make invocation"
MAKE="make"
else
printf "%s" "${MAKE}"
fi
printf "\\n"
# Don't replace an existing root
printf "checking for existing gopher root... "
if [ -d "${GOPHERROOT}" ] || [ -f "${GOPHERROOT}/gophermap" ]; then
INSTALL_ROOT="install-root"
printf "yes"
else
printf "no"
fi
printf "\\n"
# Sub in values
cp Makefile.in Makefile
printf "creating Makefile... "
2020-12-26 17:34:40 -05:00
sed -i -e "s:@CC@:${CC}:" Makefile
sed -i -e "s:@HOSTCC@:${HOSTCC}:" Makefile
sed -i -e "s:@LIBWRAP@:${LIBWRAP}:" Makefile
sed -i -e "s:@INSTALL@:${INSTALL}:" Makefile
sed -i -e "s:@MAKE@:${MAKE}:" Makefile
2020-12-04 18:20:10 -05:00
2020-12-26 17:34:40 -05:00
sed -i -e "s:@PREFIX@:${PREFIX}:" Makefile
sed -i -e "s:@BINDIR@:${BINDIR}:" Makefile
sed -i -e "s:@SBINDIR@:${SBINDIR}:" Makefile
sed -i -e "s:@DOCDIR@:${DOCDIR}:" Makefile
sed -i -e "s:@MANDIR@:${MANDIR}:" Makefile
sed -i -e "s:@MAN8DIR@:${MAN8DIR}:" Makefile
2020-12-04 18:20:10 -05:00
2020-12-26 17:34:40 -05:00
sed -i -e "s:@IPCRM@:${IPCRM}:" Makefile
sed -i -e "s:@CLEAN_SHM@:${CLEAN_SHM}:" Makefile
2020-12-04 18:20:10 -05:00
2020-12-26 17:34:40 -05:00
sed -i -e "s:@SYSCONF@:${SYSCONFIG}:" Makefile
sed -i -e "s:@DEFAULT@:${DEFAULTCONF}:" Makefile
sed -i -e "s:@HOSTNAME@:${HOSTNAME}:" Makefile
sed -i -e "s:@ROOT@:${GOPHERROOT}:" Makefile
2020-12-04 18:20:10 -05:00
2020-12-26 17:34:40 -05:00
sed -i -e "s:@HAIKUSRV@:${HAIKUSRV}:" Makefile
sed -i -e "s:@LAUNCHD@:${LAUNCHD}:" Makefile
2021-01-02 20:09:34 -05:00
sed -i -e "s:@SYSTEMD@:${SYSTEMD}:" Makefile
2020-12-26 17:34:40 -05:00
sed -i -e "s:@INSTALL_ROOT@:${INSTALL_ROOT}:" Makefile
2020-12-04 18:20:10 -05:00
2020-12-26 17:34:40 -05:00
sed -i -e "s:@INSTALL_OSX@:${INSTALL_OSX}:" Makefile
sed -i -e "s:@INSTALL_INETD_MANUAL@:${INSTALL_INETD_MANUAL}:" Makefile
sed -i -e "s:@INSTALL_INETD_UPDATE@:${INSTALL_INETD_UPDATE}:" Makefile
sed -i -e "s:@INSTALL_XINETD@:${INSTALL_XINETD}:" Makefile
sed -i -e "s:@INSTALL_SYSTEMD@:${INSTALL_SYSTEMD}:" Makefile
sed -i -e "s:@INSTALL_HAIKU@:${INSTALL_HAIKU}:" Makefile
2021-01-02 20:39:14 -05:00
sed -i -e "s:@INSTALL_MSG_INETD@:${INSTALL_MSG_INETD}:" Makefile
sed -i -e "s:@INSTALL_MSG_XINETD@:${INSTALL_MSG_XINETD}:" Makefile
sed -i -e "s:@INSTALL_MSG_SYSTEMD@:${INSTALL_MSG_SYSTEMD}:" Makefile
sed -i -e "s:@INSTALL_MSG_OSX@:${INSTALL_MSG_OSX}:" Makefile
sed -i -e "s:@INSTALL_MSG_HAIKU@:${INSTALL_MSG_HAIKU}:" Makefile
2020-12-26 17:34:40 -05:00
sed -i -e "s:@UNINSTALL_OSX@:${UNINSTALL_OSX}:" Makefile
sed -i -e "s:@UNINSTALL_INETD_MANUAL@:${UNINSTALL_INETD_MANUAL}:" Makefile
sed -i -e "s:@UNINSTALL_INETD_UPDATE@:${UNINSTALL_INETD_UPDATE}:" Makefile
sed -i -e "s:@UNINSTALL_XINETD@:${UNINSTALL_XINETD}:" Makefile
sed -i -e "s:@UNINSTALL_SYSTEMD@:${UNINSTALL_SYSTEMD}:" Makefile
sed -i -e "s:@UNINSTALL_HAIKU@:${UNINSTALL_HAIKU}:" Makefile
2020-12-04 18:20:10 -05:00
2020-12-26 17:34:40 -05:00
sed -i -e "s:@INETD_CONF@:${INETD_CONF}:" Makefile
sed -i -e "s:@XINETD_CONF@:${XINETD_CONF}:" Makefile
2020-12-22 19:48:42 -05:00
2020-12-04 18:20:10 -05:00
printf "done\\n"
2020-12-22 19:48:42 -05:00
# Also sub in $HOSTNAME to the various init systems (whether or not we really
# use them, its just easier)
for f in gophernicus.env haiku_snippet org.gophernicus.server.plist \
gophernicus.xinetd; do
printf "creating init/${f}... "
sed -e "s:@HOSTNAME@:${HOSTNAME}:" "init/${f}.in" > "init/${f}"
printf "done\\n"
done
2021-01-02 20:09:34 -05:00
# And generate gophernicus@.service
printf "creating init/gophernicus@.service... "
sed -e "s:@DEFAULT@:${DEFAULTCONF}:" \
-e "s:@SYSCONFIG@:${SYSCONFIG}:" \
'init/gophernicus@.service.in' > 'init/gophernicus@.service'
printf "done\\n"
2020-12-04 18:20:10 -05:00
# Cleanup
rm -f conftest conftest.c