-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-helpful-shell-scripts
More file actions
executable file
·76 lines (66 loc) · 2.35 KB
/
install-helpful-shell-scripts
File metadata and controls
executable file
·76 lines (66 loc) · 2.35 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
#!/usr/bin/zsh
# NOTE TO SELF: I would like the arrow printouts to be lined up; maybe write a function to calculate the necessary white space needed to do that?
# NOTE TO SELF: While I'm at it, why not add a flag for dry runs; it would only be useful for testing the script but hey, why not?
THIS_NAME="${0##*/}"
WARN_MSG_PERMISSIONS="[$THIS_NAME][WARNING] You need special permissions to write to this directory! Trying sudo...\n"
ERR_MSG_SPECIFY_LOCATION="Please specify the install location.\n"
ERR_MSG_INVALID_LOCATION="Install location does not exist!\n"
ERR_MSG_INSTALL_SCRIPT_MOVED="The install script has been moved! Please put it back in the Github repository directory.\n"
INSTALL_FILE_REALPATH="$(realpath $0)"
SCRIPTS_LOCATION="${INSTALL_FILE_REALPATH%/*}"
INSTALL_LOCATION="$1"
TEST_FILE="$INSTALL_LOCATION/.test"
LONGEST_NAME=""
FILE_COLOR="\x1b[1;36m"
ARROWS_COLOR="\x1b[1;39m"
LINK_COLOR="\x1b[1;32m"
RESET="\x1b[0m"
throw_error()
{
printf "[$THIS_NAME][ERROR] $1" >&2
exit 1
}
equal_lengths()
{
for file in $SCRIPTS_LOCATION/*; do
if [[ ! "$file" =~ "install-helpful-shell-scripts" && ! "$file" =~ "README.md" ]]; then
if [[ ${#file} -gt ${#LONGEST_NAME} ]]; then
LONGEST_NAME="$file"
fi
fi
done
}
install()
{
if [[ ! "$INSTALL_FILE_REALPATH" =~ "HelpfulShellScripts" ]]; then
throw_error "$ERR_MSG_INSTALL_SCRIPT_MOVED"
fi
equal_lengths
for file in "$SCRIPTS_LOCATION"/*; do
if [[ ! "$file" =~ "install-helpful-shell-scripts" && ! "$file" =~ "README.md" ]]; then
LINKED_FILE="${INSTALL_LOCATION%/}/${file##*/}"
NUMBER_OF_SPACES=$((${#LONGEST_NAME} - ${#file}))
SPACES=""
for ((i = 0 ; i < $NUMBER_OF_SPACES ; i++)); do
SPACES="$SPACES "
done
printf "Creating static link: $FILE_COLOR$file$SPACES$ARROWS_COLOR -> $LINK_COLOR$LINKED_FILE$RESET\n"
rm -f "$LINKED_FILE" &>/dev/null
ln -s "$file" "$LINKED_FILE"
fi
done
}
if [[ -z "$INSTALL_LOCATION" ]]; then
throw_error "$ERR_MSG_SPECIFY_LOCATION"
fi
if [[ ! -e "$INSTALL_LOCATION" ]]; then
throw_error "$ERR_MSG_INVALID_LOCATION"
fi
if [[ $(touch "$TEST_FILE" &>/dev/null ; echo "$?") -eq 1 ]]; then
printf "$WARN_MSG_PERMISSIONS"
sudo install
exit 0
else
rm "$TEST_FILE"
fi
install