graffyworkenv/home/bin/cssh

164 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
HOSTNAME=`echo $@ | sed s/.*@//`
AS_ROOT='false'
if [ "$(echo $@ | grep '^root@' > /dev/null)" ]
then
AS_ROOT='true'
fi
function getHostRealFqdn()
{
local strHostname="$1"
local strHostFqdn="$(host $strHostname | tail -1 | awk '{print $1}')"
echo "$strHostFqdn"
}
HOST_FQDN=$(getHostRealFqdn $HOSTNAME)
SOLARIZED_BACKGROUND='{0, 8256, 10368}' #002b36 (solarized dark base03)
SAILOR_BLUE='{8832, 14208, 18816}' # 2e4a62
PRUNE='{15360, 10944, 14592}' #50394c
TAUPE='{19968, 18816, 16512}' #686256
DARK_ORANGE='{27456, 12864, 0}' #8f4300
DARK_OLIVE='{8832, 11904, 5568}' #2e3e1d
function get_current_terminal_window_id()
{
local terminal_windows_id=''
terminal_windows_id=$(osascript -e "tell application \"Terminal\" to get id of window 1")
echo "$terminal_windows_id"
}
function set_bg ()
{
local strOsxTermWindowId="$1"
local strColor="$2" # eg {45000, 0, 0, 50000}
osascript -e "tell application \"Terminal\" to set background color of (every window whose id is $strOsxTermWindowId) to $strColor"
}
function on_exit ()
{
set_bg "$THIS_OSX_TERM_WINDOW_ID" "$SOLARIZED_BACKGROUND"
}
# if [ $(echo $HOST_FQDN | grep '^simpatix') ]
# then
# BG_COLOR="$SAILOR_BLUE"
# elif [ $(echo $HOST_FQDN | grep '^physix') ]
# then
# BG_COLOR="$TAUPE"
# else
# case $HOST_FQDN in
# 'pr079234.spm.univ-rennes1.fr')
# BG_COLOR="$SOLARIZED_BACKGROUND"
# ;;
# 'puppet3.ipr.univ-rennes1.fr')
# BG_COLOR="$PRUNE"
# ;;
# *)
# BG_COLOR="$DARK_OLIVE"
# ;;
# esac
# fi
#if [ "$AS_ROOT" = 'true' ]
#then
# BG_COLOR="$DARK_ORANGE"
#fi
DCONF_PROFILES_PATH='/org/gnome/terminal/legacy/profiles:'
create_new_profile()
{
# copied from https://askubuntu.com/questions/270469/how-can-i-create-a-new-profile-for-gnome-terminal-via- command-line
local profile_ids=($(dconf list $DCONF_PROFILES_PATH/ | grep ^: |\
sed 's/\///g' | sed 's/://g'))
local profile_name="$1"
local profile_ids_old="$(dconf read "$DCONF_PROFILES_PATH"/list | tr -d "]")"
local profile_id="$(uuidgen)"
[ -z "$profile_ids_old" ] && local lb="[" # if there's no `list` key
[ ${#profile_ids[@]} -gt 0 ] && local delimiter=, # if the list is empty
dconf write $DCONF_PROFILES_PATH/list \
"${profile_ids_old}${delimiter} '$profile_id']"
dconf write "$DCONF_PROFILES_PATH/:$profile_id"/visible-name "'$profile_name'"
echo $profile_id
}
function get_terminal_profile_uuid()
{
# returns the id of the profile which has the given name.
# if the named profile doesn't exist, it creates it first
local profile_name="$1" # eg physix.ipr.univ-rennes1.fr
local profile=''
local this_profile=''
local profile_uuid=''
for this_profile in $(dconf list "$DCONF_PROFILES_PATH/")
do
# eg value for profile : ':b1dcc9dd-5262-4d8d-a863-c897e6d979b9/'
local this_profile_name=''
this_profile_name=$(dconf read $DCONF_PROFILES_PATH/${this_profile}visible-name | tr -d "'" )
# echo "this_profile_name=$this_profile_name"
if [ "$this_profile_name" = "$profile_name" ]
then
profile_uuid=$(echo "$this_profile" | sed 's/^://' | sed 's|/||')
echo "$profile_uuid"
return 0
fi
done
# the profile named $profile_name doesn't exist... create it then
profile_uuid=$(create_new_profile $profile_name)
echo "$profile_uuid"
}
function set_terminal_profile_bg_color()
{
local profile_name="$1" # eg physix.ipr.univ-rennes1.fr
local bg_color="$2" # eg rgb(17, 25, 24)
local profile_uuid=''
profile_uuid=$(get_terminal_profile_uuid $profile_name)
echo "profile_name=$profile_name profile_uuid=$profile_uuid"
dconf write $DCONF_PROFILES_PATH/:$profile_uuid/use-theme-colors 'false'
dconf write $DCONF_PROFILES_PATH/:$profile_uuid/background-color "'rgb$bg_color'"
}
COLOR_SATURATION='0.3'
COLOR_VALUE='0.2'
OS_NAME=$(uname)
case "$OS_NAME" in
'Darwin')
BG_COLOR=$(make_color.py $HOST_FQDN $COLOR_VALUE $COLOR_SATURATION osx)
THIS_OSX_TERM_WINDOW_ID=$(get_current_terminal_window_id)
set_bg "$THIS_OSX_TERM_WINDOW_ID" "$BG_COLOR"
trap on_exit EXIT
/usr/bin/ssh "$@"
;;
'Linux')
BG_COLOR=$(make_color.py $HOST_FQDN $COLOR_VALUE $COLOR_SATURATION linux)
if [ $? != 0 ]
then
echo "error : make_color.py failed"
exit 1
fi
set_terminal_profile_bg_color "$HOST_FQDN" "$BG_COLOR"
gnome-terminal --window-with-profile=$HOST_FQDN --title=$HOST_FQDN -- /usr/bin/ssh "$@"
;;
*)
echo "error : unexpeced os name : $OS_NAME"
exit 1
esac