#!/usr/bin/env python3
# -*- coding: UTF-8 -*-


import math
import jinja2
import optparse
import os
import shutil
import time


example_gnuplot = """
set terminal wxt


#set xdata time # tells gnuplot the x axis is time data
#set timefmt '%s' # specify our time string format
#set format x '%H' # otherwise it will show only MM:SS

set title "GnuPlot Example"
set xlabel "X"
set ylabel "Y"

set key autotitle columnhead
set datafile separator ','

plot 'example.csv' using 1:2 with lines title "title 1", \
     ''            using 1:3 with lines title "title 2", \
     ''            using 1:2 with lines, \
     ''            using 1:3 with lines


pause -1 "Hit any key to continue"
"""

example_csv = "x, sin(x)/x, sin(x)\n"
for i in range (-500, 500):
    x = 3* i * math.pi / 500
    f2_x = math.sin(x)
    if x == 0:
        f1_x = 1
    else:
        f1_x = f2_x / x
    example_csv += "%f, %f, %f\n" % (x, f1_x, f2_x)


def mkdir(path):
    """.. warning:: Needs to be documented
    """
    path=os.path.abspath(path)
    if not os.path.exists(os.path.dirname(path)):
        mkdir(os.path.dirname(path))
    if not os.path.exists(path):
        os.mkdir(path)
    return os.path.isdir(path)


if __name__ == "__main__":
    parser = optparse.OptionParser("usage: %%prog [options] folder_for_document")
    parser.add_option("-f", "--force", dest="force", action="store_true", default=False)
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.print_help()
    else:
        target_path = os.path.join(os.path.abspath('.'), args[0])

        #
        # Main Actions
        #
        if not os.path.exists(target_path) or options.force:
            mkdir(target_path)
            with open(os.path.join(target_path, 'example.gnuplot'), 'w+') as fh:
                fh.write(example_gnuplot)
            with open(os.path.join(target_path, 'example.csv'), 'w+') as fh:
                fh.write(example_csv)
        else:
            print("Folder exists. Use option -f to force creation")
