#!/bin/bash

loadSecretsRepo() {
  secretsGitPath="ssh://secrets-git-host/neintonine/yadm-config-secrets.git"
  secretsFolderPath="$HOME/.local/share/secrets"
  configFile="$HOME/.ssh/config"

  if [[ -d "$secretsFolderPath/.git" ]]; then
    git --git-dir "$secretsFolderPath/.git" --work-tree "$secretsFolderPath" pull --ff-only
    return 0
  fi

  if [[ ! -f "$configFile" ]]; then
    echo "Can't find the ssh config file!"
    return 1
  fi

  if ! grep "Host secrets-git-host" "$configFile" >/dev/null; then
    echo "Can't find secrets-git-host config"
    return 1
  fi

  git clone "$secretsGitPath" "$secretsFolderPath"
}

echo "# Updating secrets"
loadSecretsRepo
"$secretsFolderPath/deploy.sh"
