#!/usr/bin/env python3

# Secinfo-client
#
# See the file LICENSE for copying permission.
#  (MIT license - found at https://slixmpp.readthedocs.io/en/latest/license.html)
#
#   Copyright (c) 2025 John Lines - MIT/Expat

#
# secinfo-client takes a message from the command line and sends it over
# a socket to secinfo-xmpp
#

from argparse import ArgumentParser
import configparser
import os
import sys
import socket




configSocket = "/run/secinfo-xmpp/secinfo.sock"
configPort = 0

def parseConfig():
    global configSocket
    config = configparser.ConfigParser()
    # will look for a config file in argument passed by -f, then
    # .config/secinfo-xmpp/config.ini and then
    #  /etc/secinfo-xmpp/config.ini
    #
    configfile = os.path.expanduser("~/.config/secinfo-xmpp/config.ini")
    if os.path.isfile(configfile):
        config.read(configfile)
    else:
        configfile = "/etc/secinfo-xmpp/config.ini"
        if os.path.isfile(configfile):
            config.read(configfile)
        else:
            sys.exit("Configuration file not found")
    configPort = config['Server'].getint('port')
    configSocket = config['Server'].get('socket','')

if __name__ == '__main__':
    parseConfig()
    argstr=" ".join(sys.argv[1:])
#     print("arguments:"+argstr)
#   handle -h and --help and no argurment string locally


    if os.path.exists(configSocket):
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        client.connect(configSocket)
        client.sendall(argstr.encode())
        response = client.recv(1024)
#         print(f'Received response: {response.decode()}')
        print(response.decode())
        client.close()
    else:
       print("Socket does not exist "+configSocket)

