#!/usr/bin/env python2

conf = '/etc/malmon/malmon.conf'

import socket
from os import popen, path
from sys import argv, exit
from ConfigParser import ConfigParser

def usage():
	print 'Usage:'
	print '\t', argv[0], '<arguments> <path>'
	print '\t\targuments\t- find syntax'
	print '\t\tpath\t\t- path to the dir you want to scan'
	print

def send(msg, sockfile):
	msg = "f:" + msg
	sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
	sock.connect(sockfile)
	sock.send(msg)
	sock.close()


# Check args
if len(argv) == 1:
	usage()
	exit(0)

# Read conf
config = ConfigParser()
config.read(conf)
sockfile = config.get("global", "sockfile")

if not path.exists(sockfile):
	print "malmon is not running."
	print"malmon must be running to use this option."
	exit()

cmd = "/usr/bin/find " + " ".join(argv[1:]) + " -type f"
result = popen(cmd, 'r')

while True:
	line = result.readline().strip()
	if line: send(line, sockfile)
	else: break
	print "Adding:", line

print 'Finish!'
exit()