unameval="$(uname -s)"
case "${unameval}" in
    Linux*)     os=Linux;;
    Darwin*)    os=Mac;;
    CYGWIN*)    os=Cygwin;;
    MINGW*)     os=MinGw;;
    *)          os="UNKNOWN:${unameval}"
esac
echo "Setup for ${os}"



# Config in shell
config=~/icecap_config.sh
if [ -f "$config" ] ; then
    source ${config}
fi

if [ -z "$IBMLONGHOST" ] ; then
    echo "Enter full qualified system name of your IBM i:"
    read IBMLONGHOST
    echo "export IBMLONGHOST=${IBMLONGHOST}"  >> ${config}
    IBMIHOST="${IBMLONGHOST%%.*}"
    echo "export IBMIHOST=${IBMIHOST}"  >> ${config}
fi

if [ -z "$IBMIUSER" ]; then
    echo "Enter your IBM i user profile on ${IBMIHOST}:"
    read IBMIUSER
    echo "export IBMIUSER=${IBMIUSER}" >> ${config}
fi

keyfile=~/.ssh/${IBMIHOST}
configexists=$(cat ~/.ssh/config | grep ${IBMIHOST}$)

if [  -z "${configexists}" ]; then
    mkdir -pv ~/.ssh/controlmasters/
    echo "Setting up host alias"
    echo "host ${IBMIHOST}"  >> ~/.ssh/config
    echo "    hostname ${IBMLONGHOST}" >> ~/.ssh/config
    echo "    user ${IBMIUSER}" >> ~/.ssh/config
    echo "    IdentityFile ${keyfile}" >> ~/.ssh/config
    #echo "    ControlPath ~/.ssh/controlmasters/%r@%h:%p" >> ~/.ssh/config
    #echo "    ControlMaster auto" >> ~/.ssh/config
    #echo "    ControlPersist yes" >> ~/.ssh/config
    echo " " >> ~/.ssh/config
fi

if [ ! -f "${keyfile}" ] ; then
    echo "Running ssh key generator for ${IBMIHOST} : Press enter to all to get defaults"
    ssh-keygen -f ${keyfile} -t rsa
    chmod 600 ${keyfile}
    ssh-copy-id -i ${keyfile} ${IBMIHOST}
fi

echo "done!!"