#! /bin/bash

# Terminal / UTF-8 setup
function tutf
{
  export LANG=en_CA.UTF-8
  if [ "$PS1" ]; then
     if [ "$TERM" == "rxvt-unicode" ]; then
       printf '\e]701;%s\007' $LANG
     fi
     export PROMPT_COMMAND='echo -ne "\033]0;u8:${USER}@${HOSTNAME}: ${PWD}\007"'
  fi
}

function tiso
{
  export LANG=en_CA.ISO-8859-1
  if [ "$PS1" ]; then
    if [ "$TERM" == "rxvt-unicode" ]; then
      printf '\e]701;%s\007' $LANG
    fi
    export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
  fi
}

if [ -z "$LANG" ]; then
  # Set default LANG
  if [ "$TERM" == "rxvt-unicode" ]; then
    export LANG=en_CA.UTF-8
  else
    export LANG=en_CA.ISO-8859-1
  fi
fi

# Fix "utf8"
if [[ "$LANG" =~ "utf8" ]]; then
  export LANG=en_CA.UTF-8
fi

# Setup prompt, title and such
if [[ "$LANG" =~ "UTF-8" ]]; then
  tutf
else
  tiso
fi
