-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·176 lines (164 loc) · 5.54 KB
/
setup.sh
File metadata and controls
executable file
·176 lines (164 loc) · 5.54 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
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
#!/usr/bin/env bash
# Colors
RESET="\033[0m"
UNDERLINE="\033[4m"
UNDERLINEOFF="\033[24m"
BOLD="\033[1m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
WHITE="\033[37m"
if [[ -z "${BASH_VERSION}" ]]; then
echo -e "${BOLD}${RED}ERROR: ${WHITE}Please run this script using ${GREEN}bash${WHITE}, not ${YELLOW}sh or other shells${WHITE}.${RESET}" >&2
exit 1
fi
# Get system information
OS_NAME=""
if [[ "$(uname -s)" == "Darwin" ]]; then
OS_NAME="macOS"
PACKAGE_MANAGER="Homebrew"
elif [[ "$(uname -s)" == "Linux" ]]; then
if grep -qiE 'ID.*ubuntu' /etc/*-release; then
OS_NAME="Ubuntu"
PACKAGE_MANAGER="APT, Homebrew"
elif grep -qiE 'ID.*debian' /etc/*-release; then
OS_NAME="Debian"
PACKAGE_MANAGER="APT, Homebrew"
elif grep -qiE 'ID.*fedora' /etc/*-release; then
OS_NAME="Fedora"
PACKAGE_MANAGER="DNF, Homebrew"
elif grep -qiE 'ID.*manjaro' /etc/*-release; then
OS_NAME="Manjaro"
PACKAGE_MANAGER="Pacman, Homebrew"
fi
fi
if [[ -z "${OS_NAME}" ]]; then
echo -e "${BOLD}${RED}The operating system is not supported yet. ${YELLOW}Only macOS, Debian/Ubuntu Linux, Fedora Linux, and Manjaro Linux are supported.${RESET}" >&2
exit 1
fi
echo -e "${BOLD}${WHITE}Operating System: ${GREEN}${OS_NAME}${RESET}" >&2
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
IS_LOCAL_SCRIPT=''
if [[ -x "${SCRIPT_DIR}/setup_${OS_NAME}.sh" && "$(basename "$0")" == "setup.sh" ]]; then
IS_LOCAL_SCRIPT=true
fi
function print_help() {
(
echo -e "${BOLD}${WHITE}Usage: ${GREEN}/bin/bash setup.sh [options]${RESET}"
echo
echo -e "${BOLD}${WHITE}Options:${RESET}"
echo -e " ${GREEN}--set-mirrors [yes|no]${RESET} Set or not set the source of package managers to the open source mirrors at TUNA (@China)."
echo -e " ${GREEN}--no-set-mirrors${RESET} Do not set the source of package managers to the open source mirrors at TUNA (@China)."
echo
echo -e " ${GREEN}--force-per-user-homebrew${RESET} Force to install Homebrew in per-user mode even if running as sudoers (Linux only)."
echo
echo -e " ${GREEN}--non-interactive${RESET} Run the script in non-interactive mode (Linux only)."
echo
echo -e " ${GREEN}--help, -h${RESET} Show this help message and exit."
echo
echo -e "${BOLD}${WHITE}If no option is provided for setting mirrors, the script will prompt for confirmation.${RESET}"
) >&2
}
while (("$#" > 0)); do
option="$1"
case "${option}" in
--set-mirrors)
shift
if [[ -z "${1:-}" ]] || [[ "${1:-}" == --* ]]; then
SET_MIRRORS=true
else
SET_MIRRORS="${1:-}"
shift
fi
;;
--no-set-mirrors)
SET_MIRRORS=false
shift
;;
--set-mirrors=*)
SET_MIRRORS="${1#*=}"
shift
;;
--force-per-user-homebrew)
export FORCE_PER_USER_HOMEBREW=true
shift
;;
--non-interactive)
export NONINTERACTIVE=true
shift
;;
--help | -h)
echo
print_help
exit 0
;;
*)
echo -e "${BOLD}${RED}Warning: Unknown option: '${option}'.${RESET}" >&2
shift
;;
esac
done
# Options
if [[ "${SET_MIRRORS}" =~ (1|yes|Yes|YES|true|True|TRUE|on|On|ON) ]]; then
SET_MIRRORS=true
elif [[ "${SET_MIRRORS}" =~ (0|no|No|NO|false|False|FALSE|off|Off|OFF) ]]; then
SET_MIRRORS=false
else
if [[ -n "${SET_MIRRORS}" ]]; then
echo -e "${BOLD}${YELLOW}Warning: Unknown value for SET_MIRRORS: '${SET_MIRRORS}'.${RESET}" >&2
fi
unset SET_MIRRORS
if [[ -t 0 ]] && [[ -t 1 ]]; then
while true; do
read -n 1 -p "$(
echo -e "${BOLD}${WHITE}Do you wish to set the source of package managers ${GREEN}(${PACKAGE_MANAGER}, CPAN, Gem, Conda, and Pip)${WHITE}"
echo -e "to the open source mirrors at ${YELLOW}TUNA (@China) (${UNDERLINE}https://mirrors.tuna.tsinghua.edu.cn${UNDERLINEOFF})${WHITE} [y/N]: ${RESET}"
)" answer
if [[ -n "${answer}" ]]; then
echo
else
answer="n"
fi
if [[ "${answer}" == [Yy] ]]; then
SET_MIRRORS=true
break
elif [[ "${answer}" == [Nn] ]]; then
SET_MIRRORS=false
break
fi
done
fi
fi
export SET_MIRRORS
# Start logging
export DEV_SETUP_BACKUP_DIR="${HOME}/.dotfiles/backups/$(date +"%Y-%m-%d-%T")"
mkdir -p "${DEV_SETUP_BACKUP_DIR}/.dotfiles"
chmod 755 "${HOME}/.dotfiles"
LOG_FILE="${DEV_SETUP_BACKUP_DIR}/dev-setup.log"
exec 2> >(tee -a "${LOG_FILE}" >&2)
echo -e "${BOLD}${WHITE}The script output will be logged to file ${YELLOW}\"${LOG_FILE}\"${WHITE}.${RESET}" >&2
# Run script if it exists
if [[ -n "${IS_LOCAL_SCRIPT}" ]]; then
echo -e "${BOLD}${WHITE}Run existing script ${GREEN}\"${SCRIPT_DIR}/setup_${OS_NAME}.sh\"${WHITE}.${RESET}" >&2
echo
exec /bin/bash "${SCRIPT_DIR}/setup_${OS_NAME}.sh"
fi
# Download and run
if [[ -x "$(command -v wget)" ]]; then
echo -e "${BOLD}${WHITE}Download and run script via ${GREEN}wget${WHITE}.${RESET}" >&2
echo
wget --progress=bar:force:noscroll -O "${DEV_SETUP_BACKUP_DIR}/setup_${OS_NAME}.sh" "https://github.com/XuehaiPan/Dev-Setup/raw/HEAD/setup_${OS_NAME}.sh"
exec /bin/bash "${DEV_SETUP_BACKUP_DIR}/setup_${OS_NAME}.sh"
elif [[ -x "$(command -v curl)" ]]; then
echo -e "${BOLD}${WHITE}Download and run script via ${GREEN}curl${WHITE}.${RESET}" >&2
echo
curl -fL# -o "${DEV_SETUP_BACKUP_DIR}/setup_${OS_NAME}.sh" "https://github.com/XuehaiPan/Dev-Setup/raw/HEAD/setup_${OS_NAME}.sh"
exec /bin/bash "${DEV_SETUP_BACKUP_DIR}/setup_${OS_NAME}.sh"
elif [[ -x "$(command -v git)" ]]; then
echo -e "${BOLD}${WHITE}Download and run script via ${GREEN}git${WHITE}.${RESET}" >&2
echo
git clone --depth=1 https://github.com/XuehaiPan/Dev-Setup.git 2>&1
exec /bin/bash "Dev-Setup/setup_${OS_NAME}.sh"
else
echo -e "${BOLD}${WHITE}Please download the script from ${YELLOW}https://github.com/XuehaiPan/Dev-Setup${WHITE} manually.${RESET}" >&2
fi