#!/usr/bin/env python3

from subprocess import check_output
from sys import exit

CONVERSIONS = [
    ('ns', 1/1000000),
    ('us', 1/1000),
    ('ms', 1),
    ('s', 1000),
]

try:
    in_sync = check_output(
        "timedatectl status | grep -iF 'system clock synchronized' | cut -d: -f2",
        shell=True,
    ).strip().decode()

    if in_sync.lower() != 'yes':
        print(f'systemd-timesyncd reports sync status: {in_sync}')
        exit(2)

    out = check_output(
        "timedatectl timesync-status | grep -iF offset | cut -d: -f2",
        shell=True,
    ).strip().decode()

    if not out:
        print('NTP service is active, but could not get offset. Something is wrong.')
        exit(1)

    if out in ('+0', '-0', '0'):
        # if the offset is exactly zero, timedatectl only shows '0', thus
        # breaking the logic below.
        out = '0ms'

    if out.startswith('+'):
        out = out[1:]

    offset = None
    for unit, factor in CONVERSIONS:
        if out.endswith(unit):
            offset = float(out[:(len(unit)*-1)])
            offset = offset * factor
            break
    else:
        raise ValueError(out)

    print(f'Sync offset is {round(offset, 2)} ms')

    if -50 < offset < 50:
        exit(0)
    elif -100 < offset < 100:
        exit(1)
    else:
        exit(2)
except Exception as e:
    print(repr(e))
    exit(3)
