#!/usr/bin/env python3

import os
import sys
import random

from pprint import pprint

if len(sys.argv)>1:
    thedir = sys.argv[1]
else:
    thedir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/src'

count = 0
is_it = False
pathes = []
for name in os.listdir(thedir):
    if name[-4:] != '.txt':
        continue
    path = thedir + '/' + name
    pathes.append(path)
    if is_it:
        count += 1
    is_it = False
    with open(path) as f:
        for line in f:
            line = line.rstrip()
            if is_it and line=='':
                count += 1
                is_it = False
            elif line!='':
                is_it = True
if is_it:
    count += 1

num = random.randint(1, count)

count = 0
is_it = False
quote = ''

def next_quote():
    global count, quote, is_it
    count += 1
    if count >= num:
        print("{}        {}:{}".format(quote, os.path.basename(path)[:-4], count - start - 1))
        exit()
    is_it = False
    quote = ''

for path in pathes:
    start = count
    if is_it:
        next_quote()
    with open(path) as f:
        for line in f:
            line = line.rstrip()
            if is_it and line=='':
                next_quote()
            elif line!='':
                is_it = True
                quote += line + "\n"
if is_it:
    next_quote()
