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

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

HOST=""
ARGS=()

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

# Adds soft buttons to xt5250 Not supported by xt5250
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 xt5250 Not supported by xt5250
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 xt5250
# 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. Not supported.
            ;;
        *background*)
            # Set BG color. Not supported.
            ;;
    esac
}

# Loop through command line arguments and map the existing args to the
# equivalent xt5250 if possible.
while [ $# != 0 ]
do
    case "${1}" in
        -tnhost)
            # Host
            HOST="${2}"
            shift 2
            ;;
        -tnport)
            # Port
            HOST="${HOST}:${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. Not supported.
            shift
            ;;
        +mb)
            # Without menu bar. Not supported.
            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 xt5250
exec "${APPLICATION}" "${ARGS[@]}" "${HOST}"

