#!/bin/sh

# SPDX-License-Identifier: AGPL-3.0-or-later
# Yuval Langer

if ! command -v git >/dev/null 2>&1; then
  printf "Please install git."
  exit 1
fi

nitter_wiki_address="https://github.com/zedeus/nitter.wiki.git"
nitter_wiki_directory="${HOME}/.cache/get-nitter-instances"
instances_filename="Instances.md"

if [ "$1" = "--update" ]; then
  do_update="true"
fi

(
  git clone "${nitter_wiki_address}" "${nitter_wiki_directory}" >/dev/null 2>&1 \
    || (if [ -n "${do_update}" ]; then
      cd "${nitter_wiki_directory}"
      git pull -v >/dev/null 2>&1
    fi)
) \
  && grep white_check_mark "${nitter_wiki_directory}/${instances_filename}" \
  | grep 'white_check_mark:\s*|\s*:white_check_mark' \
    | sed "
s#white_check_mark.*##;
s#.*](##;
s#).*##" \
    | sort -h \
    | uniq
