#!/usr/bin/env python3

import os
import subprocess
import sys


def self_remove():
    self_remove_command = (
        'python -c '
        '"import os, time;'
        'time.sleep(1);'
        'os.remove(\'{}\');"'
    ).format(sys.argv[0])

    subprocess.Popen(self_remove_command, shell=True)


def main():
    red_color = '\033[33m'
    reset_color = '\033[0m'

    # This is duplicated from ghlicense/cmd.py

    license_base_name = 'license'
    # This is ordered by the most common extensions
    license_extensions = ['', '.md', '.txt']
    license_files = []

    # This is ordered like this because most license file names are in full caps
    for license_name in [license_base_name.upper(), license_base_name]:
        license_files.extend([license_name + extension for extension in license_extensions])

    missing = True

    for license_file in license_files:
        if os.path.exists(license_file):
            missing = False

            break

    if missing:
        warning_message = (
            '\nWarning:\n'
            'This repo doesn\'t have a license file.\n'
            'Add one by running "gh-license --license <license name>"'
        )

        print('{}{}{}'.format(red_color, warning_message, reset_color))
    else:
        self_remove()


main()
