#!/usr/bin/env python3
import argparse
import os
from datetime import datetime

def newnote():
    file_name_to_open = os.path.join(os.getenv("HOME"),".notes", datetime.now().strftime('%Y-%m-%d') + ".md")
    lines_to_write = []
    if not os.path.exists(file_name_to_open):
        lines_to_write.append("# "+datetime.now().strftime('%Y-%m-%d') + "\n")
    f = open(file_name_to_open, 'a')
    lines_to_write.append("""
## {current_time}

""".format(current_time = datetime.now().strftime('%H:%M:%S')))
    f.writelines(lines_to_write)
    f.close()

    if not os.getenv('EDITOR') or os.getenv('EDITOR') == "vi":
        os.system('vi "+normal G$" +startinsert {file_name}'.format(file_name=file_name_to_open))
    elif os.getenv('EDITOR') == "gvim":
        os.system('gvim "+normal G$" +startinsert {file_name}'.format(file_name=file_name_to_open))
    elif os.getenv('EDITOR') == "vim":
        os.system('vim "+normal G$" +startinsert {file_name}'.format(file_name=file_name_to_open))
    elif os.getenv('EDITOR') == "code":
        os.system('code --goto {file_name}:100000'.format(file_name=file_name_to_open))
    else:
        os.system('{editor} {file_name}'.format(editor=os.getenv('EDITOR'), file_name=file_name_to_open))

if __name__=="__main__":
    parser = argparse.ArgumentParser()
    if not os.path.exists(os.path.join(os.getenv("HOME"),".notes")):
        os.mkdir(os.path.join(os.getenv("HOME"),".notes"))
    newnote()
