#!/bin/bash
#  $Id:
#  Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
#
# Wrapper script to launch system installed 3270 application.

# The default system application to use.
APPLICATION="/usr/bin/x3270"
# Different location for Solaris
if [ "$(uname -s)" = "SunOS" ] ; then
    APPLICATION="/opt/csw/bin/x3270"
fi

HOST=""
# This creates an 80x24 terminal. Note if foreground or background color are
# specified this will be overrideen with model 3278.
ARGS=(-model 2)

# Adds the equivalent x3270 close action (if any) to the x3270 arg list.
# Argmuments
# $1 - The close action 0 - 3.
closeAction() {
    case "${1}" in
        0)
            # Prompt user for action. Not supported.
            ;;
        1)
            # Reconnect.
            ARGS+=(-xrm "x3270.reconnect: true")
            ;;
        2)
            # Close connection. Not supported.
            ;;
        3)
            # Exit emulator. Not supported.
            ;;
    esac
}

# Adds soft buttons to x3270. Not supported by x3270.
softButtons() {
    case "${1}" in
        0)
            # No buttons. Not supported.
            ;;
        1)
            # 2 Rown. Not supported.
            ;;
        2)
            # 4 Rows. Not supported.
            ;;
        3)
            # 6 Rows. Not supported.
            ;;
        4)
            # 8 Rows. Not supported.
            ;;
    esac
}

# Add keyboard setting to x3270. Not supported by x3270.
keyboard() {
    case "${1}" in
        pc)
           # Not supported.
           ;;
        sun4)
           # Not supported.
           ;;
        sun5)
           # Not supported.
           ;;
        hp)
           # Not supported.
           ;;
    esac
}

# Add the foreground and background color arguments to x3270.
# Whether it is fg/bg and color are parsed from the argument.
# Arguments:
# $1 - The foreground or background color to use.
color() {
    case "${1}" in
        *foreground*)
            # Set FG color.
            ARGS+=(-xrm "x3270.normalColor: ${1#* }" -xrm "x3270.model: 3278")
            ;;
        *background*)
            # Set BG color.
            ARGS+=(-xrm "x3270.colorBackground: ${1#* }" -xrm "x3270.model: 3278")
            ;;
    esac
}

# Loop through command line arguments and map the existing args to the
# equivalent x3270 if possible.
while [ $# != 0 ]
do
    case "${1}" in
        -tnhost)
            # Host
            HOST="${2}"
            shift 2
            ;;
        -tnport)
            # Port
            ARGS+=(-port ${2})
            shift 2
            ;;
        -tnclose)
            # Action when session is closed.
            closeAction "${2}"
            shift 2
            ;;
        -ma)
            # Maximimised. Not supported.
            shift
            ;;
        +ma)
            # Un-maximised. Not supported.
            shift
            ;;
        -mb)
            # With menu bar.
            ARGS+=(-xrm "x3270.menuBar: true")
            shift
            ;;
        +mb)
            # Without menu bar.
            ARGS+=(-xrm "x3270.menuBar: false")
            shift
            ;;
        -si)
            # Settings item. Not supported.
            shift
            ;;
        +si)
            # No settings item. Not supported.
            shift
            ;;
        -bl)
            # Displayed soft buttons.
            softButtons "${2}"
            shift 2
            ;;
        -kt)
            # Keyboard type
            keyboard "${2}"
            shift 2
            ;;
        -sf)
            # Settings file. Not supported.
            shift 2
            ;;
        -xrm)
            # Foreground and background color.
            color "${2}"
            shift 2
            ;;
        *)
            # Unknown arg
            shift
            ;;
    esac
done

# Start x3270
exec "${APPLICATION}" "${ARGS[@]}" "${HOST}"

