-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsetup
executable file
·185 lines (166 loc) · 6.09 KB
/
setup
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash
#*****************************************************************
# Copyright (c) 2013 The 6Th Column Project
# All rights reserved.
#*****************************************************************
#
# Organization: A Couple of Guys That Need A Life
# Directorate: Computation
# Department: Computing Applications and Research
# Program: Tom Foolery
# Project: BASH-FOO
# First Authors: Gavin M. Bell ([email protected]) &
# Charles Doutriaux ([email protected])
#
# Description:
# A clean and easy way to setup your bash environment that is hassle free.
# We use lots of different machines... it makes it less painful
#
# Sets up dot bash files in your home directory
#
#*****************************************************************
#TODO - implement revert_homedir function
#TODO - write arg code to trigger revert if necessary
DEBUG=0
DRY_RUN=0
FORCE=0
homedir=${homedir:-${HOME:?"HOME environment var must be set!!!!"}}
programs_to_check_for=(fortune cowsay uname uptime)
quick_checks() {
local oops_count=0
for program in ${programs_to_check_for[@]}; do
${program} >& /dev/null
if [ $? != 0 ]; then
((oops_count++))
echo "Couldn't find [${program}]"
fi
done
if ((oops_count > 0)); then
echo "Warning: You may want to install the missing programs for optimum use..."
if uname -a | grep -q Darwin; then
echo " MacPorts: https://www.macports.org/install.php"
echo "> port install <program>"
else
echo " Please install the missing program(s) for optimum use (yum, apt-get, rpm, etc..)"
fi
fi
}
# Utility "private" method
_readlinkf() {
# This is a portable implementation of GNU's "readlink -f" in
# bash/zsh, following symlinks recursively until they end in a
# file, and will print the full dereferenced path of the specified
# file even if the file isn't a symlink.
#
# Loop detection exists, but only as an abort after passing a
# maximum length.
local start_dir=$(pwd)
local file=${1}
cd $(dirname ${file})
file=$(basename ${file})
# Iterate down symlinks. If we exceed a maximum number symlinks, assume that
# we're looped and die horribly.
local maxlinks=20
local count=0
local current_dir
while [ -L "${file}" ] ; do
file=$(readlink ${file})
cd $(dirname ${file})
file=$(basename ${file})
((count++))
if (( count > maxlinks )) ; then
current_dir=$(pwd -P)
echo "CRITICAL FAILURE[4]: symlink loop detected on ${current_dir}/${file}"
cd ${start_dir}
return ${count}
fi
done
current_dir=$(pwd -P)
echo "${current_dir}/${file}"
cd ${start_dir}
}
setup_dot_bash_files() {
[ ! -e .git ] && echo "Sorry this is not a git repo" && exit 1
local mod_count=0
for dot_file in $(ls -a | grep bash); do
echo -n "setting up $dot_file... "
#exclusion for the .bash_profile file - only link if not already present in ${homedir}
if [ "${dot_file}" = "bash_profile" ] && [ -e ${homedir}/.${dot_file} ]; then
((DEBUG)) && echo "Already have a .bash_profile present, will not alter" || echo "[OK] <skipping>"
continue
fi
#check if the file exists already
if [ "$(_readlinkf ${dot_file})" = "$(_readlinkf ${homedir}/.${dot_file} | tail -n 1)" ]; then
echo "[OK] (nothing to do already linked)"
else
((DEBUG|DRY_RUN)) && echo "[ -e ${homedir}/.${dot_file} ] && mv -v ${homedir}/.${dot_file}{,.bak}"
((! DRY_RUN)) && [ -e ${homedir}/.${dot_file} ] && mv -v ${homedir}/.${dot_file}{,.bak}
((DEBUG||DRY_RUN)) && echo "ln -s $(_readlinkf ${dot_file}) ${homedir}/.${dot_file}"
if ((! DRY_RUN)); then
((FORCE & DEBUG)) && echo "[ -L "${homedir}/.${dot_file}" ] && [ "$(_readlinkf ${homedir}/.${dot_file})" != "$(_readlinkf ${dot_file})" ] && unlink ${homedir}/.${dot_file}"
((FORCE)) && [ -L "${homedir}/.${dot_file}" ] && [ "$(_readlinkf ${homedir}/.${dot_file})" != "$(_readlinkf ${dot_file})" ] && unlink ${homedir}/.${dot_file}
ln -s $(_readlinkf ${dot_file}) ${homedir}/.${dot_file}
if [ $? != 0 ]; then echo "ERROR: problem linking ${dot_file}"; else echo " linked file... [OK] " && ((++mod_count)); fi
fi
fi
done
# A succesful run (returning 0) is when some work or change in state has occured from the installation
# If no work had to be done then it returns 1
# This allows for the capability to condition subsequent action based on the behavior of this function.
((mod_count > 0)) && return 0 || return 1
}
revert_homedir() {
[ ! -e .git ] && echo "Sorry this is not a git repo" && exit 1
echo " Revert Not Yet Implemented"
return 0
}
_usage() {
printf "
Usage:
> setup
--install - Runs the installation process [default]
--revert - Uninstalls the system [not yet implemented]
--help|-h - Prints this help message
--debug - Runs in debug mode (more output)
--dry-run - Will not perform operations but will print what would be done
"
exit 0
}
main() {
while [ -n "$1" ]; do
case "$1" in
--install)
shift
INSTALL=1
;;
--revert)
shift
REVERT=1
;;
--help|-h)
_usage
;;
--debug)
shift
DEBUG=1
;;
--force)
shift
FORCE=1
;;
--dry-run)
shift
DRY_RUN=1
;;
*)
echo "Unknown option $1"
exit 1
esac
done
((DEBUG)) && echo "Debug mode on"
((DRY_RUN)) && echo "Dry run mode on"
if ((REVERT)); then revert_homedir; exit $?; fi
#Functions that peform the installtion....
quick_checks && setup_dot_bash_files $@ && echo "Don't forget to run: source ${homedir}/.bashrc" && source ${homedir}/.bashrc
}
main $@