New Script -- run-parts
[pkgusr] / etc / pkgusr / bash_profile
index 52d885e..4548e0f 100644 (file)
@@ -1,31 +1,38 @@
 # -*- shell-script -*-
-# couple of environment settings to ensure sanity
+## Ensure sanity
 set +h
 umask 022
-LC_ALL=POSIX
+
+## We live in a unicode world now
+LANG=en_AU.UTF-8
+LC_CTYPE=en_AU.UTF-8
 
 ## PATH
-# The wrappers directory must be the first entry in the PATH.  
+# The wrappers directory must be the first entry in the PATH, and
+# /tools/bin should be last.
 #
-# Once you are no longer needing or using the tools dir you can remove it
-# from the PATH if you want, but leaving it there won't hurt.
-#PATH=/usr/lib/pkgusr:/usr/bin:/bin:/usr/X11R6/bin:/opt/qt/bin:/tools/bin
-PATH=/usr/lib/pkgusr:/usr/bin:/bin:/usr/X11R6/bin:/opt/qt/bin
+# Once you are no longer needing or using the tools dir you can remove
+# it from the PATH.
+#PATH=/usr/lib/pkgusr:/usr/bin:/usr/X11R6/bin:/opt/qt/bin:/tools/bin
+PATH=/usr/lib/pkgusr:/usr/bin:/usr/X11R6/bin:/opt/qt/bin
 
 ## A couple things to make less(1) nicer.
-LESS=-MRgisw
+LESS='-FgiMrswX --use-color'
 LESSCHARSET=utf-8
 LESSOPEN='|lesspipe.sh %s'
 
 ## Timezone -- set to your local zone
 TZ='Australia/Brisbane'
 
+## Shell Opts
+shopt -s autocd
+
 ## QT
 QTDIR=/opt/qt
 
 ## pkg-config
 _XORG=/usr/X11R6/lib/pkgconfig:/usr/X11R6/share/pkgconfig
-_KDE=/opt/kde/lib/pkgconfig:/opt/kde/share/pkgconfig
+_KDE=/opt/kde/lib/pkgconfig
 _QT=${QTDIR}/lib/pkgconfig
 _PKGCFG=$(pkg-config --variable pc_path pkg-config)
 PKG_CONFIG_PATH=${_PKGCFG}:${_XORG}:${_KDE}:${_QT}
@@ -54,26 +61,169 @@ SUPPRESSLOCALEDIR=1
 #  Override this in ~/.pkgusrrc.
 CHECKUPDATES=1
 
