This repository has been archived on 2019-10-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
xtermmaker/xtermmaker.sh
heinzel 4ba9ea984f Removed CVS keywords...
... and increased version number.
2011-09-08 16:55:54 +02:00

317 lines
6.2 KiB
Bash
Executable File

#!/bin/bash
# ~/scripts/xtermmaker.sh
VERSION='XTermMaker Version 4.3 (08.Sep.2011 heinzel)'
### config ###
CONFIG="${HOME}/.xtermmakerrc"
NUMBER=1
COLOR="RAND"
DEFAULT_THEME="light"
XTERM="xterm"
#XTERM="echo xterm"
XTERM_OPTS="-sb -sl 1024"
RSH="ssh"
RHOST=""
RUSER=""
# if cols > 0, we arange the possitions of the xterms in $cols columns
COLS=0
hoffset=64
voffset=64
width=499
height=348
space=4
### functions ###
print_version() {
echo "$VERSION"
}
print_help() {
cat <<EOH
Usage: xtermmaker.sh [-n <number>] [-C <columns>] [-u <user>] [-r <host>]
[-c <color>] [-g] [-b] [-z] [-t <theme>] [-p <preset>]
[-w] [-h,--help] [-V,--version] [- <xterm options>]
-n Opens <number> xterms (default: $NUMBER).
-C If specified, the xterms will be aranged in <columns> columns.
-r Exec remote shell to the specified hostname.
-u Specifies the username for the remote host.
-c Set the backgroundcolor of the xterms.
-g Same as -c Gray.
-b Generates 'Bunte XTerms' in serial sequence.
-z Generates 'Bunte XTerms' in random sequence (default).
-t Use color theme <theme> for 'Bunte XTerms'.
Defined themes: $THEMES (default: $DEFAULT_THEME)
-w Use 'wterm' with transparency instead of xterm.
-p Use preset <preset>.
Defined presets: $PRESETS
Anything after a standalone dash (-) will be passed as options
to the xterm application.
EOH
}
rand() {
# Usage: rand <int>
local m="${1:-1}"
local f="/dev/urandom"
local n=""
n="`dd if=\"$f\" bs=8 count=1 2>/dev/null | md5sum | \
tr -d \"[:alpha:] [:punct:]\" | cut -c-2`"
expr \( $n \* $m \) / 100 + 1
}
parse_config() {
# Usage: parse_config
local f="${CONFIG}"
local section="";
local theme="";
local preset="";
local buf="";
THEMES=""
PRESETS=""
if test "X${f}" != "X" -a -r "$f" ; then
while read line ; do
if expr "$line" : "#.*" >/dev/null ; then
continue
elif test "X${line}" = "X" ; then
continue
elif expr "$line" : "\[.*\]" >/dev/null ; then
section="$line"
elif test "X${section}" = "X[themes]" ; then
if expr "$line" : ".*:" >/dev/null ; then
theme="`echo \"$line\" | tr -d ': '`"
THEMES="${THEMES}${theme}, "
elif test "X${theme}" != "X" ; then
eval THEME_${theme}="\"\${THEME_${theme}}${line%,},\""
else
echo "Error parsing themes config ($f)" >&2
fi
elif test "X${section}" = "X[presets]" ; then
if expr "$line" : ".*:" >/dev/null ; then
preset="`echo \"$line\" | tr -d ': '`"
PRESETS="${PRESETS}${preset}, "
elif test "X${preset}" != "X" ; then
buf="$line
"
eval PRESET_${preset}="\"\${PRESET_${preset}}${buf}\""
else
echo "Error parsing presets config ($f)" >&2
fi
else
echo "Error parsing config ($f): Unknown section '$section'" >&2
fi
done < "$f"
fi
# Do cosmetic on the themes and presets list.
THEMES="${THEMES%, }"
PRESETS="${PRESETS%, }"
# If the default theme was not defined (or has zero colors), define it.
eval buf="\"\${THEME_${DEFAULT_THEME}}\""
if test "X${buf}" = "X" ; then
eval THEME_${DEFAULT_THEME}="\"gray\""
fi
# If the default preset was not defined, define it with zero entries.
if test "X${PRESET_default}" = "X" ; then
PRESET_default=""
fi
}
### set options ###
parse_config
# read arguments from stdin
while test $# -gt 0 ; do
case "$1" in
-h|-help|--help)
print_help
exit 0
;;
-V|-version|--version)
print_version
exit 0
;;
-n|-number|--number)
NUMBER="$2"
shift
shift
;;
-C|-cols|--cols)
COLS="$2"
shift
shift
;;
-c|-color|--color)
COLOR="$2"
shift
shift
;;
-g)
COLOR=Gray
shift
;;
-b)
COLOR=BUNT
shift
;;
-z)
COLOR=RAND
shift
;;
-t|-theme|--theme)
THEME="$2"
shift
shift
;;
-p|-preset|--preset)
PRESET="$2"
shift
shift
;;
-u|-user|--user)
RUSER="$2"
shift
shift
;;
-r|-host|--host)
RHOST="$2"
shift
shift
;;
-w)
if test -x "`which wterm`" ; then
XTERM="wterm"
XTERM_OPTS="$XTERM_OPTS -tr -sh"
fi
shift
;;
-|--)
shift
XTRA_OPTS=$@
break
;;
esac
done
test "X${THEME}" = "X" && THEME="$DEFAULT_THEME"
test "X${PRESET}" = "X" && PRESET="default"
test "X${NUMBER}" = "X" && NUMBER=1
eval buf="\"\${THEME_${THEME}}\""
if test "X${buf}" = "X" ; then
echo "Theme '$THEME' is not defined." >&2
exit 1
fi
if ! set | grep "^PRESET_${PRESET}=" >/dev/null ; then
echo "Preset '$PRESET' is not defined." >&2
exit 1
fi
### action ###
# position of the first window
column=1
hpos="$hoffset"
vpos="$voffset"
eval colors="\"\${THEME_${THEME}%,}\""
ncolors="`echo \"$colors\" | tr -d '[[:alnum:]]'`"
ncolors="${#ncolors}"
ncolors="`expr $ncolors + 1`"
eval presets="\"\${PRESET_${PRESET}}\""
npresets="`echo \"$presets\" | wc -l`"
npresets="`expr $npresets - 1`"
xterm_count=1
color_count=1
until test $xterm_count -gt $NUMBER ; do
if test "1${xterm_count}" -le "1${npresets}" ; then
set_opts="`echo \"$presets\" | sed -ne \"${xterm_count}p\"`"
else
set_opts=""
fi
title_opt="-title $xterm_count"
case "$COLOR" in
NONE)
;;
BUNT)
bg_opt="-bg `echo $colors | cut -f $color_count -d ,`"
fg_opt="-fg ${FG_COLOR-black}"
if test "$color_count" -lt "$ncolors" ; then
color_count=`expr $color_count + 1`
else
color_count=1
fi
;;
RAND)
color_count=`rand $ncolors`
bg_opt="-bg `echo $colors | cut -f $color_count -d ,`"
fg_opt="-fg ${FG_COLOR-black}"
;;
*)
bg_opt="-bg $COLOR"
fg_opt="-fg ${FG_COLOR-black}"
;;
esac
if test "$COLS" -gt 0 ; then
# position for this window
pos_opt="-geometry +${hpos}+${vpos}"
# calculate the position for the next window
if test "$column" -lt "$COLS" ; then
# current column is less than max columns ->
# we can place the next window right of this one
column="`expr $column + 1`"
hpos="`expr $hpos + $width + $space`"
else
# we need a new row
column=1
hpos="$hoffset"
vpos="`expr $vpos + $height + $space`"
fi
else
pos_opt=""
fi
if test "X${RHOST}" != "X" ; then
t="$RHOST"
if test "X${RUSER}" != "X" ; then
t="${RUSER}@${RHOST}"
fi
exec_opt="-e $RSH $t"
fi
$XTERM $XTERM_OPTS $title_opt $bg_opt $fg_opt $pos_opt \
$set_opts $XTRA_OPTS $exec_opt &
xterm_count=`expr $xterm_count + 1`
done
exit 0