-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·118 lines (102 loc) · 2.65 KB
/
setup.sh
File metadata and controls
executable file
·118 lines (102 loc) · 2.65 KB
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
#!/bin/bash
#
# setup.sh
# Setup my environment scripts on a new machine
#
# don't always depend on the 'HOME' env variable...
eval h="~/"
if [ -z "$h" -o "$h" = "~/" ]; then
echo "hmmm, I can't find your home."
echo "please install this environment manually."
echo ""
exit
fi
#eval mydir_=`dirname $(which $(test -L $0 && readlink $0 || echo $0))`
#mydir=$(echo $mydir_ | sed "s#^\./*#`pwd`/#; s#^\.\./#`pwd`/../#; s#[^/]*/\.\./##")
# this is _much_ cleaner...
#mydir="$(cd "$(dirname $0)" && pwd)"
# but this is much more _stable_
eval SCRIPT_PATH="${BASH_SOURCE[0]}";
if ([ -h "${SCRIPT_PATH}" ]) then
while([ -h "${SCRIPT_PATH}" ]) do SCRIPT_PATH=`readlink "${SCRIPT_PATH}"`; done
fi
pushd . > /dev/null
cd `dirname ${SCRIPT_PATH}` > /dev/null
SCRIPT_PATH=`pwd`;
popd > /dev/null
mydir=${SCRIPT_PATH%/}
h=${h%/}
echo "Installing environment in $mydir into $h"
echo -n "Continue? [Y|n] "
read ans
if [ "$ans" = "n" -o "$ans" = "N" -o "$ans" = "no" -o "$ans" = "No" ]; then
echo "aborting."
exit
fi
echo "setting up your environment..."
pushd "$h" 2>&1 >/dev/null
# check for existing '.bashrc' or '.bash_profile'
# back them up if they exist
installed_rc=0
backup_dir=".envbackup.$RANDOM$RANDOM"
while [ -d $backup_dir ]; do
backup_dir=".envbackup.$RANDOM$RANDOM"
done
files_backedup=
function backup_file() {
local file="$1"
local dst=$backup_dir/${file#.}
mkdir -p "$backup_dir" 2>/dev/null
# do a cp, then rm to handle symlinks somewhat gracefully
cp "$file" "$dst"
rm -f "$file"
files_backedup="$files_backedup\n\t$file -> $dst"
}
if [ -a .bashrc ]; then
backup_file .bashrc
if [ $installed_rc -lt 1 ]; then
ln -s "$mydir/bash_profile" .bashrc
installed_rc=1
fi
fi
if [ -a .bash_profile ]; then
backup_file .bash_profile
if [ $installed_rc -lt 1 ]; then
ln -s "$mydir/bash_profile" .bash_profile
installed_rc=1
fi
fi
# default to .bashrc link
if [ $installed_rc -lt 1 ]; then
ln -s "$mydir/bash_profile" .bashrc
installed_rc=1
fi
if [ -a .env-setup ]; then
backup_file .env-setup
fi
envroot_extra=${ENV_ROOT_EXTRA:-$h/.env.d}
mkdir -p "$envroot_extra" 2>/dev/null
cat > .env-setup <<ENDENVSETUP
export ENV_ROOT=$mydir
export ENV_ROOT_EXTRA=${envroot_extra}
ENDENVSETUP
popd 2>&1 >/dev/null
echo "Success!"
if [ -d "$backup_dir" ]; then
echo "Your old files are backed up in: $backup_dir:"
echo -e $files_backedup
echo ""
fi
echo "You can now add files to '$envroot_extra'"
echo "and they will be automagically sourced into"
echo "your environment for you at login."
echo ""
echo "You can also force a re-evaluation by:"
if [ -a .bashrc ]; then
echo " source $h/.bashrc"
else
echo " source $h/.bash_profile"
fi
echo ""
echo "Enjoy!"
echo ""