#!/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

Make table-based string substitutions

Options
  -h, --help       print this help message and exit
  -v, --verbose
'''

def read_2c_table(file_path):
    '''
    Read a two-column translation table file.
    '''
    table = []
    with open(file_path, 'r', encoding='utf-8-sig') as fin:
        for line in fin:
            if '|' in line:
                from_str = line.split('|')[0].strip()
                to_str = line.split('|')[1].strip()
                table.append((from_str, to_str))
    return table

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

        table = read_2c_table(sys.argv[1])
        while True:
            line = input()
            for defn in table:
                line = line.replace(defn[0], defn[1])
                line = line.replace(defn[0].lower(), defn[1].lower())
                line = line.replace(defn[0].upper(), defn[1].upper())
            print(line)
    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__':
    exit(main())
