-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·179 lines (158 loc) · 5.83 KB
/
install.sh
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
#!/bin/sh
###################################################################################################
# File: install.sh
# Author: [Vatsal Gupta (gvatsal60)]
# Date: 12-Jul-2024
# Description: Setup of custom aliases on a Linux system by downloading a
# predefined .aliases.sh file from a GitHub repository and
# integrating it into the user's shell configuration
# files (~/.bashrc, ~/.zshrc, or ~/.profile)
###################################################################################################
###################################################################################################
# License
###################################################################################################
# This script is licensed under the Apache 2.0 License.
###################################################################################################
# Global Variables & Constants
###################################################################################################
# Exit the script immediately if any command fails
set -e
readonly FILE_NAME=".aliases.sh"
readonly FILE_PATH="${HOME}/${FILE_NAME}"
readonly ALIAS_SRC_URL="https://raw.githubusercontent.com/gvatsal60/Linux-Aliases/HEAD/${FILE_NAME}"
readonly ALIAS_SEARCH_STR="\. \"${FILE_PATH}\""
ALIAS_SOURCE_STR=$(
cat <<EOF
# Common and useful aliases
if [ -f "${FILE_PATH}" ]; then
. "${FILE_PATH}"
fi
EOF
)
###################################################################################################
# Functions
###################################################################################################
# Function: println
# Description: Prints a message to the console, followed by a newline.
# Usage: println "Your message here"
println() {
printf "%s\n" "$*" 2>/dev/null
}
# Function: print_err
# Description: Prints an error message to the console, followed by a newline.
# Usage: print_err "Your error message here"
print_err() {
printf "%s\n" "$*" >&2
}
# Function: update_rc
# Description: Update shell configuration files
update_rc() {
_rc=""
case ${ADJUSTED_ID} in
debian | rhel)
_rc="${HOME}/.bashrc"
;;
alpine | arch)
_rc="${HOME}/.profile"
;;
darwin)
_rc="${HOME}/.zshrc"
;;
*)
print_err "Error: Unsupported or unrecognized OS distribution ${ADJUSTED_ID}"
exit 1
;;
esac
# Check if ".aliases.sh" is already sourced, if not then append it
if [ -f "${_rc}" ]; then
if ! grep -qsE "${ALIAS_SEARCH_STR}" "${_rc}"; then
println "=> Updating ${_rc} for ${ADJUSTED_ID}..."
# Append the sourcing block to the RC file
println "${ALIAS_SOURCE_STR}" >>"${_rc}"
fi
else
# Notify if the rc file does not exist
println "=> Profile not found. ${_rc} does not exist."
println "=> Creating the file ${_rc}... Please note that this may not work as expected."
# Create the rc file
touch "${_rc}"
# Append the sourcing block to the newly created rc file
println "${ALIAS_SOURCE_STR}" >>"${_rc}"
fi
println ""
println "=> Close and reopen your terminal to start using aliases"
println " OR"
println "=> Run the following to use it now:"
println ">>> source ${_rc} # This loads aliases"
}
# Function: dw_file
# Description: Download file using wget or curl if available
dw_file() {
# Check if curl is available
if command -v curl >/dev/null 2>&1; then
curl -fsSL -o "${FILE_PATH}" ${ALIAS_SRC_URL}
# Check if wget is available
elif command -v wget >/dev/null 2>&1; then
wget -O "${FILE_PATH}" ${ALIAS_SRC_URL}
else
print_err "Error: Either install wget or curl"
exit 1
fi
}
###################################################################################################
# Main Script
###################################################################################################
OS=$(uname)
case ${OS} in
Darwin)
ADJUSTED_ID="darwin"
;;
Linux)
# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME
# shellcheck source=/dev/null
. /etc/os-release
# Get an adjusted ID independent of distro variants
if [ "${ID}" = "debian" ] || [ "${ID_LIKE#*debian}" != "${ID_LIKE}" ]; then
ADJUSTED_ID="debian"
elif [ "${ID}" = "arch" ] || [ "${ID_LIKE#*arch}" != "${ID_LIKE}" ]; then
ADJUSTED_ID="arch"
elif [ "${ID}" = "rhel" ] || [ "${ID}" = "fedora" ] || [ "${ID}" = "mariner" ] || [ "${ID_LIKE#*rhel}" != "${ID_LIKE}" ] || [ "${ID_LIKE#*fedora}" != "${ID_LIKE}" ] || [ "${ID_LIKE#*mariner}" != "${ID_LIKE}" ]; then
ADJUSTED_ID="rhel"
elif [ "${ID}" = "alpine" ]; then
ADJUSTED_ID="alpine"
else
print_err "Error: Linux distro ${ID} not supported."
exit 1
fi
;;
*)
print_err "Error: Unsupported or unrecognized OS distribution ${ADJUSTED_ID}"
exit 1
;;
esac
# Default behavior
_action="y"
# Check if the script is running in interactive mode, for non-interactive mode `_action` defaults to 'y'
if [ -t 0 ]; then
# Interactive mode
if [ -f "${FILE_PATH}" ]; then
println "=> File already exists: ${FILE_PATH}"
println "=> Do you want to replace it (default: y)? [y/n]: "
# Read input, use default value if no input is given
read -r _rp_conf
_rp_conf="${_rp_conf:-${_action}}"
_action="${_rp_conf}"
fi
fi
if [ "${_action}" = "y" ]; then
println "=> Updating the file: ${FILE_PATH}"
# Download the necessary file from the specified source
dw_file
# Update the configuration file with the latest changes
update_rc
elif [ "${_action}" = "n" ]; then
println "=> Keeping existing file: ${FILE_PATH}"
else
print_err "Error: Invalid input. Please check your entry and try again."
exit 1
fi