#!/usr/bin/env python

"""
Finds clean files and asks to remove them
"""

import os
import shutil

CLEAN_FILES = [
    '~/.bzr.log',
    '~/.cache/babl/',
    '~/.cache/gegl-0.2/',
    '~/.cache/google-chrome/',
    '~/.cache/gstreamer-1.0/',
    '~/.cache/menu/',
    '~/.cache/thumbnails/',
    '~/.local/share/gegl-0.2/',
    '~/.local/share/recently-used.xbel',
    '~/.ncmpcpp/error.log',
    '~/.npm/',
    '~/.nv/',
    '~/.pki/',
    '~/.pylint.d/',
    '~/.recently-used',
    '~/.thumbnails/',
    '~/.w3m/',
    '~/.xsession-errors.old'
]


def answer(question, default="n"):
    """
    Asks the user for YES or NO, always case insensitive.
    """

    prompt = "%s (y/[n]) " % question
    ans = input(prompt).strip().lower()

    if not ans:
        ans = default

    if ans == "y":
        return True
    return False


def remove_clean():
    """
    Removes clean files based on answer
    """

    found = []

    print("\n")
    for jfile in CLEAN_FILES:
        extra = os.path.expanduser(jfile)
        if os.path.exists(extra):
            found.append(extra)
            print("    %s" % jfile)
    total = len(found)

    if total == 0:
        print("No clean files found :)\n")
        return

    if answer("\nRemove all?", default="n"):
        for jfile in found:
            if os.path.isfile(jfile):
                os.remove(jfile)
            else:
                shutil.rmtree(jfile)
        print("\nAll clean cleaned")

    else:
        print("\nNo files removed")


if __name__ == '__main__':
    remove_clean()
