#!/usr/bin/env bash
# shellcheck disable=2043

mkdir -p ~/.tmuxinator

for template in "${DOTFILES}"/tmux/projects/*; do
  cp "${template}" "${HOME}/.tmuxinator/$(basename "$template")"
done

mkdir -p ~/.mozilla/native-messaging-hosts/
cp "${DOTFILES}/org.keepassxc.keepassxc_browser.json" ~/.mozilla/native-messaging-hosts/

pushd "${DOTFILES}/bash/templates/" > /dev/null || exit 1
export DOLLAR='$'

for job in annex elfeed backup copy-box-backups repo-update notmuch-dump mbox-archive nix-search-update mr-remotes-backup; do
  sed "s/__JOB__/$job/" cronjob-with-email.sh > "$HOME/dev/libre/dotfiles/scripts/cron/${job}-job.sh"
  chmod +x "$HOME/dev/libre/dotfiles/scripts/ad-hoc/${job}.sh"
  chmod +x "$HOME/dev/libre/dotfiles/scripts/cron/${job}-job.sh"
done

for job in pacheco; do
  prefix="$HOME/annex/bin/ad-hoc"
  sed "s/__JOB__-job/$job-job/" cronjob-with-email.sh | sed "s|.*__JOB__.*|$prefix/$job.sh|" > "$HOME/annex/bin/cron/${job}-job.sh"
  chmod +x "$HOME/annex/bin/ad-hoc/${job}.sh"
  chmod +x "$HOME/annex/bin/cron/${job}-job.sh"
done

normalize-name() {
  if [[ "$1" = ..* ]]; then
    echo "${1:1}"
  else
    echo "$1"
  fi
}

cp-all() {
  for f in "$@"; do
    mkdir -p "$(dirname "$PREFIX/$f")"
    cp "$f" "$PREFIX/$(normalize-name "$f")"
  done
}

cp-if-not() {
  for f in "$@"; do
    if [[ ! -f "$PREFIX/$(normalize-name "$f")" ]]; then
      mkdir -p "$(dirname "$PREFIX/$f")"
      cp "$f" "$PREFIX/$(normalize-name "$f")"
    fi
  done
}

envsubst-all() {
  for f in "$@"; do
    mkdir -p "$(dirname "$PREFIX/$f")"
    envsubst < "$f" > "$PREFIX/$(normalize-name "$f")"
  done
}

envsubst-if-not() {
  for f in "$@"; do
    if [[ -n "$RENAME_TO" ]]; then
      OUT_FILE="$PREFIX/$RENAME_TO"
    else
      OUT_FILE="$PREFIX/$(normalize-name "$f")"
    fi
    if [[ ! -f "$OUT_FILE" ]]; then
      mkdir -p "$(dirname "$OUT_FILE")"
      envsubst < "$f" > "$OUT_FILE"
    fi
  done
}

sed-all() {
  for f in "$@"; do
    mkdir -p "$(dirname "$PREFIX/$f")"
    sed "s/__PROJECT_UC__/$PROJECT_UC/" "$f" > "$PREFIX/$(normalize-name "$f")"
  done
}

sed-if-not() {
  for f in "$@"; do
    if [[ ! -f "$PREFIX/$(normalize-name "$f")" ]]; then
      mkdir -p "$(dirname "$PREFIX/$f")"
      sed "s/__PROJECT_UC__/$PROJECT_UC/" "$f" > "$PREFIX/$(normalize-name "$f")"
    fi
  done
}

chmod+x-all() {
  for f in "$@"; do
    chmod +x "$PREFIX/$(normalize-name "$f")"
  done
}

ln-env() {
  export PROJECT="$1"
  export PREFIX="$HOME/dev/libre/$PROJECT"
  export MAILING_LIST="${ML_FOR_PROJECT[$PROJECT]:-public-inbox}"
  export PROJECT_UC="${PROJECT_NAMES[$PROJECT]:-$PROJECT}"
}

ln-init() {
  if ! grep "$PROJECT" ~/annex/bin/misc/git/mrconfig.ini > /dev/null; then
    red "$(printf '%s missing from mrconfig.ini!\n' "$PROJECT")"
  fi

  if [[ ! -d "$PREFIX" ]]; then
    git init "$PREFIX"
  fi
}

ln-autotools() {
  cp-all AUTHORS bootstrap ChangeLog INSTALL
  cp-if-not description

  # assumes:
  # - ln-worflow for .gitignore
  # - ln-git-notes-release for Makefile.am
  # - ln-guix-container for Makefile.am
  envsubst-if-not ..gitignore configure.ac Makefile.am README
}

ln-workflow() {
  cp-all workflow/vendor/org.css      \
         workflow/vendor/htmlize.el   \
         workflow/workflow.css        \
         scripts/assert-todos.sh      \
         scripts/assert-shellcheck.sh \
         scripts/generate-tasks-and-bugs.sh
  cp-if-not TODOs.org
  envsubst-all workflow/preamble.org
}

ln-agpl() {
  cp-all COPYING
}

ln-guix-container() {
  cp-all scripts/with-container.sh scripts/assert-shellcheck.sh
  cp-if-not guix/channels.scm guix/manifest.scm
}

ln-ci() {
  cp-all scripts/assert-shellcheck.sh

  if [[ -f "$PREFIX/configure.ac" ]]; then
    export AUTOTOOLS_SETUP='./bootstrap && ./configure && '
  else
    export AUTOTOOLS_SETUP=''
  fi
  envsubst-all scripts/ci-build.sh ..git/hooks/pre-push
  chmod+x-all  scripts/ci-build.sh ..git/hooks/pre-push
}

ln-git-notes-release() {
  cp-all build-aux/git-notes-release.am
  cp-if-not NEWS
}

ln-dicts() {
  cp-all doc/dictionaries/international.txt doc/dictionaries/en.txt
}

ln-texinfo() {
  envsubst-all HACKING
  RENAME_TO="doc/$PROJECT.texi" envsubst-if-not rename/empty-texinfo.texi
  cp-all build-aux/texinfo.tex
  chmod +w "$PREFIX/build-aux/texinfo.tex" ||:
  ln-dicts
}

ln-sphinx() {
  ln-dicts
  cp-all scripts/assert-docs-spelling.sh doc/_templates/footer.html doc/favicon.ico
  envsubst-all doc/conf.py.in doc/_templates/layout.html
  envsubst-if-not doc/index.rst
  sed-all scripts/build-sphinx.sh
  chmod+x-all /scripts/build-sphinx.sh
  cp-if-not doc/i18n-pagenames.json doc/Makefile
}

ln-scribble() {
  ln-dicts
}

ln-antora() {
  ln-dicts
}

ln-base-autotools-guix-project() {
  ln-env "$1"
  ln-init

  ln-workflow
  ln-guix-container
  ln-git-notes-release
  ln-autotools

  ln-agpl
  ln-ci
}

ln-guile-files() {
  if [[ "$PROJECT" = guile* ]]; then
    cp-all build-aux/guile.am build-aux/pre-inst-env.in m4/guile.m4
  fi
}

ln-texinfo-autotools-guix-project() {
  ln-base-autotools-guix-project "$1"
  ln-texinfo
  ln-guile-files
}

ln-sphinx-autotools-guix-project() {
  ln-base-autotools-guix-project "$1"
  ln-sphinx
}

ln-scribble-autotools-guix-project() {
  ln-base-autotools-guix-project "$1"
  ln-scribble
}

ln-antora-autotools-guix-project() {
  ln-base-autotools-guix-project "$1"
  ln-antora
}

ln-simple-makefile() {
  envsubst-if-not Makefile
}

ln-generated-readme() {
  cat <(printf "%s\n\n" "$PROJECT_UC")                        \
      description                                             \
      <(printf "\nSee: https://%s.euandreh.xyz\n" "$PROJECT") \
      > "$PREFIX/README"
}

ln-makefile-only-guix-project() {
  ln-env "$1"
  ln-agpl
  ln-workflow
  ln-guix-container
  ln-ci
  ln-simple-makefile
}

ln-bare-project() {
  ln-agpl

  if [[ -f "$PREFIX/Makefile" ]]; then
    export DISABLE_CI=''
    envsubst-all scripts/ci-build.sh
    chmod+x-all  scripts/ci-build.sh
  else
    export DISABLE_CI='# '
  fi
  envsubst-all ..git/hooks/pre-push
  chmod+x-all  ..git/hooks/pre-push
}

declare -A PROJECT_NAMES=(
  [vps]=VPS
  [cement]=Cement
  [parsecc]=ParsecC
)

declare -A ML_FOR_PROJECT=(
  [cement]=byopds
)

ln-all() {
  LN_FN="$1"
  shift
  for f in "$@"; do
    "$LN_FN" "$f"
  done
}

ln-all ln-texinfo-autotools-guix-project  guile-quickcheck guile-pds cement
ln-all ln-sphinx-autotools-guix-project   libedn x-bindgen eq mediator
ln-all ln-scribble-autotools-guix-project songbooks
ln-all ln-antora-autotools-guix-project   byopds
ln-all ln-makefile-only-guix-project      vps parsecc
ln-all ln-bare-project parseclj boneco brinquedoteca casa-do-caminho dotfiles euandreh-guix-channel music pdfs-da-d-maria swift2nix swift2nix-demo cargo2nix cargo2nix-demo website

popd > /dev/null || exit 1
