import glob
import os
import sys
import shutil

# Returns a list of strings of the typed-repos generated by the script stored in "typed-repos"
def get_list_of_typed():
    content = None
    with open("../data/typed-repos-org") as f:
        content = f.readlines()
    content = [x.strip() for x in content]
    return content


def copy_repo(src, dst):
    shutil.copytree(src, dst)

def verify_args():
    args = sys.argv
    if len(args) == 3:
        if os.path.exists(args[1]) and os.path.exists(args[2]):
            return True
    return False

def _add_slash(s):
    if s[len(s)-1] != "/":
        return s + "/"
    return s

def main():
    if not verify_args():
        print("Usage: python3 ./copy-typed <all-repos> <new-folder>")
        sys.exit()

    # Retreives all of the metrics
    repo_names = get_list_of_typed()
    folder = sys.argv[1]
    new_folder = sys.argv[2]

    # Change to absolute paths to avoid weird relative paths
    folder = os.path.abspath(folder)
    folder = _add_slash(folder)

    new_folder = os.path.abspath(new_folder)
    new_folder = _add_slash(new_folder)
    
    for subfolder in repo_names:
        copy_repo(folder + subfolder, new_folder + subfolder)

if __name__ == "__main__":
    main()
