#! /usr/bin/env python3.10

import os, configparser

#Set Skript Vars
user_gitconfig = os.environ["HOME"] + os.sep + ".gitconfig"
config_ini = configparser.ConfigParser()


list_my_git_projects = [
                        "https://gitea.schlaubistechtalk.de",
                        "https://gitlab.com",
                        "https:ubuntu:5000"
                    ]
my_username = os.environ["USER"]
if not os.path.exists(user_gitconfig):
    os.popen("touch " + user_gitconfig)

config_ini.read(user_gitconfig)
if not config_ini.has_section("user"):
    config_ini.add_section("user")
    config_ini.set("user", "email", "mr.blue82@gmail.com")
    config_ini.set("user", "name", my_username)

if not config_ini.has_section("core"):
    config_ini.add_section("core")
    config_ini.set("core", "editor", '"vim"')
    config_ini.set("core", "pager", '"less --raw - control - chars"')

    config_ini.set("core", "autocrlf","false" )
if not config_ini.has_section("init"):
    config_ini.add_section("init")
    config_ini.set("init", "defaultBranch", "main")

if not config_ini.has_section("credential"):
    config_ini.add_section("credential")
    config_ini.set("credential", "helper", "store")
    config_ini.set("credential", "helper", "cache –timeout=9640")

if not config_ini.has_section("alias"):
    config_ini.add_section("alias")
    config_ini.set("alias", "lo", "log --oneline --stat")
    config_ini.set("alias", "last", "log -1 HEAD")    
    config_ini.set("alias", "br", "branch")
    config_ini.set("alias", "ci", "commit")  
    config_ini.set("alias", "co", "checkout")
    config_ini.set("alias", "st", "status")
    config_ini.set("alias", "p", "push")
    config_ini.set("alias", "ldiff", "diff HEAD")

for my_git_site in list_my_git_projects:
    if not config_ini.has_section("credential " + '"' + my_git_site + '"'):
        config_ini.add_section("credential " + '"' + my_git_site + '"')
        config_ini.set("credential " + '"' + my_git_site + '"', "username", my_username)

    if not config_ini.has_section("URL " + '"' + my_git_site + '"'):
        config_ini.add_section("URL " + '"' + my_git_site + '"')
        config_ini.set("URL " + '"' + my_git_site + '"',  "insteadOf", 'git@' + my_git_site + ":")

#writing_all_configs
with open(user_gitconfig, "w") as configfile:
    config_ini.write(configfile)


