#!/usr/libexec/platform-python

# Usage: pbench-add-metalog-option <metadata log file> <section> <option>

# Add an option to a section of the metadata.log file.
# E.g. using an 'iterations' arg for the option
#
# iterations: 1-iter, 2-iter, 3-iter
#
# where the iterations are in the <iterations.file>, one iteration per line.

import sys
from configparser import ConfigParser, NoSectionError

def main(lfile, section, option):
    config = ConfigParser()
    config.read(lfile)
    # python3
    # config[section][option] = ', '.join(sys.stdin.read().split())
    sin = sys.stdin.read().replace("%","%%")
    try:
        config.set(section, option, ', '.join(sin.split()))
    except NoSectionError:
        config.add_section(section)
        config.set(section, option, ', '.join(sin.split()))
    config.write(open(lfile, "w"))
   
if __name__ == '__main__':
   lfile = sys.argv[1]
   section = sys.argv[2]
   option = sys.argv[3]
   status = main(lfile, section, option)
   sys.exit(status)
