import random

# sets lists
puns = []
repeat = []

# reads the holytext and appends it to "puns"
with open("/var/www/html/holy_order/holytext.txt", "r", encoding="UTF-8") as f:
    for i in f:
        if i == "\n":
            continue
        else:
            puns.append(i)

# checks file ".holy_counter.txt" and appends content to "repeat"
with open("/var/www/html/holy_order/.holy_counter.txt", "r") as f:
    for i in f:
        if i == "\n":
            continue
        else:
            repeat.append(int(i.strip()))

# selects a random number
random_num = random.randint(0, len(puns)-1)

# checks if the index is already in the list od "repeat" and selects a new one
while random_num in repeat:
    random_num = random.randint(0, len(puns)-1)

# prints the pun    
print(puns[random_num])

# set how many numbers you want in ".holy_counter"(not over the number of puns)
length_counter = 10

# checks if counter is higher than the puns themselves
if length_counter >= len(puns):
    print("Counter length too highs")

# in case of ".holy_counter" having to many numbers it deletes them
while len(repeat) > length_counter:
    del repeat[0]

if len(repeat) == length_counter:
    # overwrites the file ".holy_counter" and freshly appends "repeat"
    with open("/var/www/html/holy_order/.holy_counter.txt", "w") as f:
        # gets rid of the first input
        del repeat[0]
            
        for i in repeat:
            f.write(str(i)+"\n")
        f.write(str(random_num)+"\n")

else:
    # creates/appends a file that stores the values of repeat
    with open("/var/www/html/holy_order/.holy_counter.txt", "a") as f:
        f.write(str(random_num)+"\n")
