import sys
import os
from json import load as jsonDec

# File config
executeAppCommand = './record.sh'
appName = 'record'

# Streams owners file
streamPublishersFile = 'streamPublishers.json'

#  Child
def startStream(publisher_name,publisher_url, record_time):
	os.execl(executeAppCommand, appName, publisher_name, publisher_url, record_time)
	os._exit(0)

# Parent
def streamsInitializer(record_time):

	with open(streamPublishersFile) as data:
		publishers_data = jsonDec(data)

	for i in range(len(publishers_data)):
		new_pid = os.fork()
		if(new_pid == 0):
			startStream(publishers_data[i]["publisher"], publishers_data[i]["url"], record_time)
		else:
			pids = (os.getpid(), new_pid)
			print("parent: %d, child: %d\n" % pids)

def main():

	if(len(sys.argv) < 2):
		print("Record time required - eg. python startRecorders.py 60")
	else:
		record_time = sys.argv[1]
		streamsInitializer(record_time)

if __name__ == '__main__':
	main()