aboutsummaryrefslogtreecommitdiff
path: root/multihome
blob: 56666cc5e436d88f3600dd43ef2ffdefcdca6b1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
#
# multihome is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# multihome is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with multihome.  If not, see <http://www.gnu.org/licenses/>.


# Associative arrays are not supported before BASH 4.x
if [ "$BASH_VERSINFO" -lt 4 ]; then
	echo echo \"multihome: unsupported BASH version, requires \>\= 4.x.x\"
	exit 3
fi

# Determine parent shell to know what kind of variable export mode we need later
parent_shell=`ps -ocommand= -p $PPID | awk -F/ '{print $NF}' | awk '{print $1}'| sed 's|-||'`

### MULTI-HOME BEGIN ###
# Define rc file for custom configuration
RC=$HOME/.homerc

# Add default cluster patterns
# Elements are delimited by SPACES
clusters=( )

transfer_env=( .*profile .*login .*cshrc* .bash* .Xauthority .ssh )
declare -A transfer_env_custom

# Don't edit these
hostname_short=$(hostname -s)
host_suffix=".stsci.edu"
home_remote_path=$HOME
cluster=""
cluster_force=false

if [ -f "$RC" ]; then
    source $RC
fi

# Are we on a cluster machine?
if [ ${#clusters[@]} -gt 0 ]; then
	for host in "${clusters[@]}"
	do
		if [[ "$HOSTNAME" == *${host}*${host_suffix} ]]; then
			cluster=${host}
			break
		fi
	done
fi

if [ "$cluster_force" != false ]; then
	cluster="standard"
fi

if [ ! -z "$cluster" ]; then
	# Assign directory structure
	home_local_path="/home/$USER/home_local/$cluster/$hostname_short"

	# If we are already inside, die
	if [[ "$PWD" == $home_local_path* ]]; then
		exit 2
	fi

	# If multi-home directory does not exist, create it
	if [ ! -d $home_local_path ]; then
		mkdir -p $home_local_path
		if [ $? -ne 0 ]; then
			echo echo \"No write access: \$home_local_path, aborting\"
			home_local_path=/home/$USER
		fi
	fi

	# Generate symlink to original home directory
	( cd $home_local_path ;
		if [ ! -L HEAD_"$USER" ]; then
			ln -sf "$home_remote_path" HEAD_"$USER"
		fi )

	( cd $home_local_path ;
		for file in "${transfer_env[@]}"
		do
			SRC="$home_remote_path/$file"
			DEST="$home_local_path"
			if [ "$file" == "multihome" ]; then
				continue
			fi

			if [ ! -f "$DEST/$file" ] || [ "$transfer_env_always" == true ]; then
				cp -a $SRC $DEST 2>/dev/null
			fi
		done )
	(cd $home_local_path ;
		if [ ! -z "${transfer_env_custom[${hostname_short}]}" ]; then
			for file in ${transfer_env_custom["${hostname_short}"]}
			do
				SRC="$file"
				DEST="$home_local_path"
				if [ ! -f "$DEST/$file" ] || [ "$tranfer_env_${hostname_short}_always" == true ]; then
					cp -a $SRC $DEST 2>/dev/null
				fi 
			done ;
		fi )
else
	home_local_path=/home/$USER
fi

SHELL_MODE=
SHELL_MODE_DELIMITER=
#Environment switch-over (external eval required)
case "$parent_shell" in
	bash|ksh|sh)
		SHELL_MODE="export"
		SHELL_MODE_DELIMITER="="
	;;
	tcsh|csh)
		SHELL_MODE="setenv"
		SHELL_MODE_DELIMITER=" "
	;;
	*)
	;;
esac

if [ -z "$SHELL_MODE" ]; then
	echo echo \"multihome: unsupported shell: \$parent_shell, aborting\"
	exit 1
fi

# Give us a new HOME
echo $SHELL_MODE HOME${SHELL_MODE_DELIMITER}"${home_local_path}"

### MULTI-HOME END ###