#!/usr/bin/python
import sys, os

if sys.argv[1] in ["--help", "-h"]:
    print("\nWelcome to the cli helper for foreach loops.")
    print("\nUsage:\neachf [path] [filter] [command1] [command2] [command3] [test]")
    print("\nDescription:\nIn between the command-arguments, the filename will be inserted.\n")
    exit()

files = os.listdir(sys.argv[1])
filtered_files = []
commands = []


for file in files:
    if sys.argv[2] in file:
        filtered_files.append(file)

for f in filtered_files:
    c = sys.argv[3] + f
    if len(sys.argv) >= 5:
        c = c + sys.argv[4]
    if len(sys.argv) >= 6:
        c = c + f + sys.argv[5]
    commands.append(c)
    

if sys.argv[-1] in ["test", "-t", "--test"]:
    for command in commands:
        print(command)
else:
    for command in commands:
        os.system(command)