#!/usr/bin/env bash

# The MIT License (MIT)
#
# Copyright (c) 2014 Gil Gonçalves
#
# 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.


function translate_color {
    COLOR_CODE=$1

    case $COLOR_CODE in
        'black')   COLOR_CODE=232;;
        'red')     COLOR_CODE=124;;
        'green')   COLOR_CODE=128;;
        'yellow')  COLOR_CODE=190;;
        'blue')    COLOR_CODE=19;;
        'magenta') COLOR_CODE=52;;
        'cyan')    COLOR_CODE=57;;
        'white')   COLOR_CODE=255;;
    esac
}


while getopts “:B:C:biut” OPTION
do
    case $OPTION in
        B)
            translate_color $OPTARG
            color=$(tput setab $COLOR_CODE)
            STRING_START+="${color}"
            ;;
        C)
            translate_color $OPTARG
            foreground=$(tput setaf ${COLOR_CODE})
            STRING_START+="${foreground}"
            ;;
        b)
            bold=$(tput bold)
            STRING_START+="${bold}"
            ;;
        i)
            italics=$(tput sitm)
            STRING_START+="${italics}"
            ;;
        u)
            underline=$(tput smul)
            STRING_START+="${underline}"
            ;;
        t)
            blink=$(tput blink)
            STRING_START+="${blink}"
            ;;
    esac
done


RESET_TEXT_ATTRIBUTES=$(tput sgr0)
while read LINE; do
    echo -e ${STRING_START}${LINE}${RESET_TEXT_ATTRIBUTES};
done