#!/usr/bin/sh
set -ex

# Temporarily skip the AutoYaST XML validation
export YAST_SKIP_XML_VALIDATION=1

if [ -z "$1" ]; then
  url=$(grep '\(agama\|inst\).auto=' < /run/agama/cmdline.d/agama.conf | awk -F ':?(inst|agama).auto=' '{sub(/ .*$/, "", $2); print $2}')
else
  url="$1"
fi

if [ -z "$url" ]; then
  echo "no autoinstallation profile"
  exit 0
fi

method=$(grep '\(agama\|inst\).finish=' </run/agama/cmdline.d/agama.conf | awk -F ':?(inst|agama).finish=' '{sub(/ .*$/, "", $2); print $2}')

echo "Using the profile at $url"

case "$url" in
*)
  /usr/bin/agama profile import "$url"
  /usr/bin/agama install
  case "$method" in
  "stop" | "halt" | "poweroff")
    /usr/bin/agama finish "$method"
    ;;
  *)
    /usr/bin/agama finish
    ;;
  esac
  ;;
esac
