#!/bin/sh

while true; do
    tmpfile=$(mktemp)
    if curl -fSL 'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fnyct_ene_equipments.json' -o "$tmpfile"; then
        filtered=$(jq -c '[.[] | select(.equipmenttype == "EL")]' "$tmpfile")
        if [ "$filtered" != "[]" ]; then
            echo "$filtered" >"$tmpfile"
            mv "$tmpfile" /app/info.json
            chmod 644 /app/info.json
            chown nginx:nginx /app/info.json
        else
            rm "$tmpfile"
        fi
    else
        rm "$tmpfile"
    fi
    sleep 60
done
