#!/bin/bash
#
# Simple script that echoes the command line, it's environment variables and
# the user details it is running under
#
# Copyright 2023 BeyondCron Pty Ltd and related entities / All rights reserved.

main() {

  initCommands

  echo Command
  echo -------
  echo "$*"
  echo

  echo User details
  echo ------------
  echo 'user:' `${cmd_id} -un`
  echo 'group:' `${cmd_id} -gn`
  echo 'umask:' `umask`
  echo 'dir:' `pwd`
  echo

  echo Environment
  echo -----------
  vars=`${cmd_env} | ${cmd_sort}`
  while read -r var
  do
    echo "${var/=/ = }"
  done <<< "${vars}"
  echo

  read line
  if [ -n "${line}" ]; then
    echo Output
    echo ------
    echo "${line}"
    echo
  fi

  echo Input
  echo -----
  IFS=''
  while read -r line
  do
    echo "${line}"
  done

  exit 0
}

initCommands() {
  if [ -x /usr/bin/env ]; then
    cmd_env=/usr/bin/env
  else
    cmd_env=/bin/env
  fi

  if [ -x /usr/bin/id ]; then
    cmd_id=/usr/bin/id
  else
    cmd_id=/bin/id
  fi

  if [ -x /usr/bin/sort ]; then
    cmd_sort=/usr/bin/sort
  else
    cmd_sort=/bin/sort
  fi
}

main $*

# end