#!/usr/bin/env bash
# Makes a patch that takes an app generated from one version of the LibreSharp SaaS Starter
# template to another: it generates the app twice, with your name and options, and diffs them.
# Apply it in your app's repository with `git apply -3 upgrade.patch`.
#
# Usage, at the root of an app created from 0.9.0 or later (it reads .saas-starter.json):
#   upgrade-patch.sh <to version>                    e.g. upgrade-patch.sh 0.10.0
# For an older app, give the version, name and options it was created with:
#   upgrade-patch.sh <from version> <to version> <app name> [options...]
#   e.g. upgrade-patch.sh 0.8.0 0.9.0 MyApp --Persistence efcore --Auth saas
#
# A version can also be a folder with the template's source, such as a clone of the repository,
# to try an upgrade to (or from) a version that isn't released yet.
#
# Needs the .NET SDK and git. It installs both versions in a throwaway template folder
# (--debug:custom-hive), so the template you have installed is left alone.
set -euo pipefail

usage() {
  sed -n '2,16p' "$0" | sed 's/^# \{0,1\}//'
  exit 1
}

if [ $# -eq 1 ]; then
  [ -f .saas-starter.json ] || { echo "No .saas-starter.json here: run this at your app's root, or give the version, name and options (below)."; echo; usage; }
  # The file is written by the template, one value per line.
  field() { sed -n "s/^ *\"$1\": *\"\([^\"]*\)\".*/\1/p" .saas-starter.json | head -1; }
  from=$(field version) to=$1 name=$(field name)
  set -- --Persistence "$(field Persistence)" --Auth "$(field Auth)" \
    --IncludeTests "$(field IncludeTests | tr '[:upper:]' '[:lower:]')" \
    --Aspire "$(field Aspire | tr '[:upper:]' '[:lower:]')"
  # Apps created from 0.10.1 on record their display name too (--DisplayName).
  display=$(field displayName)
  if [ -n "$display" ]; then set -- "$@" --DisplayName "$display"; fi
  if [ "$from" = 0.0.0-dev ]; then
    echo "This app was created from an unreleased template (version 0.0.0-dev), so there's no version to upgrade from."
    echo "Give the version it is closest to instead: upgrade-patch.sh <from> $to $name $*"
    exit 1
  fi
  echo "Upgrading $name from $from to $to, with $*"
elif [ $# -ge 3 ]; then
  from=$1 to=$2 name=$3
  shift 3
else
  usage
fi
if [ "$from" = "$to" ]; then
  echo "The app is already at $to."
  exit 1
fi

package=LibreSharp.Templates.SaasStarter
output=${UPGRADE_PATCH:-upgrade.patch}
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT

generate() {
  local version=$1 dir=$2 label=$3 source
  shift 3
  # A version from nuget.org, or a folder with the template's source.
  if [ -d "$version" ]; then source=$(cd "$version" && pwd); else source="$package@$version"; fi
  dotnet new install "$source" --debug:custom-hive "$work/hive-$label" > "$work/install-$label.log" \
    || { cat "$work/install-$label.log"; exit 1; }
  dotnet new saas-starter -n "$name" -o "$dir" --debug:custom-hive "$work/hive-$label" "$@" > "$work/new-$label.log" \
    || { cat "$work/new-$label.log"; exit 1; }
}

# The old version, committed, then replaced by the new one: git sees what was added, changed,
# renamed and deleted.
generate "$from" "$work/repo" from "$@"
git -C "$work/repo" init -q
git -C "$work/repo" add -A
git -C "$work/repo" -c user.name=upgrade -c user.email=upgrade@localhost commit -qm "$from"
generate "$to" "$work/new" to "$@"
find "$work/repo" -mindepth 1 -maxdepth 1 ! -name .git -exec rm -rf {} +
cp -a "$work/new/." "$work/repo/"
git -C "$work/repo" add -A
git -C "$work/repo" diff --cached --binary -M > "$output"

echo "Wrote $output: $(git -C "$work/repo" diff --cached --shortstat)"
echo "Apply it at the root of your app's repository with: git apply -3 $output"
