#!/usr/bin/python

import os
import sys

# Go to parent folder

def usage():
    print('')
    print('USAGE: '+__file__+' <step_1_or2> <site_packages_folder_path> ')
    print('')
    print('')


def show_help_and_exit():
    usage()
    exit()

def on_result_and_exit(error_code):
    if error_code==0:
        print('')
        print('')
        exit(0)
    
    print('ERRROR')
    print('')
    print('')
    exit(error_code)

#######


if len(sys.argv)<=2:
    show_help_and_exit()

########################################
#
# BELOW ARE STANDARD ACTIONS
#
########################################

patch_step = sys.argv[1]
sitepackages_path = sys.argv[2]

print('PATCHING PYTHON CODE')
print('--------------------')
print('site packages path:    %s'%(sitepackages_path))
print('')

if patch_step=='1':

    patchable_paths = [
        sitepackages_path+'/tgext/pluggable',
        sitepackages_path+'/resetpassword',
        sitepackages_path+'/babel'
    ]

    for patchable_path in patchable_paths:
        print('2to3 conversion for %s...' % (patchable_path))
        os.system('2to3 -w %s'%(patchable_path))
        print('-> done')

    babel_source_code_patch_content = """--- tg2env/lib/python3.2/site-packages/babel/messages/pofile.py 2014-11-07 15:35:14.039913184 +0100
    +++ tg2env/lib/python3.2/site-packages/babel/messages/pofile.py 2014-10-30 17:37:36.295091573 +0100
    @@ -384,8 +384,13 @@
     
         def _write(text):
             if isinstance(text, text_type):
    -            text = text.encode(catalog.charset, 'backslashreplace')
    -        fileobj.write(text)
    +            pass
    +            # text = text.encode(catalog.charset, 'backslashreplace')
    +        try:
    +            fileobj.write(text.encode('UTF-8'))
    +        except Exception as e:
    +            fileobj.write(text)
    +        
     
         def _write_comment(comment, prefix=''):
             # xgettext always wraps comments even if --no-wrap is passed;
    """

    babel_patchable_file_path = sitepackages_path+'/babel/messages/pofile.py'
    print('Patching code in file %s...'%(babel_patchable_file_path))
    os.system('echo "%s"|patch -p1 %s'%(babel_source_code_patch_content, babel_patchable_file_path))
    print('-> done')

elif patch_step=='2':
    resetpassword_patchable_file_path = sitepackages_path+'/resetpassword/lib/__init__.py'
    print('Patching code in file %s...'%(resetpassword_patchable_file_path))
    os.system("sed -i 's/body\.encode/body/g' %s" % (resetpassword_patchable_file_path))
    print('-> done')

else:
    show_help_and_exit()

print('')
print('')


