#!/bin/sh

logon_user=$LOGNAME
asm_motd_s=/opt/Dell/ASM/conf/asm_motd.sh
ini_runf=$HOME/.dell_app/single_run.lock
asm_motd=$HOME/.dell_app/asm_motd.sh

msg_note=" WARNING! Appliance admin account is using the default password. Please update using the asm_init_shell described below."

# When logging in via ssh, SSH_TTY variable will be set. We will check for this environment variable to determine if it is local login vs SSH login. 

if [ "x${SSH_TTY}" != 'x' -o "x${SSH_CLIENT}" != 'x' -o "x${SSH_CONNECTION}" != 'x' -o -e ${ini_runf} ]; then
    export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

    if [ "x$1" == 'x-c' ]; then 
	## SSH and SCP can pass commands to be executed from SSH SHELL. 
	## This section will handle those commands.
	shift
	$@
    else
        ## First we will check if the default password is still in use or not
        /usr/sbin/asm_chkpass.exp ${logon_user} ${logon_user} 2>&1 > $HOME/.dell_app/.asm_chkpass.log
        retc=$?

        # Test to see file is none 0 also
	if [ "$retc" = "1" -a -s $asm_motd ]; then
	   # Replace Notes with warning message into the login greeting
	   sed -i -e "/----------/ s/----------/${msg_note}/" $asm_motd
           # -s will test for none zero bytes
	elif [ "$retc" = "2" -o ! -s $asm_motd ]; then
           echo "$(cat ${asm_motd_s})" > $asm_motd
        else 
	   echo "asm_chkpass.exp failed"  >> $HOME/.dell_app/.asm_chkpass.log
	fi

        sleep 1
	exec /bin/bash --login
    fi
else
    if [ -f /etc/sysconfig/i18n ]; then
	. /etc/sysconfig/i18n
	unset SYSFONTACM SYSFONT consoletype
    fi
        
    if [ x${LANG} == 'x' ]; then
	export LANG='en_US.UTF-8'
    else
	export LANG
    fi
    
    sudo /usr/share/dell_app_init/dell_app_init.py 

    #Give the users a chance to read the error messages if any.
    sleep 1
    if [ -f /opt/Dell/eula_state ]; then
       touch ${ini_runf} && chmod 400 ${ini_runf}
       echo "$(cat ${asm_motd_s})" > $asm_motd
    fi

fi
