diff options
| author | Andreas Politz <politza@hochschule-trier.de> | 2017-09-10 09:11:17 +0200 |
|---|---|---|
| committer | Andreas Politz <politza@hochschule-trier.de> | 2017-09-10 09:11:17 +0200 |
| commit | 694ac2389c6091d514744c001c5bd9a68b2039af (patch) | |
| tree | 45daf42e594c4568453465cda87ffea2636c8379 /server | |
| parent | 920e598c1ec6eb9a48dde32282d10adc9cc3b011 (diff) | |
Adds autobuild script
This script is able to install the server dependencies on a variety of
systems.
Diffstat (limited to 'server')
| -rwxr-xr-x | server/autobuild | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/server/autobuild b/server/autobuild new file mode 100755 index 0000000..4301e35 --- /dev/null +++ b/server/autobuild @@ -0,0 +1,259 @@ +#!/bin/sh + +## +## Installs package dependencies and builds the application. +## + +# Don't exit if some command fails. +set +e +# Disalbe file globbing. +set -f + +# Command to install packages. +PKGCMD= +# Args to pass to $PKGCMD. +PKGARGS= +# Required packages. +PACKAGES= +# Whether package installation requires root permissions. +INSTALL_AS_ROOT=true + +## +-----------------------------------------------------------+ +## * Utility Functions +## +-----------------------------------------------------------+ + +# Search for command $1 in PATH. Print its absolute filename. +which() +{ + if [ -z "$1" ]; then + return 1 + fi + IFS=: + for dir in $PATH; do + if [ -x "$dir/$1" ]; then + echo "$dir/$1" + unset IFS + return 0 + fi + done + unset IFS + return 1 +} + +# Exec $@ as root if required. +exec_privileged() { + if [ -z "$*" ]; then + echo "internal error: package install command is empty" >&2 + exit 2 + fi + if [ -w / ] || [ "$INSTALL_AS_ROOT" != "true" ]; then + "$@" + elif which sudo >/dev/null 2>&1; then + sudo "$@" + elif which su >/dev/null 2>&1; then + su -c "$@" + else + echo "No such program: sudo or su" >&2 + exit 1 + fi +} + +# Test if $1 is in PATH or exit with a failure status. +assert_program() +{ + if ! which "$1" >/dev/null 2>&1; then + echo "No such program: $1" >&2 + exit 1 + fi +} + +# Source filename $1 and echo variable $2. +source_var() +{ + if ! [ -f "$1" ] || ! [ -r "$1" ]; then + return 1 + fi + # shellcheck source=/dev/null + . "$1" + eval "echo \$$2" + return 0 +} + +## +-----------------------------------------------------------+ +## * OS Functions +## +-----------------------------------------------------------+ + +# Archlinux +os_arch() { + if ! [ -e "/etc/arch-release" ]; then + return 1; + fi + PKGCMD=pacman + PKGARGS="-S --needed" + PACKAGES="base-devel libpng zlib poppler-glib" + return 0; +} + +# CentOS +os_centos() { + if ! [ -e "/etc/centos-release" ]; then + return 1; + fi + PKGCMD=yum + if yum help install-n >/dev/null 2>&1; then + PKGARGS=install-n + else + PKGARGS=install + fi + PACKAGES="libpng-devel zlib-devel poppler-glib-devel poppler-devel pkgconfig autoconf automake gcc gcc-c++ make" + return 1 +} + +# FreeBSD +os_freebsd() { + if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "FreeBSD" ]; then + return 1 + fi + PKGCMD=pkg + PKGARGS=install + PACKAGES="autotools poppler-glib png pkgconf" + return 0 +} + +# Fedora +os_fedora() { + if ! [ -e "/etc/fedora-release" ]; then + return 1 + fi + PKGCMD=dnf + PKGARGS=install + PACKAGES="libpng-devel zlib-devel poppler-glib-devel poppler-devel autoconf automake gcc gcc-c++ make" + VERSION=$(source_var /etc/os-release VERSION_ID) + if [ -n "$VERSION" ] && [ "$VERSION" -ge 26 ]; then + PACKAGES="$PACKAGES pkgconf" + else + PACKAGES="$PACKAGES pkgconfig" + fi + return 0 +} + +# Debian/Ubuntu +os_debian() { + if ! [ -e "/etc/debian_version" ]; then + return 1 + fi + PACKAGES="libpng-dev libz-dev libpoppler-glib-dev libpoppler-dev libpoppler-private-dev pkg-config autoconf automake gcc g++ make" + PKGCMD=apt-get + PKGARGS=install + return 0 +} + +# Msys2 +os_msys2() { + if [ -z "$MSYSTEM" ] || ! [ -r "/etc/profile" ]; then + return 1 + fi + case $MSYSTEM in + MINGW64) + PACKAGES="base-devel mingw-w64-x86_64-toolchain mingw-w64-x86_64-libpng mingw-w64-x86_64-zlib mingw-w64-x86_64-poppler" ;; + MINGW32) + PACKAGES="base-devel mingw-w64-i686-toolchain mingw-w64-i686-libpng mingw-w64-i686-zlib mingw-w64-i686-poppler" ;; + MSYS) + assert_program uname + case $(uname -m) in + x86_64) + MSYSTEM=MINGW64 ;; + *) + MSYSTEM=MINGW32 ;; + esac + export MSYSTEM + # shellcheck source=/dev/null + . /etc/profile + exec "$0" ;; + *) + echo "Unrecognized MSYSTEM value: $MSYSTEM" >&2 + exit 1 ;; + esac + PKGCMD=pacman + PKGARGS="-S --needed" + INSTALL_AS_ROOT=false + return 0 +} + +# MacOS +os_macos() { + if ! which uname >/dev/null 2>&1 || [ "$(uname -s)" != "Darwin" ]; then + return 1 + fi + PKGCMD=brew + PKGARGS=install + PACKAGES="pkg-config poppler automake" + INSTALL_AS_ROOT=false + return 0 +} + +# NixOS +os_nixos() { + if [ -n "$AUTOBUILD_NIX_SHELL" ]; then + return 0 + fi + if ! which nix-shell >/dev/null 2>&1; then + return 1 + fi + if [ -z "$AUTOBUILD_NIX_SHELL" ]; then + command="AUTOBUILD_NIX_SHELL=true; export AUTOBUILD_NIX_SHELL; $0" + exec nix-shell --pure --command "$command" \ + -p gcc gnumake automake autoconf pkgconfig libpng zlib poppler + fi +} + +## +-----------------------------------------------------------+ +## * Figure out were we are, install deps and build the program +## +-----------------------------------------------------------+ + +os_arch || \ + os_centos || \ + os_freebsd || \ + os_fedora || \ + os_debian || \ + os_msys2 || \ + os_macos || \ + os_nixos || \ + { + echo "Failed to recognize this system" >&2 + exit 1 + } + + +if [ -n "$PKGCMD" ];then + assert_program "$PKGCMD" + echo "---------------------------" + echo " Installing packages " + echo "---------------------------" + echo "$PKGCMD $PKGARGS $PACKAGES" + exec_privileged $PKGCMD $PKGARGS $PACKAGES +fi + +echo "---------------------------" +echo " Configuring and compiling " +echo "---------------------------" +if ! [ -f ./configure ]; then + assert_program autoreconf + echo "autoreconf -i" + autoreconf -i +fi + +echo "./configure -q && make -s" +if ./configure -q && make -s; then + echo "===========================" + echo " Build succeeded. :O) " + echo "===========================" +else + echo "===========================" + echo " Build failed. ;o( " + echo "===========================" +fi + +# Local Variables: +# compile-command: "shellcheck autobuild" +# End: |
