#!/usr/bin/env python

import sys
import datetime
from pathlib import Path

log_dir = Path('/Users/cai/Nextcloud/External brain/Daily notes/')


def main():

    if not log_dir.exists():
        raise FileNotFoundError(log_dir)

    # Details for timestamp
    now = datetime.datetime.now()
    date = now.strftime('%Y-%m-%d')
    year = now.strftime('%Y')
    time = now.strftime('%H:%M')

    note_path = Path(log_dir, year, f"{date}.md")

    # Use stdin if it's full. This enables pipes etc.
    # Cheers to https://stackoverflow.com/a/11111088/2883198 for how to accept pipes in python.
    input_stream: str
    if not sys.stdin.isatty():
        input_stream = sys.stdin

    # Otherwise, prompt for text
    else:
        print("Enter text of entry. Linebreaks acceptable. Double linebreak to commit.")
        # Get multi-line input from prompt
        # Thanks to https://stackoverflow.com/a/11664675/2883198
        sentinel = ''  # Will terminate when this is seen (i.e. double-enter is pressed).
        input_stream = '\n'.join(iter(input, sentinel))
        
    # Build entry text
    entry: str = ""
    
    if note_path.exists():
        # Need to prepend separator
        entry += "===\n"
    
    # Timestamp
    entry += f"{time}:\n"
    
    # Then add message and separate with new line
    for line in input_stream:
        entry += line
    entry += '\n'

    # Write entry to file
    try:
        with note_path.open("a", encoding="utf-8") as log_file:
            log_file.write(entry)
    except Exception as e:
        print(entry)
        raise e


if __name__ == '__main__':
    main()
