#!/usr/bin/python3
import getpass
import os, subprocess
import random

class Colors:
    RED = "\033[0;31m"
    GREEN = "\033[0;32m"
    YELLOW = "\033[1;33m"
    ORANGE = "\033[38;5;208m"
    END = "\033[0m"



def ask(question: str, default=None, required=False, confidential=False):
    """Ask a question to the user and check if the result is good"""
    while True:
        if confidential:
            n = getpass.getpass(question, echo_char="*") or default
            if n == getpass.getpass("confirm : ", echo_char="*") or default:
                if (not required) or (required and n != None):
                    return n
                else:
                    print(f"{Colors.RED}this field is required !{Colors.END}")
            else:
                print(f"{Colors.END}the 2 results mismatch !{Colors.END}")
        else:
            n = input(question) or default
            if not "n" in input(f"{n} is correct ? "):
                if (not required) or (required and n != None):
                    return n
                else:
                    print(f"{Colors.RED}this field is required !{Colors.END}")

tips = [
    f"you can use environnment variables to set an Email and a Discord to the README file ! "
]

print(Colors.YELLOW+random.choice(tips)+Colors.END)

project_name = ask("enter your project filename > ", required=True)
libs = ask("enter your libs flags press (let the field blank if None) > ", default="")

readme = """
# ###PROJECTNAME###

## Description

## Compilation / Execution

pour compiler le projet il suffit d'éxecuter : ```make```

pour éxecuter le projet il suffit d'éxécuter : ```bin/###PROJECTNAME###```

## FAQ



"""

template = f"""# tool macros
CC ?= gcc
#CXX ?= 
CFLAGS := -ansi -pedantic {libs}
#CXXFLAGS := # FILL: compile flags
DBGFLAGS := -g
COBJFLAGS := $(CFLAGS) -c

# path macros
BIN_PATH := bin
OBJ_PATH := obj
SRC_PATH := src
DBG_PATH := debug

# compile macros
TARGET_NAME := {project_name}
ifeq ($(OS),Windows_NT)
	TARGET_NAME := $(addsuffix .exe,$(TARGET_NAME))
endif
TARGET := $(BIN_PATH)/$(TARGET_NAME)
TARGET_DEBUG := $(DBG_PATH)/$(TARGET_NAME)

# src files & obj files
SRC := $(foreach x, $(SRC_PATH), $(wildcard $(addprefix $(x)/*,.c*)))
OBJ := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
OBJ_DEBUG := $(addprefix $(DBG_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))

# clean files list
DISTCLEAN_LIST := $(OBJ) \
                  $(OBJ_DEBUG)
CLEAN_LIST := $(TARGET) \
			  $(TARGET_DEBUG) \
			  $(DISTCLEAN_LIST)

# default rule
default: makedir all

# non-phony targets
$(TARGET): $(OBJ)
	$(CC) -o $@ $(OBJ) $(CFLAGS)

$(OBJ_PATH)/%.o: $(SRC_PATH)/%.c*
	$(CC) $(COBJFLAGS) -o $@ $<

$(DBG_PATH)/%.o: $(SRC_PATH)/%.c*
	$(CC) $(COBJFLAGS) $(DBGFLAGS) -o $@ $<

$(TARGET_DEBUG): $(OBJ_DEBUG)
	$(CC) $(CFLAGS) $(DBGFLAGS) $(OBJ_DEBUG) -o $@

# phony rules
.PHONY: makedir
makedir:
	@mkdir -p $(BIN_PATH) $(OBJ_PATH) $(DBG_PATH) $(SRC_PATH)

.PHONY: all
all: $(TARGET)

.PHONY: debug
debug: $(TARGET_DEBUG)

.PHONY: clean
clean:
	@echo CLEAN $(CLEAN_LIST)
	@rm -f $(CLEAN_LIST)

.PHONY: distclean
distclean:
	@echo CLEAN $(DISTCLEAN_LIST)
	@rm -f $(DISTCLEAN_LIST)"""

with open("Makefile", "w", encoding="utf-8", errors="ignore") as f:
    f.write(template)
    print(f"{Colors.GREEN}Makefile build successfully !{Colors.END}")
    f.close()


readme=readme.replace("###PROJECTNAME###",project_name)

mail=os.getenv("EMAIL") or None
mail=os.getenv("DISCORD") or None
if not mail == None and not discord == None:
    readme+=f"\n\n## Bugs / Question\n-email: {mail}\n- Discord: {discord}"

with open("README.md", "w", encoding="utf-8", errors="ignore") as f:
    f.write(readme)
    print(f"{Colors.GREEN}README written successfully !{Colors.END}")
    f.close()

os.system("make makedir")
open("src/main.c", "w").write(
    """#include <stdio.h>\n#include <stdlib.h>\n\nint main(void) {\n\n\treturn EXIT_SUCCESS;\n}"""
)
