#!/bin/zsh
# ====================[ zshenv.login                       ]====================
#                     [ Time-stamp: "2009-04-18 19:29:24 leycec" ]
#
# --------------------( SYNOPSIS                           )--------------------
# Run before "zprofile", "zshrc", and "zlogin", for login shells. This is the
# first script ZSH calls when starting up.
#
# --------------------( DESCRIPTION                        )--------------------
# This file aggregates all environment variables into one file. Customarily,
# Gentoo disaggregates all environment variables into the "/etc/env.d/" path,
# from which it then dynamically constructs the "/etc/profile.env" file, which
# this file sources. I find that to be a rather clumsy and overly abstracted
# means for defining environment variables, however; I prefer this.

# ....................{ INITIALIZATION                     }....................
echo "zeshy: loading \"/etc/zsh/zshenv.login\"..."

# ....................{ DEPENDENCIES                       }....................
# Load environment settings from "profile.env", if that file exists. This is a
# programmatically-generated file created by:
#
# * Under Gentoo, "env-update" from the files in "/etc/env.d/".
# * Under Exherbo, "eclectic env update" from the files in "/etc/env.d/".
[ -e /etc/profile.env ] && source /etc/profile.env

# ....................{ PATHS                              }....................
# void PATH_append(char *PATHNAME, bool IS_NOT_TESTING_PATH)
#
# Add the passed pathname to this user's "$PATH", if: '
#
# * If "$IS_NOT_TESTING_PATH" is not passed or is passed but is empty, then if
#   that path exists, is readable by this user, and has not already been added.
# * If "$IS_NOT_TESTING_PATH" is passed and is non-empty, then that is
#   sufficient and we perform no path testing. (This should be used sparingly.)
PATH_append() {
  if [ -z "$1" ]; then
    echo "PATH_append: not passed a path to append!" 1>&2
    return
  fi

  if [ -n "$2" ] || [ -d "$1" -a -r "$1" ]; then
    if [ -n "$PATH" ]
    then PATH+=":$1"
    else PATH="$1"
    fi
  fi
}

# void PATH_append_if_root(char *PATHNAME)
#
# Add the passed pathname to this user's "$PATH", if the above conditions hold
# and if this user is the superuser (i.e., root).
PATH_append_if_root() {
  if [ "$EUID" = "0" ] || [ "$USER" = "root" ]
  then PATH_append $*
  fi
}

# Record the prior value of PATH: typically, a user-specific PATH as defined by
# one or several of the dependencies, above.
if [ "$EUID" = "0" ] || [ "$USER" = "root" ]; then
  if [ -n "$ROOTPATH" ]
  then USERPATH="$ROOTPATH"
  else USERPATH="$PATH"
  fi
else   USERPATH="$PATH"
fi

# Reset PATH to the empty string now that we've recorded its prior value.
PATH=

# Append all paths onto PATH.
PATH_append         "/usr/lib/colorgcc/bin"
PATH_append_if_root "/usr/local/sbin"
PATH_append         "/usr/local/bin"
PATH_append_if_root "/usr/sbin"
PATH_append         "/usr/bin"
PATH_append_if_root "/sbin"
PATH_append         "/bin"

# Append the prior value of PATH back onto PATH.
PATH+=":${USERPATH}"

# Append a user-specific path "~/bin" onto PATH.
PATH_append         "$HOME/bin"

# Lastly, export the new PATH into our caller's shell environment.
export PATH

# ....................{ PATHS =other                       }....................
# Read X.Org application defaults from this path.
export XAPPLRESDIR="$HOME/.X"
export XUSERFILESEARCHPATH="$XAPPLRESDIR/%N"

# Set the ZSH cache path, and make that path if it does not already exist.
# ZSH, itself, is unaware of this path. We find it convenient, nonetheless, to
# centralize ZSH caches into this path.
       ZSH_DOT_HOME="$HOME/.zsh"
[ -d "$ZSH_DOT_HOME" ] || mkdir "$ZSH_DOT_HOME"

# ....................{ *NIX                               }--------------------
# Enable Unicode support.
export LANG="en_US.utf8"
export LC_ALL="en_US.utf8"

# Select a default command-line editor and pager.
export EDITOR="/usr/bin/vim"
export PAGER="/usr/bin/less"

# ....................{ SYSTEM                             }--------------------
# Grep the system's total available memory in Kb.
export SYSTEM_RAM_KB=$(cat /proc/meminfo | awk -F '[ ]+' '/^MemTotal/ { print $2 }')

# Mark systems having more than 1GB memory as high-end, and all others as low-
# end. This simple test permits conditional logic based on system quality.
if [ "$SYSTEM_RAM_KB" -gt 1000000 ]; then
  export IS_SYSTEM_HIGH_END=1
else
  export IS_SYSTEM_HIGH_END=
fi

# ....................{ GENTOO                             }--------------------
# Reduce Paludis and Inquisitio verbosity and "upgrade eagerness."
export INQUISITIO_OPTIONS="\
--log-level warning \
--matcher pcre"
export PALUDIS_OPTIONS="\
--continue-on-failure if-satisfied \
--dl-blocks accumulate \
--dl-circular error \
--dl-downgrade warning \
--dl-new-slots always \
--dl-reinstall never \
--dl-reinstall-scm never \
--dl-reinstall-targets auto \
--dl-suggested install \
--dl-upgrade always \
--log-level warning \
--multitask \
--show-reasons summary \
--show-use-descriptions all \
--show-package-descriptions new \
--with-unused-dependencies"

# ....................{ ZSH                                }....................
# Enable an in-memory cache of ZSH history; persist this history to some file.
export HISTSIZE=1024  # maintain this number of lines of history
export SAVEHIST=1024  # ..retain this number of lines of history between logins
export HISTFILE="${ZSH_DOT_HOME}/history"

# ....................{ NET                                }--------------------
# Enable Firefox's Pango-enabled font antialiasing on high-end machines, but
# disable the same on low-end machines.
if [ -n "$IS_SYSTEM_HIGH_END" ]; then
  export MOZ_DISABLE_PANGO=
else
  export MOZ_DISABLE_PANGO=1
fi

# Enable SSH over non-anonymous (i.e., non-pserver) CVS.
export CVS_RSH=ssh

# ....................{ TEX                                }--------------------
# Locate user-specific "TeX" trees at this user-specific path. This variable
# defaults to "~/texmf/" in "/etc/texmf/texmf.d/05searchpaths.cnf", which as
# defaults go is rather clumsy. Thus, this. (Environment variables of the same
# name take precedence over "/etc/texmf/web2c/texmf.cnf" variables and also do
# not require updating on change. So, this is an elegant solution. Somewhat.)
#
# User-specific "TeX" trees should have directory structure resembling that of
# system-specific "TeX" trees (e.g., "/usr/share/texmf-dist/") but probably not.
# the overarching "TeX" tree at "/usr/share/texmf/".
export TEXMFHOME="$HOME/share/texmf"

# --------------------( COPYRIGHT AND LICENSE              )--------------------
# The information below applies to everything in this distribution,
# except where noted.
#              
# Copyleft 2007, 2008, 2009, and 2010 by Cecil Curry.
#   
#   http://www.raiazome.com
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
