#!/usr/bin/env python3

"""Output a logging timestamp."""

import os
import sys


def ensure_directory(directory):
    """Create a directory.  If it already exists, don't stress about it."""
    try:
        os.mkdir(directory)
    except (OSError, 17):
        pass


def hex_nybble(nybble):
    """Return a single hex digit."""
    return '0123456789abcdef'[nybble]


def hex_byte(byte):
    """Return a single hex byte."""
    return '%s%s' % (hex_nybble(byte // 16), hex_nybble(byte & 0xf))


def random_string():
    """Return a random number in hexadecimal."""
    with open('/dev/urandom', 'rb') as file_:
        bytes_ = file_.read(16)
    result = []
    for byte in bytes_:
        result.append(hex_byte(byte))
    return ''.join(result)

# note the relative paths!
# hcm is going to chdir to ~/.hcm/logs - if something else cd's away
# from there, these are going to go to the wrong place
# ensure_directory(time.strftime('%Y'))
# ensure_directory(time.strftime('%Y/%m'))
# ensure_directory(time.strftime('%Y/%m/%d'))


def main():
    """Output a logging timestamp."""
    if sys.argv[1:]:
        path = '%s' % sys.argv[1].replace('!', '-')
    else:
        path = ''
    print('%s-R%s' % (path, random_string()))


main()