#!/bin/bash

set -ex

################
# Usage: controller.sh <os> <version> <basepath>
# The controller script does all of the jar compilation
# and setup needed to build packages for <os>
# The os string should be one of el, redhatfips, sles, debian, ubuntu,
# or fedora
# The version should be the numerical OS version for el,
# redhatfips, sles, and fedora and the codename for debian/ubuntu.
# The resulting artifacts will be stored in <basepath>
################
build_os=$1
build_ver=$2
basepath=$3

if [ ! -d "$basepath" ]; then
  mkdir -p $basepath
fi
if [ ! -d "$basepath/base" ]; then
        DESTDIR="$basepath/base" bash install.sh install_redhat
fi
if [ ! -d "$basepath/systemd_el" ]; then
        cp -r "$basepath/base" "$basepath/systemd_el"
        DESTDIR="$basepath/systemd_el" bash install.sh systemd_redhat
fi

# things are only different if we have docs, deb docs get
# installed in an unversioned folder but rpm docs get installed
# in a versioned folder.
if [ -d ext/docs ]; then
        if [ ! -d "$basepath/base_deb" ]; then
                DESTDIR="$basepath/base_deb" bash install.sh install_deb
                if [ ! -d "$basepath/systemd_deb" ]; then
                        cp -r "$basepath/base_deb" "$basepath/systemd_deb"
                        DESTDIR="$basepath/systemd_deb" bash install.sh systemd_deb
                fi
        fi
else
        if [ ! -d "$basepath/systemd_deb" ]; then
                cp -r "$basepath/base" "$basepath/systemd_deb"
                DESTDIR="$basepath/systemd_deb" bash install.sh systemd_deb
        fi
fi

os=$build_os
if [ "$os" = "debian" ]; then
        os_dist=$build_ver
else
        os_version=$build_ver
fi

case $os in
        # there's no differences in packaging for deb vs ubuntu
        # if that changes we'll need to fix this
        debian|ubuntu)
                dir="$basepath/systemd_deb"
                ;;
        amazon|el|fedora|redhatfips|sles)
                dir="$basepath/systemd_el"
                ;;
        *)
                echo "I have no idea what I'm doing with $os, teach me?" >&2
                exit 1
                ;;
esac

# bash will eat your spaces, so let's array. see http://mywiki.wooledge.org/BashFAQ/050 for more fun.
params=("--user" "puppet" "--group" "puppet" "--chdir" "$dir" "--realname" "puppetserver" "--operating-system" "$os" "--name" "openvox-server" "--package-version" "8.15.2" "--release" "1" "--platform-version" "8")
if [ -n "$os_version" ]; then params+=("--os-version" "$os_version"); fi
if [ -n "$os_dist" ]; then params+=("--dist" "$os_dist"); fi

params+=('--description' "$(printf "Vox Pupuli puppetserver\nContains: OpenVox Server (org.openvoxproject/puppetserver 8.15.2,org.clojure/clojure 1.12.5,org.bouncycastle/bcpkix-jdk18on 1.85,org.openvoxproject/jruby-utils 5.5.0,org.openvoxproject/puppetserver 8.15.2,org.openvoxproject/trapperkeeper-webserver 12.1.0,org.openvoxproject/trapperkeeper-metrics 2.3.0)")")



params+=('--replaces' "'puppetserver',''")


params+=('--create-dir' '/opt/puppetlabs/server/data/puppetserver/jars')
params+=('--create-dir' '/opt/puppetlabs/server/data/puppetserver/yaml')


if [[ "$os" = 'el' || "$os" = 'sles' || "$os" = 'fedora' || "$os" = 'redhatfips' || "$os" = 'amazon' ]]; then
        # pull in rpm dependencies
                        params+=("--additional-dependency")
                params+=("openvox-agent >= 8.28.1")
                        params+=("--additional-dependency")
                params+=("openvox-agent < 9.0.0~")
                # get rpm install trigger scripts
                # get rpm upgrade trigger scripts
                : # Need something in case there are no additional dependencies
else
        # if we aren't an rpm, pull in deb dependencies
                        params+=("--additional-dependency")
                params+=("openvox-agent (>= 8.28.1)")
                        params+=("--additional-dependency")
                params+=("openvox-agent (<< 9.0.0~)")
                                        : # Need something in case there are no additional dependencies
fi


ruby $PWD/ext/fpm.rb "${params[@]}"
