#!/opt/alt/python35/bin/python3
#
# Wait for PAM socket is ready
#

import socket as s
import sys
import time

PAMSOCK_PATH = '/opt/i360_pam_imunify/pam_imunify360.sock'
RETRIES = 10
DELAY = 1


if __name__ == '__main__':
    pamsock = s.socket(s.AF_UNIX, s.SOCK_STREAM)
    for attempt in range(RETRIES):
        if attempt > 0:
            time.sleep(DELAY)
        try:
            pamsock.connect(PAMSOCK_PATH)
        except (ConnectionRefusedError, FileNotFoundError) as e:
            print("PAM socket error:", e, file=sys.stderr)
        else:
            print("PAM socket is ready.")
            sys.exit(0)
    else:
        sys.exit("Failed to connect PAM socket in time.")
