#!/usr/bin/env bash
set -x

# xapp gathers the components which are common to multiple GTK desktop
# environments (Cinnamon, MATE and Xfce) and required to implement cross-DE solutions.

# Xreader is a document viewer capable of displaying multiple and single page
# document formats like PDF and Postscript.
# https://github.com/linuxmint/xreader

# Xviewer is a simple image viewer which uses the gdk-pixbuf library.
# https://github.com/linuxmint/xviewer

AUTOMATE_HOME="/var/tmp/automate"
SCRIPT="build_deb_from_dsc_with_update.sh"
BUILD_DIR="/var/tmp/build_xapp"

# https://github.com/linuxmint/xapp
# DSC_FILE="http://packages.linuxmint.com/pool/main/x/xapp/xapp_1.0.0-1.dsc"

# Source: xapp
# Binary: xapps-common, libxapp1, libxapp-dev, gir1.2-xapp-1.0, libxapp-dbg, xapps-doc

# https://github.com/linuxmint/xapps-common
# DSC_FILE="http://packages.linuxmint.com/pool/main/x/xapps-common/xapps-common_1.0.0.dsc"

# Source: xapps-common
# Binary: xapps-common
# https://github.com/linuxmint/xplayer
# DSC_FILE="http://packages.linuxmint.com/pool/main/x/xplayer/xplayer_1.4.3+betsy.dsc"

# Source: xplayer
# Binary: libxplayer0, xplayer, xplayer-mozilla, xplayer-common, xplayer-dbg, xplayer-plugins, xplayer-plugins-extra, gir1.2-xplayer-1.0, libxplayer-dev

# https://github.com/linuxmint/xreader
# DSC_FILE="http://packages.linuxmint.com/pool/main/x/xreader/xreader_1.4.4+betsy.dsc"

# Source: xreader
# Binary: xreader, xreader-dbg, xreader-common, libxreaderview3, libxreaderview-dev, libxreaderview3-dbg, libxreaderdocument3, libxreaderdocument-dev, libxreaderdocument3-dbg, gir1.2-xreader

# https://github.com/linuxmint/xviewer
# DSC_FILE="http://packages.linuxmint.com/pool/main/x/xviewer/xviewer_1.4.3+betsy.dsc"

# Source: xviewer
# Binary: xviewer, xviewer-dbg, xviewer-dev

print_conf_watch() {
  local XAPP="$1"
  cat <<EOF
version: 5
Source: https://github.com/linuxmint/${XAPP}/tags
Matching-Pattern: .*/archive/refs/tags/v?@ANY_VERSION@@ARCHIVE_EXT@   
EOF
}
# conf_print_watch | debian/watch

DSC_URLS=(
  "http://packages.linuxmint.com/pool/main/x/xapp/xapp_1.0.3+betsy.dsc"
  "http://packages.linuxmint.com/pool/main/x/xplayer/xplayer_1.4.3+betsy.dsc"
  "http://packages.linuxmint.com/pool/main/x/xreader/xreader_1.4.4+betsy.dsc"
  "http://packages.linuxmint.com/pool/main/x/xviewer/xviewer_1.4.3+betsy.dsc"
)

for DSC_URL in "${DSC_URLS[@]}"; do
  echo "Building from $DSC_URL"
  bash "${AUTOMATE_HOME}/${SCRIPT}" "${BUILD_DIR}" "$DSC_URL"
  FILENAME=$(basename "$DSC_URL")
  XAPP="${FILENAME%%_*}"

  if [[ -d "${BUILD_DIR}/debian" ]]; then
    conf_print_watch "${XAPP}" | tee "${BUILD_DIR}/debian/watch"
  fi

  if [ $? -ne 0 ]; then
    echo "Build failed for $DSC_URL"
    exit 1
  fi
done
