#!/bin/bash

REPO_LIST=(
  rabuchaim/geolite2mirror
  MetaCubeX/mihomo
  MetaCubeX/ClashMetaForAndroid
  2dust/v2rayNG
  shadowsocks/shadowsocks-windows
  MetaCubeX/meta-rules-dat
  MetaCubeX/clash-verge
  2dust/v2rayN
  prometheus-community/windows_exporter
  SagerNet/sing-box
  RocketChat/Rocket.Chat.Electron
)

template(){
  local DIR REPO BASE_DIR BASE_URL
  BASE_DIR="/var/www/drive/git-releases"
  BASE_URL="https://api.github.com/repos"
  DIR="$BASE_DIR/$1"
  REPO="$2"

  mkdir -p "$DIR" 2> /dev/null
  cd "$DIR" || exit
  if [[ "$PWD" == "$DIR" ]]; then
      rm -rf ./*
  fi

  curl -s "$BASE_URL/$REPO/releases/latest" \
  | grep "browser_download_url" \
  | cut -d : -f 2,3 \
  | tr -d \" \
  | wget -i -


}

maxmind(){
    cp /var/www/drive/git-releases/maxmind/{GeoLite2-ASN.mmdb,GeoLite2-City.mmdb,GeoLite2-Country.mmdb} /home/bea/apps/ipinfo/geolite2/ 2> /dev/null
    docker restart echoip
}


for list in "${REPO_LIST[@]}"; do
  repo_name=$(echo "$list" | cut -d "/" -f2)

  if [[ "$repo_name" == "mihomo" ]]; then
      repo_name="clash"
  elif [[ "$repo_name" == "geolite2mirror" ]]; then
    repo_name="maxmind"
    cp /var/www/drive/git-releases/maxmind/{GeoLite2-ASN.mmdb,GeoLite2-City.mmdb,GeoLite2-Country.mmdb} /home/bea/apps/ipinfo/geolite2/
    docker restart echoip
  fi

  template "$repo_name" "$list"

done
