#!/usr/bin/python3
#coding=utf-8
import subprocess
import argparse
import sys

recognized = [ "debian", "arch" ]

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--verbose", "-v", action="store_true")
    parser.add_argument("--expect")
    args = parser.parse_args()

    quiet = not args.verbose

    try:
        with open("/etc/os-release") as f:
            lines = f.read().splitlines()
    except Exception as e:
        print("UNKNOWN - " + str(e))
        sys.exit(3)

    detected_os = None
    like_os = None

    for line in lines:
        if line.startswith("ID_LIKE="):
            for os in recognized:
                if os in line:
                    like_os = os
        if line.startswith("ID="):
            for os in recognized:
                if os in line:
                    detected_os = os

    if not detected_os:
        if like_os:
            detected_os = like_os
        else:
            detected_os = "unknown"

    if detected_os == args.expect:
        print("OK - OS matches")
        sys.exit(0)

    if args.expect != detected_os:
        print("CRITICAL - MISSMATCH DETECTED {detected_os} != {args.expect}".format(**globals()))
        sys.exit(2)
