#!/bin/bash
#
# Installs tools and libraries needed to run, compile or develop ARB on Ubuntu.
#
# Tested with:
#       * Ubuntu 20.04 LTS
#       * Debian 11
#
# Previous versions of this script are available at
#     http://bugs.arb-home.de/export/18373/trunk/SH/arb_installubuntu4arb.sh      (Linux Mint 19; Ubuntu 18.04 LTS)
#     http://bugs.arb-home.de/export/14798/trunk/SH/arb_installubuntu4arb.sh      (Ubuntu 16.04 LTS)
#     http://bugs.arb-home.de/export/14409/trunk/SH/arb_installubuntu4arb.sh      (older)
#
# [ May as well work with other ubuntu flavors or debian.
#   Please report working tests and/or send needed changes to devel@arb-home.de
# ]

listwords() {
    if [ ! -z "$1" ]; then
        echo "    $1"
        shift
        listwords $*
    fi
}

SELF=${0}
CMD=${1:-}

if [ -z ${CMD} ]; then
    echo ""
    echo "Usage: arb_installubuntu4arb.sh what [openGL]"
    echo ""
    echo "If optional parameter 'openGL' is specified,"
    echo "packages needed for ARB with openGL get installed."
    echo "(Note: currently does only work with what=arb and old os version)"
    echo ""
    echo "what      installs"
    echo "-------   ---------------------------------------------------"
    echo "arb       things needed to run ARB"
    echo "compile   things needed to compile and run ARB"
    echo "develop   like 'compile' plus some optional development tools"
    echo "devdox    like 'develop' plus things needed for doxygen (big)"
    echo ""
    echo "(Note: script contains pointers to older versions of itself)"
    echo ""
else
    # other parameters
    if [ "$CMD" == "echo" ]; then
        shift
    fi

    WHAT=${1:-}
    OPENGL=${2:-}

    if [ "$CMD" == "echo" ]; then
        if [ "$WHAT" == "arb" ]; then
            echo \
              gnuplot \
              gv \
              libmotif-common \
              libxerces-c-dev \
              libgomp1 \
              transfig \
              xfig \
              xterm \

            # dependency for motif library itself (e.g. libxm4) is handled either via libglw1-mesa or via libmotif-dev below.

            if [ "$OPENGL" == "openGL" ]; then
                echo \
                  libpng16-16 \
                  libglw1-mesa \

            else
                echo \
                    libmotif-dev \

            fi

        elif [ "$WHAT" == "compile" ]; then
            ${SELF} echo arb ${OPENGL}
            echo \
              autoconf \
              automake \
              g++ \
              libboost-filesystem-dev \
              libboost-iostreams-dev \
              libboost-program-options-dev \
              libboost-random-dev \
              libboost-system-dev \
              libboost-thread-dev \
              libboost-test-dev \
              libglib2.0-dev \
              libreadline-dev \
              libtbb-dev \
              libtiff5-dev \
              libtool \
              libx11-dev \
              libxaw7-dev \
              libxext-dev \
              libxml2-utils \
              libxpm-dev \
              libxt-dev \
              lynx \
              make \
              time \
              xsltproc \

            if [ "$OPENGL" == "openGL" ]; then
                echo \
                  freeglut3-dev \
                  libglew-dev \
                  libpng-dev \
                  libglw1-mesa-dev \

            fi

            # the following package no longer works for ubuntu 20.04:
            # x11proto-print-dev

        elif [ "$WHAT" == "develop" ]; then
            ${SELF} echo compile ${OPENGL}
            echo \
              valgrind \
              pixmap \
              exuberant-ctags \
              xutils-dev \

        elif [ "$WHAT" == "devdox" ]; then
            ${SELF} echo develop ${OPENGL}
            echo \
              doxygen \
              texlive-latex-base \
              texlive-base-bin \

        else
            echo error_unknown_target_${WHAT}
        fi
    else
        echo "Ubuntu for ARB installer"
        PACKAGES=`$SELF echo $WHAT $OPENGL`
        SHOWWHAT=$WHAT
        if [ ! -z ${OPENGL} ]; then
            SHOWWHAT="${WHAT}+${OPENGL}"
        fi
        echo "Packages needed for '${SHOWWHAT}': `echo $PACKAGES | wc -w`"
        # listwords $PACKAGES

        echo '-------------------- [apt start]'
        sudo apt-get install $PACKAGES
        echo '-------------------- [apt end]'
    fi
fi

