apiVersion: v1
data:
  mod-downloader.sh: |
    modDir=/factorio/mods
    MOD_BASE_URL="https://mods.factorio.com"
    declare -a officialMods
    officialMods=(
      Krastorio2
      StorageTank2_Updated
      early-robots
    )
    declare -A unofficialMods
    unofficialMods["Squeak Through_1.8.2.zip"]="https://github.com/Suprcheese/Squeak-Through/archive/refs/tags/1.8.2.zip"
    function print_step()
    {
      echo "$1"
    }
    function print_success()
    {
      echo "$1"
    }
    function print_failure()
    {
      echo "$1"
    }
    function downloadUnofficial() {
        cd $modDir;curl -L -o $2 $1
    }
    function downloadofficial() {
        if [[ -z ${USERNAME:-} ]]; then
          USERNAME="$(jq -j ".username" "/factorio/configs/server-settings.json")"
        fi

        if [[ -z ${TOKEN:-} ]]; then
          TOKEN="$(jq -j ".token" "/factorio/configs/server-settings.json")"
        fi

        if [[ -z ${USERNAME:-} ]]; then
          echo "You need to provide your Factorio username to update mods."
        fi

        if [[ -z ${TOKEN:-} ]]; then
          echo "You need to provide your Factorio token to update mods."
        fi
        MOD_INFO_URL="$MOD_BASE_URL/api/mods/$1"
        MOD_INFO_JSON=$(curl --silent "$MOD_INFO_URL")
        #echo "$MOD_INFO_URL $MOD_INFO_JSON"
        if ! echo "$MOD_INFO_JSON" | jq -e .name >/dev/null; then
          print_success "  Custom mod not on $MOD_BASE_URL, skipped."
          return 0
        fi
        MOD_INFO=$(echo "$MOD_INFO_JSON" | jq -j --arg version "$VERSION" ".releases|reverse|map(select(.info_json.factorio_version as \$mod_version | \$version | startswith(\$mod_version)))[0]|.file_name, \";\", .download_url, \";\", .sha1")
        echo $MOD_INFO
        MOD_FILENAME=$(echo "$MOD_INFO" | cut -f1 -d";")
        MOD_URL=$(echo "$MOD_INFO" | cut -f2 -d";")
        MOD_SHA1=$(echo "$MOD_INFO" | cut -f3 -d";")
        if [[ $MOD_FILENAME == null ]]; then
          print_failure "  Not compatible with version"
          return 0
        fi
        print_step "Downloading..."
        FULL_URL="$MOD_BASE_URL$MOD_URL?username=$USERNAME&token=$TOKEN"
        echo $FULL_URL
        HTTP_STATUS=$(curl --silent -L -w "%{http_code}" -o "$modDir/$MOD_FILENAME" "$FULL_URL")

        if [[ $HTTP_STATUS != 200 ]]; then
          print_failure "  Download failed: Code $HTTP_STATUS."
          rm -f "$modDir/$MOD_FILENAME"
          return 1
        fi

        if [[ ! -f $modDir/$MOD_FILENAME ]]; then
          print_failure "  Downloaded file missing!"
          return 1
        fi

        if ! [[ $(sha1sum "$modDir/$MOD_FILENAME") =~ $MOD_SHA1 ]]; then
          print_failure "  SHA1 mismatch!"
          rm -f "$modDir/$MOD_FILENAME"
          return 1
        fi

        print_success "  Download complete."
    }
    mkdir -p $modDir
    for key in "${!unofficialMods[@]}"; do
      downloadUnofficial "${unofficialMods[$key]}" $key
    done
kind: ConfigMap
metadata:
  labels:
    app: RELEASE-NAME-factorio-server-charts
    chart: RELEASE-NAME-factorio-server-charts
    heritage: Helm
    release: RELEASE-NAME
  name: RELEASE-NAME-factorio-server-charts-mod-downloader-configmap
