#!/bin/sh

# This script will load user prefs for terminal emulator and do the best job
# it can implementing them for the available terminal. Failing any user
# settings, a system default will apply. The user may also pass in the -e flag
# followed by a string which is the name of a program to be executed in the
# terminal, and the -p flag, indicating a terminal profile. Ex:
#
#   terminal -e vi -p myProfile

# Options are:
 
# APP: terminal app used; if none is specified, the script will find one

# LOGIN: 'yes' or 'no'. Is this a login terminal??
 
# SIZE: valid options are mini, normal, and dedicated. The first one runs a
# very small terminal which is useful as a utility for things like checking on
# processes and the like. normal is 80x24 and usually what you get when you
# run a terminal app without options. dedicated will probably take up your
# screen, and possibly more if you aren't careful

# FONT: three valid choices: small, normal, large. This seems a little
# fascist, and contrary to a distro whose motto is 'When choice matters', but
# odds are it's what you want 99% of the time. Personally, I find transparent
# terminals an eye-strain without an ample 10x20 fixed font. That's the normal
# setting. How many times have you strained to read 9x15? 10x20 should be good
# for almost anyone. large is there if you really need it, it's 12x24. small
# is there for stuff you check on only briefly

# FG: foreground color; it can be anything you see in the output of showrgb. I
# reccommend 'khaki' with black background; it's like applesauce for the eyes

# BG: background color, see above

# TRANSPARENT: valid values are 'yes' and 'no'. I'm sure you know what this
# means

# TINT: translucent tint, overrides BG. Any 'showrgb' color

# SHADE: degree of shade for translucency

# SCROLLBAR: whether or not there should be a scrollbar

# Process arguments
while getopts ":e:p:" option; do
  case $option in
    e) EXECUTE=$OPTARG ;;       # 'e' for 'execute'
    p) PROFILE=$OPTARG ;;       # 'p' for 'profile'
    \?)                         # A shit option was given to terminal
      echo "$0: Invalid argument to terminal!"
      echo "$0: usage: terminal [-e command] [-p profile]"
      exit 1 ;;
  esac
done

## If no PROFILE was specified, stick with 'default'
if [ "x$PROFILE" = "x" ]; then
  PROFILE='default'
fi

## Load settings if they exist
if [ -f "$HOME/.terminal/$PROFILE" ]; then
  . "$HOME/.terminal/$PROFILE"
fi

# We will find a term if none is supplied. konsole is excluded from this
# list because very few of its options are configured through the command
# line. If you wish to run konsole; then by all means do so, but it's not
# in here
if [ "x$APP" = "x" ]; then
  PROG_LIST="mrxvt aterm xterm"

  for PROG in $PROG_LIST; do
    which $PROG &> /dev/null;
    if [ $? = 0 ]; then
      APP=$PROG
      break;
    fi
  done
fi

# We will assume a default of login terminal because it more closely matches
# logging in at a virtual console.
if [ "x$LOGIN" = "x" ]; then
  LOGIN=yes
fi

if [ "x$SIZE" = "x" ]; then
  ## No size given; we will consequently pick 'normal' as the default
  SIZE="normal"
fi

if [ "x$FONT" = "x" ]; then
  # 'normal' also the default for font, unless SIZE is not "normal", in
  # which case it is set to match, more or less
  case $SIZE in
    mini) FONT="small" ;;
    normal|dedicated) FONT="normal" ;;
  esac
fi

if [ "x$TRANSPARENT" = "x" ]; then
  ## Assume no, it's useless eye-candy :D
  TRANSPARENT=no
fi

if [ "x$BG" = "x" ]; then # && [ "x$TRANSPARENT" != "xyes" ]; then
  # By default, let's make it black. It looks cool and it's easy on the
  # eyes
  BG="black"
fi

## Set no default tint
# if [ "x$TINT" = "x" ] && [ "x$TRANSPARENT" = "xyes" ]; then 
#   # There's no accounting for taste, but I find cyan is an aesthetically
#   # conservative choice (i.e., least likely to look butt ugly on any given
#   # background
#   TINT="cyan"
# fi

