#!/usr/bin/env python

import re
import os
import sys


def check_platform(platform='posix', warnmsg=None, endscript=False):
    """ Checks to see that os is on given platform and gives user option
    to quit if not.  If endscript set to True, then will attempt to exit
    script if user is not root and user does not choose to continue.

    We're all adults. This function treats your user like one too.
    Returns True if everything's okay, False if user elects to continue
    and raises OSError or quits otherwise.

    INPUT:
        platform - string, platform to be checked. default is 'posix'.
            NOTE: It is recommended that you explicitly set platform.
        warnmsg - string, warning message to be displayed to user if not
            root default: warning that script is meant to be run on given
            platform
        endscript - bool, whether to call sys.exit if user doesn't continue
            -> True: calls sys.exit
            -> False: raises OSError
            default: False
    RETURNS:
        True - if user is root
        False - otherwise

    RAISES:
        OSError
    """
    if not os.name == platform:
        if warnmsg is None:
            warnmsg = """
            This script is meant to run on posix platforms.
            Are you sure you want to run this script?
            'y' or enter to continue, otherwise script will quit.
            [ENTER to continue]>
            """
        cont = raw_input(warnmsg)
        if cont is not None or cont != 'y':
            if endscript or __name__ == '__main__':
                sys.exit('\nQuitting...\n')
            else:
                raise OSError("Non-posix OS, user elected to stop.")
        else:
            print "OK. Continuing."
            return False
    else:
        return True


def rm_symlink(path):
    """removes file so long as it is a symlink.
    path must be an absolute path.
    Returns True if removed, False if not."""
    if os.path.islink(path):
        print "Removing path %r" % path
        os.remove(path)
        return True
    else:
        return False


def rm_dotsymlinks(target=None, filter_fn=None, verbose=True):
    """creates symlinks for all the files in source in directory target.
    INPUT
        target - the directory to remove_symlinks from
    OUTPUT
        deletes symlinks in path (if run as main, will prompt for confirmation through main() fn)
    RAISES
        ValueError (if no target specified)"""
    if target is None:
        target = os.getcwd()
    else:
        target = os.path.realpath(target)
    # get a list of symlinks in directory and get their full paths
    symlist = map(lambda x: os.path.join(target,x),
            filter(lambda x: x[0] == '.' and os.path.islink(x), os.listdir(target)))
    sourcelist = map(os.path.realpath, symlist)
    msg = "target  %r" % (target)
    print "The following links will be removed(%s)" % msg
    for sym,source in zip(symlist, sourcelist): 
        print "link: %r -> %r" % (sym,source)
    print msg
    if raw_input("\nNOTE: no regular files will be deleted.\n[continue? y/n]>") in ['y','yes']:
        for sym in symlist:
            try:
                if not rm_symlink(sym):
                    print "Couldn't remove file %r. Continuing" % sym
            except OSError as e:
                print "OSError:\n%r\nDo you want to continue?" % e.message or e
                if not raw_input("[Type any key to continue, n or q to quit]>") in ['q','n','quit','no']:
                    continue
                else:
                    print "Raising error message for reference"
                    raise e
        return
    else:
        return "Ok, not continuing.\nNo files deleted.\n"

def main():
    try:
        if len(sys.argv) == 1:
            target = os.getcwd()
        elif len(sys.argv) == 2:
            source,target = sys.argv[1],sys.argv[2]
        else:
            raise ValueError("Too many arguments specified at command line")
        if not os.path.isdir(os.path.realpath(target)):
            raise ValueError("Specifed  target path is not a directory.\n Given %r" % target)
        # run program and get output
        output = rm_dotsymlinks(source=source,target=target, verbose = False)
    except Exception as e:
        # grab the error message if we can
        msg = e.message if hasattr(e,'message') else 'No message given'
        msg = e if isinstance(e, str) else msg
        msg = """
This script must be called as:
`%s target`
(specifying target is optional)

Error message: 
%r"""  % (sys.argv[0], msg)
        sys.exit(msg)
    sys.exit(output)


if __name__ == '__main__':
    main()
