#!/usr/bin/env bash

cat "${DOTFILES}/git/gitconfig.ini" ~/annex/secret/Git/gitconfig.ini > ~/.gitconfig

EMACS_LAYOUTS_DIR="${HOME}/.emacs.d/.cache/layouts/"
[[ -d "${EMACS_LAYOUTS_DIR}" ]] && {
  cp "${EMACS_LAYOUTS_DIR}"/* ~/Nextcloud/Cache/emacs/
}

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='$'

documented-project-fake-ln() {
  export PROJECT="$1"
  export PROJECT_UC="$2"

  sed "s/__PROJECT_UC__/$PROJECT_UC/" build-site.sh > "$HOME/dev/libre/$PROJECT/scripts/build-site.sh"
  chmod +x "$HOME/dev/libre/$PROJECT/scripts/build-site.sh"
  envsubst < conf.py > "$HOME/dev/libre/$PROJECT/docs/conf.py"
  envsubst < layout.html > "$HOME/dev/libre/$PROJECT/docs/_templates/layout.html"
  envsubst < utils-i18n.nix > "$HOME/dev/libre/$PROJECT/utils-i18n.nix"
  cp assert-docs-spelling.sh "$HOME/dev/libre/$PROJECT/scripts/assert-docs-spelling.sh"

  envsubst < utils.nix > "$HOME/dev/libre/$PROJECT/utils.nix"
  envsubst < preamble.org > "$HOME/dev/libre/$PROJECT/workflow/preamble.org"
  cp workflow.css "$HOME/dev/libre/$PROJECT/workflow/"
  envsubst < pre-push-hook.sh > "$HOME/dev/libre/$PROJECT/.git/hooks/pre-push"
  chmod +x "$HOME/dev/libre/$PROJECT/.git/hooks/pre-push"
  envsubst < ci-build.sh > "$HOME/dev/libre/$PROJECT/scripts/ci-build.sh"
  chmod +x "$HOME/dev/libre/$PROJECT/scripts/ci-build.sh"
  cat <(printf "%s\n\n" "$PROJECT_UC") <(jq -r .description < "$HOME/dev/libre/$PROJECT/metadata.json" | fold -w 72 | sed 's/^ //') <(printf "\nSee: https://%s.euandreh.xyz\n" "$PROJECT") > "$HOME/dev/libre/$PROJECT/README"
  cp breadcrumbs.html "$HOME/dev/libre/$PROJECT/docs/_templates/"
  cp footer.html "$HOME/dev/libre/$PROJECT/docs/_templates/"
  cp assert-shellcheck.sh "$HOME/dev/libre/$PROJECT/scripts/"
  cp assert-nixfmt.sh "$HOME/dev/libre/$PROJECT/scripts/"
  cp assert-todos.sh "$HOME/dev/libre/$PROJECT/scripts/"
  cp shell.nix "$HOME/dev/libre/$PROJECT/"
  envsubst < .build.yml > "$HOME/dev/libre/$PROJECT/.build.yml"
  cp favicon.ico "$HOME/dev/libre/$PROJECT/docs/"
  cp LICENSE "$HOME/dev/libre/$PROJECT/"
}

for r in songbooks mediator intermediauth cement; do
  documented-project-fake-ln "$r" "${r^}"
done

for r in byopds byodb; do
  documented-project-fake-ln "$r" "$(echo "$1" | tr '[:lower:]' '[:upper:]')"
done

for r in boneco website dotfiles; do
  export PROJECT="$r"
  envsubst < simple-pre-push-hook.sh > "$HOME/dev/libre/$r/.git/hooks/pre-push"
  chmod +x "$HOME/dev/libre/$r/.git/hooks/pre-push"
done

for job in annex elfeed backup 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/cron/${job}-job.sh"
done

popd > /dev/null || exit 1
