#!/usr/bin/env python3

from requests import get
from sys import argv, exit

if not len(argv) == 3:
    print('Usage: check_github_for_new_release <repo> <version>')
    exit(3)

repo = argv[1]
current_version = argv[2]

try:
    r = get('https://api.github.com/repos/{}/releases/latest'.format(repo), headers={'Accept': 'application/json'}).json()
except Exception as e:
    print(repr(e))
    exit(3)

newest_release = r['tag_name']

if newest_release != current_version:
    print('There is a newer version available: {} (currently installed: {})'.format(newest_release, current_version))

    if r['prerelease'] or r['draft']:
        print('Please note this is only a prerelease/draft.')

        # Don't alert people for prereleases.
        exit(0)
    else:
        exit(2)
else:
    print('Currently installed version matches newest release on github')
    exit(0)