if [ "x$SHADE" = "x" ]; then
  # I will set 70% as a default because I care about your eyes
  SHADE="70"
fi

# Here we try to determine an acceptable foreground color if none has been
# set.
 
# For a transparent background, always white. Nothing else really works
# better. For a solid background, it depends. If the background is light,
# the default is black. If the background is dark, the default is khaki.
if [ "x$FG" = "x" ]; then
  if [ "x$TRANSPARENT" = "xyes" ]; then
    FG="white"
  else
    export BG
    VALUE=$(showrgb | perl -wnle 'chomp; my @line = split /\t+/;
                                  if ($ENV{"BG"} eq $line[1]) {
                                    my $avg;
                                    $avg += int for split " ", $line[0];
                                    $avg /= 3;
                                    ($avg >= 128) ? print "light" :
                                                    print "dark";
                                  }')
    if [ "$VALUE" = "light" ]; then
      FG="black"
    else 
      FG="khaki"
    fi
  fi
fi

# If SCROLLBAR is not specified, assume yes, unless this is a mini terminal
if [ "x$SCROLLBAR" = "x" ]; then
  if [ $SIZE != "mini" ]; then
    SCROLLBAR=yes
  else
    SCROLLBAR=no
  fi
fi

## Now start constructing a command line from the options

## First we get the terminal name in, simple enough

CMD="$APP"

## Next, enable login if necessary
if [ $LOGIN = "yes" ]; then
  case $APP in
    mrxvt|aterm|xterm) CMD="$CMD -ls"
  esac
fi

## Then, set the size
case $APP in
  mrxvt|aterm|xterm)
    case $SIZE in
      mini) CMD="$CMD -geometry 80x20" ;;
      normal) CMD="$CMD -geometry 80x25" ;;
      dedicated) CMD="$CMD -geometry 80x36" ;;
    esac
esac

# Now that we've set the size, set the font. This is extensible for
# other languages; however, I have only written the default, which is
# all non-Unicode English locales and C. Will foreign users please add
# as necessary?
case $APP in
  mrxvt|aterm|xterm)
    case $LANG in
      en_[A-Z][A-Z]|C)
       case $FONT in
         small) CMD="$CMD -fn 9x15" ;;
         normal) CMD="$CMD -fn 10x20" ;;
         large) CMD="$CMD -fn 12x24" ;;
       esac
    esac
esac

## Next, enable transparency if needed, as well as related options
if [ "x$TRANSPARENT" = "xyes" ]; then
  case $APP in
    aterm)
      ## The shade option for aterm is ass-backwards
      CMD="$CMD -tr -trsb -sh $(( 100 - $SHADE ))" ;;
    mrxvt)
      CMD="$CMD -tr -trs -shade $SHADE" ;;
  esac

  ## Enable tint as well, if necessary
  if [ "x$TINT" != "x" ]; then
    case $APP in
      aterm|mrxvt) CMD="$CMD -tint $TINT"
    esac
  fi
fi

## Last, set foreground and background
case $APP in
  mrxvt|aterm|xterm)
    ## Disable bg for transparent terminal
    # if [ "x$TRANSPARENT" != "xyes" ]; then
    #  CMD="$CMD -bg $BG"
    # fi
    CMD="$CMD -fg $FG -bg $BG"
esac

# Settings for i18n. Please add as necessary; i18n support in this script
# is really poor
case $APP in
  xterm)
    case $LANG in
      *utf8) CMD="$CMD -u8" ;;
    esac
esac

## Add or subtract scrollbar
case $APP in
  mrxvt|aterm|xterm)
    if [ $SCROLLBAR="yes" ]; then
      CMD="$CMD +sb"
    else
      CMD="$CMD -sb"
    fi
esac

## Now add any command that was specified in the command line
if [ "x$EXECUTE" != "x" ]; then
  case $APP in
    mrxvt|aterm|rxvt) CMD="$CMD -e $EXECUTE"
  esac
fi

echo "Executing $CMD"
exec $CMD
