aboutsummaryrefslogtreecommitdiff
path: root/multihome
blob: 555665bda765e4917a2058c0a88a8ec1a30d831d (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
#!/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/>.

# 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* )

# 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
	echo "Forcing clustered environment"
	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 "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 )

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

			if [ ! -f "$DEST/$file" ]; then
				cp -a $SRC $DEST 2>/dev/null
			fi
		done )
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 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 ###