-### export everything
-export LC_ALL PATH LESS LESSCHARSET LESSOPEN TZ PKG_CONFIG_PATH QTDIR
-export SUPPRESSLOCALEDIR CHECKUPDATES
+## Slackware PKG local repo
+#  Having a shortcut to a local Slackware PKG repo is very useful.
+#  Pat Volkerding and his team are of a minority who actually know
+#  what they are doing.  I always check Slackware for patches.
+#
+#  I get the Slack source via rsync.
+#  See https://mirrors.slackware.com/mirrorlist/
+#
+SLACKPKG=/home/steve/download/Slackware/slackware64-current/source
+#  Obviously edit to suit your situation
+
+## Export everything
+export LANG LC_CTYPE PATH LESS LESSCHARSET LESSOPEN TZ PKG_CONFIG_PATH QTDIR
+export SUPPRESSLOCALEDIR CHECKUPDATES SLACKPKG
+
+## Bash Prompt
+#  Shell prompts are not just eye-candy they should show what you need
+#  to know most and they should do it in such a way that you don't
+#  even notice.  This sets up a two-line prompt that reminds you that
+#  you're currently logged in as a pkgusr, which pkgusr, which group
+#  if that pkgusr has multiple groups and you've newgrp'd, if you're
+#  inside a git repo display the branch and other pertinent info, if
+#  the previous command had a non-zero exit display the code, display
+#  the current time, and current working directory (truncated to
+#  prevent "prompt-creep"), and set the Xterm title.  Colourised so
+#  that often you won't even need to read it to take in the info.
+
+# Colours (doesn't include black (30))
+CO="\[\e[0m\]"        # Colour Off
+RED="\[\e[0;31m\]"    # Red
+GRN="\[\e[0;32m\]"    # Green
+YEL="\[\e[0;33m\]"    # Yellow
+BLU="\[\e[0;34m\]"    # Blue
+MAG="\[\e[0;35m\]"    # Magenta
+CYN="\[\e[0;36m\]"    # Cyan
+WTE="\[\e[0;37m\]"    # White
+# Bold (bright)
+BRED="\[\e[1;31m\]"   # Bold Red
+BGRN="\[\e[1;32m\]"   # Bold Green
+BYEL="\[\e[1;33m\]"   # Bold Yellow
+BBLU="\[\e[1;34m\]"   # Bold Blue
+BMAG="\[\e[1;35m\]"   # Bold Magenta
+BCYN="\[\e[1;36m\]"   # Bold Cyan
+BWTE="\[\e[1;37m\]"   # Bold White
+
+# Some pkgusrs may use extra groups so the prompt should reflect that.
+# This function adds the group name to the prompt if necessary.
+_sg()
+{
+    [[ $(id -gn) != $(id -un) ]] && echo "$WTE:$CYN$(id -gn)"
+}
+
+# Set the Xterm title
+_settitle()
+{
+    [[ "$TERM" = "xterm" ]] && echo "\[\e]2;PKG(\u)\a\]"
+}
+
+# Draw a horizontal line from the end of the top line of the prompt to
+# the right hand edge of the terminal.
+_hline()
+{
+    # 11 is the number of chars taken up by "-(HH:MM)-()"
+    TRIM=$((${#PWD} + 11))
+
+    # If there was a non-zero return there will be an extra 4 + length
+    # of rc chars
+    [[ $rc -gt 0 ]] && TRIM=$(($TRIM + ${#rc} + 4))
+
+    # Allow for $HOME being displayed as ~
+    [[ $HOME == ${PWD::${#HOME}} ]] && TRIM=$(($TRIM - ${#HOME} + 1))
+
+    a=($(seq -s ' ' 1 $(($COLUMNS - $TRIM))))
+    printf '%s' ${a[@]/*/-}
+}
+
+# This function, called from $PROMPT_COMMAND, puts all the pieces of
+# the prompt together.
+make_prompt()
+{
+    rc=$?                      # Return code from previous cmd
+
+    # Normally you'd define your local vars at the top of the function,
+    # well I normally do, but in this case it will overwrite the value
+    # of '$?' so we need to preserve it first.
+    local gitp p prc sg title hline
+
+    # Previous cmd's return code, but only if it was non-zero
+    [[ $rc -gt 0 ]] && prc="$BWTE--[$BRED$rc$BWTE]" || prc=
+
+    # Using a subgroup
+    sg=$(_sg)
 
-# Make prompt reflect that we are a package user.
-export PROMPT_COMMAND='PS1="[pkgusr (\u)] \w> "'
+    # Git
+    [[ -f /etc/pkgusr/git-prompt ]] && gitp=$(__git_ps1) || gitp=
 
-# Suck in some handy shell functions
+    p="pkgusr"
+
+    # Xterm title
+    title=$(_settitle)
+
+    # Horizontal line
+    hline=$(_hline)
+
+    # Build up the PS1
+    PS1="$BWTE-($BYEL\A$BWTE)-($BMAG\w$BWTE)$prc" # -(TIME)-(CWD)--[rc]
+    PS1+="$BBLU$hline\n"
+    PS1+="$BWTE[$YEL$p $BWTE($CYN\u$sg$BWTE)"   # [pkgusr (usr:grp)
+    PS1+="$CO"                                  # reset to not mess up git prompt
+    PS1+="$gitp"                                # (gitstuff)
+    PS1+="$BWTE]>$CO "                          # ]>
+    PS1+="$title"                              # Xterm title
+}
+
+# The git goodies
+[[ -f /etc/pkgusr/git-prompt ]] && source /etc/pkgusr/git-prompt
+GIT_PS1_SHOWDIRTYSTATE=true
+GIT_PS1_SHOWSTASHSTATE=true
+GIT_PS1_SHOWUNTRACKEDFILES=true
+GIT_PS1_SHOWUPSTREAM=auto
+GIT_PS1_DESCRIBE_STYLE=branch
+GIT_PS1_SHOWCOLORHINTS=true
+
+# Set this so the prompt doesn't fly off the right-hand edge of the
+# screen when you're down a long directory tree.
+#PROMPT_DIRTRIM=4
+
+# Finally, make the prompt happen!
+PROMPT_COMMAND=make_prompt
+
+
+## Pretty colours for ls
+eval $(dircolors -b)
+alias ls='ls --color=always -Fb'
+
+## Suck in some handy shell functions
 . /etc/pkgusr/handy_funcs
 
-# If they exist, load any private settings.  This comes last so that
-# we can override any system defaults if need be
-if [ -f ${HOME}/.pkgusrrc ]; then
-    . ${HOME}/.pkgusrrc
+## Go to the home directory whenever we su to a package user.
+cd
+
+## Setup the git config if needed
+if [[ ! -d ${HOME}/.config/git && -f ${HOME}/.gitconfig ]]; then
+    prep_git
 fi
 
-# Go to the home directory whenever we su to a package user.
-cd
-# Maybe check if the build script can be updated.
-if [ ${CHECKUPDATES} -eq 1 ]; then
+## Bash Completion
+if [ -f /usr/share/bash-completion/bash_completion ]; then
+    . /usr/share/bash-completion/bash_completion
+fi
+
+## Per-user settings
+#  Called last so that defaults can be overridden.
+[[ -f ${HOME}/.pkgusrrc ]] && . ${HOME}/.pkgusrrc
+
+###===============================================================###
+# Don't put anything below here unless it relies on something being #
+# set in ~/.pkgusrrc                                                #
+###===============================================================###
+
+# Maybe check if the build script can be updated, but not on dumb
+# terms so that TRAMP still works.
+if [ ${CHECKUPDATES} -eq 1 -a "$TERM" != "dumb" ]; then
     checkupdates
 fi