#!/bin/sh
# -*- Mode: sh; indent-tabs-mode: nil; tab-width: 2 -*-
#
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: Michael Terry

set -ex

cd flatpak

for PKG in rclone restic; do
  URL=$(yq -r ".modules[] | select(.name == \"$PKG\") | .sources[].url" org.gnome.DejaDupDevel.yaml)
  TAG=$(yq -r ".modules[] | select(.name == \"$PKG\") | .sources[].tag" org.gnome.DejaDupDevel.yaml)
  SRC=../_build/golang/$PKG

  if [ -d $SRC ]; then
    git -C $SRC fetch
    git -C $SRC checkout $TAG
  else
    mkdir -p $SRC
    git clone --branch $TAG $URL $SRC
  fi

  go run github.com/dennwc/flatpak-go-mod@1642390 -module-name $PKG $SRC

  # REUSE-IgnoreStart
  sed -i '1i# SPDX-License-Identifier: CC0-1.0\n# SPDX-FileCopyrightText: Michael Terry\n# This is a generated file, run ./flatpak/generate-go.sh in the main deja-dup repo to rebuild\n' $PKG.go.mod.yml $PKG.modules.txt
  # REUSE-IgnoreEnd
done

echo "All done!"
