#!/usr/bin/env python3
#
# Copyright (c) 2014 the Sanzang Utils authors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import getopt
import io
import signal
import sys

USAGE = '''Usage: szu-ss [options] table_file

Table-based string substitution.

Options
  -h, --help       print this help message and exit
  -v, --verbose    include information useful for debugging
'''

def read_2c_table(fd):
    '''
    Read a two-column translation table file.
    '''
    tab = []
    for line in fd.read().replace(' ', '').split('\n'):
        rec = line.strip().split('|')
        if len(rec) == 2:
            tab.append(rec)
        elif line.strip() != '':
            raise RuntimeError('Table error: ' + line.strip())
    return tab

def subst(table, text):
    '''
    Make string substitutions using a two-column table.
    '''
    for t1, t2 in table:
        if t1 in text:
            text = text.replace(t1, t2)
            text = text.replace(t1.lower(), t2.lower())
            text = text.replace(t1.upper(), t2.upper())
    return text

def stdio_subst(table, buf_size=1000):
    '''
    Make string substitutions, from stdin to stdout.
    '''
    buffer = ''
    line_no = 1
    for line in sys.stdin:
        buffer += line
        if line_no % buf_size == 0:
            sys.stdout.write(subst(table, buffer))
            buffer = ''
        line_no += 1
    sys.stdout.write(subst(table, buffer))

def main():
    '''
    Run as a command-line program.
    '''
    try:
        sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf-8-sig',
                errors='strict', newline=None, line_buffering=True)
        sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8',
                errors='strict', newline=None, line_buffering=True)
        sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding='utf-8',
                errors='strict', newline=None, line_buffering=True)
    except io.UnsupportedOperation:
        pass

    if 'SIGPIPE' in dir(signal):
        signal.signal(signal.SIGPIPE, signal.SIG_DFL)

    try:
        verbose = False
        opts, args = getopt.getopt(sys.argv[1:], 'h', ['help'])
        for o, a in opts:
            if o in ('-h', '--help'):
                print(USAGE)
                return 0
            if o in ('-v', '--verbose'):
                verbose = True
        if len(args) != 1:
            sys.stderr.write(USAGE + '\n')
            return 1
        with open(args[0], encoding='utf-8-sig') as table_fd:
            stdio_subst(read_2c_table(table_fd))
    except EOFError:
        return 0
    except KeyboardInterrupt:
        print()
        return 1
    except Exception as err:
        if verbose:
            raise err
        else:
            sys.stderr.write('szu-ss: ' + str(err) + '\n')
            return 1
    return 0

if __name__ == '__main__':
    sys.exit(main())
