# base-files version 4.0-6
# /etc/profile: executed by the command interpreter for login shells.

# The latest version as installed by the Cygwin Setup program can
# always be found at /etc/defaults/etc/profile

# Modifying /etc/profile directly will prevent
# setup from updating it.

# System-wide profile file

# Some resources...
# Customizing Your Shell: http://www.dsl.org/cookbook/cookbook_5.html#SEC69
# Consistent BackSpace and Delete Configuration:
#   http://www.ibb.net/~anne/keyboard.html
# The Linux Documentation Project: http://www.tldp.org/
# The Linux Cookbook: http://www.tldp.org/LDP/linuxcookbook/html/

# Setup some default paths. Note that this order will allow user installed
# software to override 'system' software.
# Modifying these default path settings can be done in different ways.
# To learn more about startup files, refer to your shell's man page.
PATH="/usr/local/bin:/usr/bin:${PATH}"
MANPATH="/usr/local/man:/usr/share/man:/usr/man:${MANPATH}"
INFOPATH="/usr/local/info:/usr/share/info:/usr/info:${INFOPATH}"

# Set the user id
USER="$(id -un)"

# TMP and TEMP are defined in the Windows environment. Leaving
# them set to the default Windows temporary directory or unset
# can have unexpected consequences. 
unset TMP TEMP
TMP="/tmp"
TEMP="/tmp"

# Define default printer
p='/proc/registry/HKEY_CURRENT_USER/Software/Microsoft/Windows NT/CurrentVersion/Windows/Device'
if [ -e "${p}" ] ; then
  read -r PRINTER < "${p}" 
  PRINTER=${PRINTER%%,*}
fi
unset p

# Default to removing the write permission for group and other
#  (files normally created with mode 777 become 755; files created with
#  mode 666 become 644)
umask 022

# Here is how HOME is set, in order of priority, when starting from Windows
#  1) From existing HOME in the Windows environment, translated to a Posix path
#  2) from /etc/passwd, if there is an entry with a non empty directory field
#  3) from HOMEDRIVE/HOMEPATH
#  4) / (root)
# If the home directory doesn't exist, create it.
if [ ! -d "${HOME}" ]; then
  if mkdir -p "${HOME}"; then
    echo "Copying skeleton files."
    echo "These files are for the users to personalise their cygwin experience."
    echo
    echo "They will never be overwritten nor automatically updated."
    echo
    cd /etc/skel || echo "WARNING: Failed attempt to cd into /etc/skel!"
    /usr/bin/find . -type f | while read f; do
      fDest=${f#.}
      if [ ! -e "${HOME}${fDest}" -a ! -L "${HOME}${fDest}" ]; then
        /usr/bin/install -D -p -v "${f}" "${HOME}/${fDest}"
      fi
    done
    unset fDest
  else
    echo "${HOME} could not be created."
    { [ -d "${TEMP}" ] && HOME="${TEMP}"; } ||
    { [ -d "${TMP}" ] && HOME="${TMP}"; } ||
    { [ -d /tmp ] && HOME=/tmp; } ||
    HOME=/
    echo "Setting HOME to ${HOME}."
  fi
fi

# Make sure we start in home unless invoked by CHERE
if [ ! -z "${CHERE_INVOKING}" ]; then
  unset CHERE_INVOKING
else
  cd "${HOME}" || echo "WARNING: Failed attempt to cd into ${HOME}!"
fi

# Shell dependent settings
profile_d ()
{
  saved_LC_COLLATE=${LC_COLLATE}
  LC_COLLATE=C
  for file in /etc/profile.d/*.$1; do
    [ -e "${file}" ] && . "${file}"
  done
  LC_COLLATE=${saved_LC_COLLATE}
  unset saved_LC_COLLATE
}

if [ ! "x${BASH_VERSION}" = "x"  ]; then
  HOSTNAME=$(/usr/bin/hostname)
  profile_d sh
  [ -f "/etc/bash.bashrc" ] && . "/etc/bash.bashrc"
elif [ ! "x${KSH_VERSION}" = "x" ]; then
  typeset -l HOSTNAME=$(/usr/bin/hostname)
  profile_d sh
  PS1=$(print '\033]0;${PWD}\n\033[32m${USER}@${HOSTNAME} \033[33m${PWD/${HOME}/~}\033[0m\n$ ')
elif [ ! "x${ZSH_VERSION}" = "x" ]; then
  HOSTNAME=$(/usr/bin/hostname)
  profile_d zsh
  PS1='(%n@%m)[%h] %~ %% '
elif [ ! "x${POSH_VERSION}" = "x" ]; then
  HOSTNAME=$(/usr/bin/hostname)
  PS1="$ "
else 
  HOSTNAME=$(/usr/bin/hostname) 
  profile_d sh
  PS1="$ "
fi

export PATH MANPATH INFOPATH USER TMP TEMP PRINTER HOSTNAME PS1 

# Check to see if mkpasswd/mkgroup needs to be run try and cut down the emails
#   about this on the lists!
#
# The following are the conditions under which the group name special cases 
#   will appear (where uid and gid are the effective user and group ids
#   for the current user, and pgsid is the primary group associated with the
#   SID for the current user):
#       mkpasswd:
#         gid is not in /etc/group
#         uid is not in /etc/passwd
#       passwd/group_GID_clash(<gid>/<pgsid>):
#         gid is not in /etc/group
#         uid is in /etc/passwd
#         pgsid is in /etc/group (and does not match gid)
#       mkgroup:
#         gid is not in /etc/group
#         uid is in /etc/passwd
#         pgsid is not in /etc/group
#
# If this message keeps appearing and you are sure it's a mistake (ie, don't
#   email about it!), comment out the test below.
case "$(id -ng)" in
mkpasswd )
  echo "Your group is currently \"mkpasswd\".  This indicates that your"
  echo "gid is not in /etc/group and your uid is not in /etc/passwd."
  echo
  echo "The /etc/passwd (and possibly /etc/group) files should be rebuilt."
  echo "See the man pages for mkpasswd and mkgroup then, for example, run"
  echo
  echo "mkpasswd -l [-d] > /etc/passwd"
  echo "mkgroup  -l [-d] > /etc/group"
  echo
  echo "Note that the -d switch is necessary for domain users."
  ;;
passwd/group_GID_clash* )
  echo "Your group is currently \"passwd/group_GID_clash(gid/pgsid)\".  This"
  echo "indicates that your gid is not in /etc/group, but the pgsid (primary "
  echo "group associated with your SID) is in /etc/group."
  echo
  echo "The /etc/passwd (and possibly /etc/group) files should be rebuilt."
  echo "See the man pages for mkpasswd and mkgroup then, for example, run"
  echo
  echo "mkpasswd -l [-d] > /etc/passwd"
  echo "mkgroup  -l [-d] > /etc/group"
  echo
  echo "Note that the -d switch is necessary for domain users."
  ;;
mkgroup )
  echo "Your group is currently \"mkgroup\".  This indicates that neither"
  echo "your gid nor your pgsid (primary group associated with your SID)"
  echo "is in /etc/group."
  echo
  echo "The /etc/group (and possibly /etc/passwd) files should be rebuilt."
  echo "See the man pages for mkpasswd and mkgroup then, for example, run"
  echo
  echo "mkpasswd -l [-d] > /etc/passwd"
  echo "mkgroup  -l [-d] > /etc/group"
  echo
  echo "Note that the -d switch is necessary for domain users."
  ;;
esac
