#!/usr/bin/env python3

# Eliminate BIRCH from a user's account.
# Lines added to user .rc files all contain #_BIRCH.
# Remove these lines.

# Synopsis nobirch.py [-Q]
#
#      -Q - quiet option. Do not prompt to verify


import os
import platform
import shutil
import sys


# Automatically converted to Python3 using 2to3. Compliant with Python 2 and 3.

# Remove lines containing searchExp from each file in filelist
def birchremove(filelist, searchExp):
    for file in filelist:
        if os.path.exists(file):
            shutil.move( file, file + "~" )
            destination = open( file, "w" )
            source= open( file + "~", "r" )
            for line in source:
                if line.find(searchExp) == -1 :
                    destination.write( line )
            source.close()
            destination.close()
            os.chmod(file, 0o700)

def nobirch(BIRCH):

    print ("getting home directory")
    HOME = os.path.expanduser('~')

    # Delete desktop launchers for BIRCH
    print('Removing launcher links')

    # We have to add the path to rmlauncher.py so that we
    # can import it.
    pathtoadd = os.path.join(BIRCH, "script")
    sys.path.insert(0, pathtoadd)
    import rmlauncher

    try :
        rmlauncher.rmlauncher()
    except:
        pass

    # delete lines from .login file, used by C-shell family
    print ("Writing .login")
    FILELIST=[os.path.join(HOME, '.login')]
    birchremove(FILELIST, "#_BIRCH")

    # C-shell family
    print ("Writing .cshrc & .tcshrc")
    FILELIST=[os.path.join(HOME, '.cshrc'), os.path.join(HOME, '.tcshrc')]
    birchremove(FILELIST, "#_BIRCH")

    # Bourne-shell family
    print ("Writing Bourne Shell .profile, .bash_profile, .zshrc, etc.")
    FILELIST=[os.path.join(HOME, '.profile'),
        os.path.join(HOME, '.bash_profile'),
        os.path.join(HOME, '.bash_login'),
        os.path.join(HOME, '.bashrc'),
        os.path.join(HOME, '.zshrc')]
    birchremove(FILELIST, "#_BIRCH")

    print('Done - nobirch.py!')
    print('Logout and login again so that the changes can take effect.')
    print('If you wish to restore BIRCH access, type')
    print('$BIRCH/admin/newuser')

if __name__=="__main__":

    choice = 'n'
    if len(sys.argv) > 1 :
        if sys.argv[1] == "-Q" :
            choice = "Y"
        else:
            print ('Useage: nobirch.py [-Q]')
            sys.exit(1)
    else:
        print('>>> You are about remove access to the BIRCH system for this account!')

        # The Python 2.x and Python 3.x get terminal input a bit differently, so we
        # have to check which version of Python we're using.
        pversion = platform.python_version()[0]
        if pversion == "2" :
            choice = raw_input(">>> Type y to continue, n to exit\n")
        else :
            choice = input(">>> Type y to continue, n to exit\n")

    if choice in ['Y','y'] :
        BIRCH = os.environ.get("BIRCH")
        if len(BIRCH) > 0 :
            nobirch(BIRCH)
        else:
            print ('>>> $BIRCH environment variable not set. Aborting nobirch.py')
    else:
        print('>>> exiting nobirch. No files changed.')
