#!/system/bin/sh

__TRUE=0
__FALSE=1


SSHD_CONFIG_FILE="/system/etc/ssh/sshd_config"

PARAMETER_FILE="/data/local/tmp/home/.ssh/sshd_parameter"

LOGFILE="/data/local/tmp/restart_sshd.log"

SSHD_EMPTY_DIR="/data/local/tmp/var/empty"

if [ 0 = 1 -o -r /data/local/tmp/debug ] ; then
  echo  "Writing all messages from $0 to the log file ${LOGFILE} ..." 
  DEBUG_MODE=0
  exec >${LOGFILE} 2>&1
  set -x
else
  DEBUG_MODE=1
fi


echo "Restarting the sshd at $( date ) ..."

if [ ! -d "${SSHD_EMPTY_DIR}" ] ; then
  echo "ERROR: The directory \"${SSHD_EMPTY_DIR}\" does not exist"
  exit 2
fi

CUR_USER="$( id -un )"
SSHD_USER="$( stat -c %U  "${SSHD_EMPTY_DIR}" )"

if [ ${CUR_USER}x != "${SSHD_USER}"x ] ; then
  echo "ERROR: The sshd must be started by the user \"${SSHD_USER}\"; the current user is \"${CUR_USER}\" "
  exit 3
fi

if [ ! -r "${SSHD_CONFIG_FILE}" ] ; then
  echo "ERROR: The sshd config file \"${SSHD_CONFIG_FILE}\" does not exist"
  exit 5
fi

SSHD_PID_FILE="$(  grep ^PidFile "${SSHD_CONFIG_FILE}" | awk '{ print $2}' )"

if [ "${SSHD_PID_FILE}"x = ""x ] ; then
  echo "ERROR: There is no PID file for the sshd configured in the sshd config file \"${SSHD_CONFIG_FILE}\"  "
  exit 10
fi


if [ ! -r "${SSHD_PID_FILE}" ] ; then
  echo "Looks like the sshd is currently not running - the file \"${SSHD_PID_FILE}\" does not exist"
else
  CUR_SSHD_PID=$( cat ${SSHD_PID_FILE} )
  if [ "${CUR_SSHD_PID}"x = ""x ] ; then
    echo "Looks like the sshd is currently not running - the file \"${SSHD_PID_FILE}\" is empty"
  else
    echo "The PID of the started sshd is ${CUR_SSHD_PID}"
    CUR_SSHD_PROCESS="$( ps -f -p ${CUR_SSHD_PID} )"
    if [ $? -ne 0 ] ; then
      echo "No process with the PID ${CUR_SSHD_PID} found"
    else
      echo "${CUR_SSHD_PROCESS}"
      echo "${CUR_SSHD_PROCESS}" | grep "sshd" >/dev/null
      if [ $? -ne 0 ] ; then
        echo "ERROR: The process ${CUR_SSHD_PID} does not look like a running sshd"
	exit 15
      fi
      echo "Killing the running sshd process with the PID ${CUR_SSHD_PID} ..." 
      sudo kill  ${CUR_SSHD_PID} ; sleep 2 ; ps -p  ${CUR_SSHD_PID} >/dev/null && kill -9  ${CUR_SSHD_PID}
    fi
  fi
fi

if [ -r "${PARAMETER_FILE}" ] ; then
	CUR_SSHD_PARAMETER="${CUR_SSHD_PARAMETER} $( egrep -v "^#|^$" "${PARAMETER_FILE}" | tr "\n" )"
fi

if [ -r ${LOGFILE} -a ${DEBUG_MODE} != 0  ] ; then
	CUR_SSHD_PARAMETER="${CUR_SSHD_PARAMETER} -E ${LOGFILE}"
fi

echo "Restarting the sshd with the parameter \"${CUR_SSHD_PARAMETER}\" ...."

/system/bin/sshd ${CUR_SSHD_PARAMETER} 


echo "Checking the result ..."

SSHD_RESTARTED=${__FALSE}

CUR_SSHD_PID="$( cat "${SSHD_PID_FILE}" )"
if [ "${CUR_SSHD_PID}"x != ""x ] ; then
  CUR_SSHD_PROCESS="$( ps -f -p ${CUR_SSHD_PID} )"
  if [ "${CUR_SSHD_PROCESS}"x != ""x ] ; then
    SSHD_RESTARTED=${__TRUE}
  fi
fi

if [ ${SSHD_RESTARTED} = ${__TRUE} ] ; then
  echo "sshd successfully restarted:"
  echo "${CUR_SSHD_PROCESS}"
else
  [ -r "${LOGFILE}" ] && cat "${LOGFILE}"

  echo "ERROR: Looks like the restart of the sshd failed"
fi


