#!/bin/bash
# Copyright (C) 2014 by Bundesamt f�r Sicherheit in der Informationstechnik
# Software engineering by Intevation GmbH
#
# This file is Free Software under the GNU GPL (v>=2)
# and comes with ABSOLUTELY NO WARRANTY!
# See LICENSE.txt for details.

process_includes()
{
  # Process include statements of the form:
  # ###INCLUDE:filename
  # in the file given as $1
  # the path given as $2, it is used as base path for the included files.

  local file="$1"
  local include_base="$2"

  local inc_directive
  while [ "${inc_directive:=`grep '^###INCLUDE:' "$file" | head -1`}" ] ; do
    local include_file="${inc_directive#'###INCLUDE:'}"
    sed -i "/${inc_directive}/r ${include_base}/${include_file}" "$file"
    sed -i "/${inc_directive}/d" "$file"
    inc_directive=""
  done
}


TMPDIR=$(mktemp -d)
TMPSHAR=$(mktemp "@CMAKE_BINARY_DIR@/shar.XXXXXXXXXX")

ARCH="$1"

if [ "$ARCH" != "i386" -a "$ARCH" != "x86_64" ]; then
    echo "Usage $0 <architecture>"
    echo "with archtiecture either i386 or x86_64"
    echo "defaulting to x86_64"
    ARCH=x86_64
fi

INSTALLER="@CMAKE_BINARY_DIR@/TrustBridge-@PROJECT_VERSION@.sh"
UNINSTALLER="$TMPDIR/bin/trustbridge-deinstall.sh"

EXEFILES=("@CMAKE_BINARY_DIR@/cinst/cinst"
          "@CMAKE_BINARY_DIR@/cinst/trustbridge-nss-installer"
          "@CMAKE_BINARY_DIR@/ui/trustbridge"
          "@CMAKE_BINARY_DIR@/ui/trustbridge-tray-starter.sh"
          "$UNINSTALLER")

ICON="@CMAKE_SOURCE_DIR@/ui/img/logo-128.png"
HELPFILES=$(find @CMAKE_BINARY_DIR@/manuals/help-manual/html \
    -maxdepth 1 -type f -printf "%p \n")
HELPFILES_STATIC=$(find @CMAKE_BINARY_DIR@/manuals/help-manual/html/_static \
    -maxdepth 1 -type f -printf "%p \n")
HELPFILES_IMG=$(find @CMAKE_BINARY_DIR@/manuals/help-manual/html/_images \
    -maxdepth 1 -type f -printf "%p \n")
HELPFILES_SOURCES=$(find @CMAKE_BINARY_DIR@/manuals/help-manual/html/_sources \
    -maxdepth 1 -type f -printf "%p \n")

# Make installation in TMPDIR, this is what we will put into the
# shar-archive.
echo "Using $TMPDIR for temporary installation to build installer..."
install -d "$TMPDIR/bin"
install -d "$TMPDIR/share/pixmaps/trustbridge"
install -d "$TMPDIR/share/doc/trustbridge"
install -d "$TMPDIR/share/doc/trustbridge/_static"
install -d "$TMPDIR/share/doc/trustbridge/_sources"
install -d "$TMPDIR/share/doc/trustbridge/_images"
install "${EXEFILES[@]}" "$TMPDIR/bin"
install "$ICON" "$TMPDIR/share/pixmaps/trustbridge/trustbridge.png"

binnames=""
for file in "${EXEFILES[@]}" ; do
  binnames="`basename $file` $binnames"
done
for file in ${HELPFILES} ; do
  install "$file" "$TMPDIR/share/doc/trustbridge"
  helpnames="`basename $file` $helpnames"
done
for file in ${HELPFILES_IMG} ; do
  install "$file" "$TMPDIR/share/doc/trustbridge/_images"
  helpnames_img="`basename $file` $helpnames_img"
done
for file in ${HELPFILES_STATIC} ; do
  install "$file" "$TMPDIR/share/doc/trustbridge/_static"
  helpnames_static="`basename $file` $helpnames_static"
done
for file in ${HELPFILES_SOURCES}; do
  install "$file" "$TMPDIR/share/doc/trustbridge/_sources"
  helpnames_sources="`basename $file` $helpnames_sources"
done

echo "Building $INSTALLER and $UNINSTALLER ..."

cp "@CMAKE_BINARY_DIR@/packaging/linux-installer.inc" "$INSTALLER"
cp "@CMAKE_BINARY_DIR@/packaging/linux-uninstaller.inc" "$UNINSTALLER"

declare -a INSTALLER_SCRIPTS
INSTALLER_SCRIPTS=("$INSTALLER" "$UNINSTALLER")

for script in "${INSTALLER_SCRIPTS[@]}" ; do
  process_includes "$script" "@CMAKE_BINARY_DIR@/packaging"
  sed -i "s/###BINNAMES###/${binnames}/" "$script"
  sed -i "s/###ICONNAME###/trustbridge.png/" "$script"
  sed -i "s/###HELPNAMES###/${helpnames}/" "$script"
  sed -i "s/###HELPNAMES_SOURCES###/${helpnames_sources}/" "$script"
  sed -i "s/###HELPNAMES_STATIC###/${helpnames_static}/" "$script"
  sed -i "s/###HELPNAMES_IMG###/${helpnames_img}/" "$script"
  sed -i "s/###ARCH###/${ARCH}/" "$script"
  chmod +x "$script"
done

echo "Creating shar archive ..."
cd "$TMPDIR"
shar -z -B `find -type f` | sed 's/^exit 0//' >>"$TMPSHAR"

sed -i "/###SHAR###/r $TMPSHAR" "$INSTALLER"

echo "Signing"
"@CMAKE_SOURCE_DIR@/ui/tests/append-sig.sh" \
    "@CMAKE_SOURCE_DIR@/ui/tests/data/codesign/codesigning.key" \
    "$INSTALLER" "${INSTALLER}_signed"
mv "${INSTALLER}_signed" "$INSTALLER"

echo "Cleaning up..."
rm -r "$TMPDIR"
rm  "$TMPSHAR"
