#!/bin/bash

cat << EOF
{ "aaData":
[
EOF

suite2version() {
    case $1 in
        precise)     echo "12.04 LTS";;
        trusty)      echo "14.04 LTS";;
        xenial)      echo "16.04 LTS";;
        bionic)      echo "18.04 LTS";;
        disco)       echo "19.04";;
        eoan)        echo "19.10";;
        focal)       echo "20.04 LTS";;
        groovy)      echo "20.10";;
        hirsute)     echo "21.04";;
        impish)      echo "21.10";;
        jammy)       echo "22.04";;
        kinetic)     echo "22.10";;
        lunar)       echo "23.04";;
        mantic)      echo "23.10";;
        *)           echo "unsupported";;
    esac
}

for index in $(find /srv/ec2-images/query* -iname "released.current.txt")
do
    while read suite stream tag serial itype arch region ami aki virttype
    do
        version=$(suite2version ${suite})
        if [ "${version}" == "unsupported" ]; then
            continue
        fi
        [ "${aki}" == "hvm" ] && itype="hvm:${itype}"

        console="console.aws.amazon.com"
        case ${region} in
            *gov*) console="console.amazonaws-us-gov.com";;
            cn-*)  console="console.amazonaws.cn";;
        esac

        printf '["%s","%s","%s","%s","%s","%s","<a href=\\"https://%s/ec2/home?region=%s#launchAmi=%s\\">%s</a>","%s"],\n' \
                "${region}" "${suite}" "${version}" "${arch}" "${itype}" "${serial}" "${console}" "${region}" "${ami}" "${ami}" "${aki}"
    done < $index
done

cat <<EOF
]
}
EOF

