#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash wget mb2md
# shellcheck shell=bash
set -Eeuo pipefail

MAILING_LIST_PROVIDER="${1:-}"
MAILING_LIST_NAME="${2:-}"
GNU_MBOX_ARCHIVE_URL='ftp://lists.gnu.org'
OUT_MAILDIR="$HOME/mbsync/EuAndreh/mailing-lists/"
TMP_DIR="$HOME/tmp/mbox-import"
usage() {
  cat <<EOF
Usage:
    $0 <MAILING_LIST_PROVIDER> <MAILING_LIST_NAME>

      Arguments
        MAILING_LIST_NAME      The name of the mailing list, e.g.: info-guix.
        MAILING_LIST_PROVIDER  The name of the host of the mailing list, e.g.: gnu.
                               Supported providers:
                                   'gnu' for GNU mailing lists
                                   'google' for Google Groups

Examples:
    Download and import into mbsync the MBOX archive from info-guix mailing-list:
        mailing-list-import-mbox.sh gnu info-guix

    Download and import into mbsync the contents for lisp-br Google Groups:
        mailing-list-import-mbox.sh google lisp-br
EOF
}

gnu_mailing_list_import() {
  mkdir -p "${TMP_DIR}"
  pushd "${TMP_DIR}"

  # Subscribe to the mailing list
  local -r recipient="${MAILING_LIST_NAME}-join@gnu.org"
  local -r subject="Subscribe to ${MAILING_LIST_NAME} mailing list"
  mail "$recipient"  \
       -s "$subject" \
       -aFrom:mailing-list@euandre.org <<<''

  # Download existing archive as MBOX files
  wget -c -r "${GNU_MBOX_ARCHIVE_URL}/${MAILING_LIST_NAME}/"

  # Concatenate all MBOX files into as single MBOX file
  cat "lists.gnu.org/${MAILING_LIST_NAME}/"* > "${MAILING_LIST_NAME}.all.mbox"

  # Transform the MBOX file into a MailDir format and put it on the right place
  mb2md -s "${TMP_DIR}/${MAILING_LIST_NAME}.all.mbox" -d "${OUT_MAILDIR}"
  popd
}

google_groups_import() {
  [[ -f ~/dev/misc/google-group-crawler/crawler.sh ]] || {
    red "Missing ${HOME}/dev/misc/google-group-crawler/crawler.sh file."
    red "Clone it before importing Google Groups emails."
    exit 1
  }
  mkdir -p "${TMP_DIR}"
  pushd "${TMP_DIR}"

  # Subscribe to the mailing list
  local -r recipient="${MAILING_LIST_NAME}+subscribe@googlegroups.com"
  local -r subject="Subscribe to ${MAILING_LIST_NAME} mailing list"
  mail.sh "${recipient}" "${subject}" ' ' 'mailing-list@euandre.org'

  # Download existing messages into MBOX Files
  CRAWLER_FILE="${TMP_DIR}/${MAILING_LIST_NAME}-crawler.sh"
  export _GROUP="${MAILING_LIST_NAME}"
  ~/dev/misc/google-group-crawler/crawler.sh -sh > "${CRAWLER_FILE}"
  bash "${CRAWLER_FILE}"

  cat "${MAILING_LIST_NAME}/mbox/"* > "${MAILING_LIST_NAME}.all.mbox"

  echo before
  echo mb2md -s "${TMP_DIR}/${MAILING_LIST_NAME}.all.mbox" -d "${OUT_MAILDIR}"
  echo after
  popd
}

main() {
  [[ -z "${MAILING_LIST_PROVIDER}" ]] && {
    red "Missing input argument 'MAILING_LIST_PROVIDER'.\n"
    usage
    exit 2
  }

  [[ -z "${MAILING_LIST_NAME}" ]] && {
    red "Missing input argument 'MAILING_LIST_NAME'.\n"
    usage
    exit 2
  }

  case "${MAILING_LIST_PROVIDER}" in
    "gnu")
      gnu_mailing_list_import
      ;;
    "google")
      google_groups_import
      ;;
    *)
      red "Unsupported mailing list provider '${MAILING_LIST_PROVIDER}'."
      usage
      exit 2
      ;;
  esac

  echo "${MAILING_LIST_PROVIDER} ${MAILING_LIST_NAME}" >> ~/annex/txt/mailing-list-subscriptions.txt

  green "Done."
  yellow "Make sure to add the correct tagging rules for notmuch post-hook!"
  blue "Remember to sync new changes by running sync-mail.sh!"
}

main "$@"
