-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·200 lines (173 loc) · 5.44 KB
/
install.sh
File metadata and controls
executable file
·200 lines (173 loc) · 5.44 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env bash
set_colors() {
# Initialize colors (if available)
if tput setaf 1 &>/dev/null; then
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
NC=$(tput sgr0) # Reset attributes
TPUT_AVAILABLE=true
else
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
TPUT_AVAILABLE=false
fi
}
ensure_dir() {
if [[ -e "${SYMLINK_DIR}" ]]; then
if [[ ! -d "${SYMLINK_DIR}" ]]; then
echo "${RED}Error: ${SYMLINK_DIR} exists but is not a directory. Please remove it or choose a different path.${NC}"
exit 1
fi
return
fi
if [[ ! -d ${SYMLINK_DIR} ]]; then
echo "${YELLOW}$SYMLINK_DIR does not exist and will be created.${NC}"
mkdir -p "${SYMLINK_DIR}" 2>/dev/null
local success=$?
if [[ $success != 0 ]]; then
echo -e -n "${RED}Could not create $SYMLINK_DIR. Exit status: ${success}.${NC} Try again with sudo? (Y/n) "
read -r answer
if [ "$answer" != "${answer#[Nn]}" ]; then
exit 1
fi
sudo mkdir -p "${SYMLINK_DIR}" 2>/dev/null
success=$?
if [[ -n $success ]]; then
echo -e -n "${RED}Could not create $SYMLINK_DIR. Exit status: ${success}.${NC}"
exit ${success}
fi
fi
fi
}
INSTALL_DIR="${HOME}/.phpctl"
SYMLINK_DIR="/usr/local/bin"
LOCAL_SOURCES_DIR=""
# --- Option Parsing ---
while getopts "hi:s:l:" opt; do
case $opt in
h)
echo "Install phpctl sources and creates symlinks for easy usage."
echo ""
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -h Display this help message and exit"
echo " -i <directory> Set the installation directory (default: $HOME/.phpctl)"
echo " -s <directory> Set the symlink directory (default: /usr/local/bin)"
echo " -l <directory> Set the local sources directory. If empty, will fetch sources from github. (default: empty string)"
exit 0
;;
i)
INSTALL_DIR="$OPTARG"
;;
s)
SYMLINK_DIR="$OPTARG"
;;
l)
LOCAL_SOURCES_DIR="$OPTARG"
;;
\?)
echo "Error: Invalid option: -$OPTARG" >&2
echo "Try '$0 -h' for more information." >&2
exit 1
;;
:)
echo "Error: Option -$OPTARG requires an argument." >&2
echo "Try '$0 -h' for more information." >&2
exit 1
;;
esac
done
set_colors
# Shift off the options and their arguments, so that any remaining
# positional parameters (if any) are correctly handled.
shift $((OPTIND - 1))
# Compatibility with previous script version
if [[ $SYMLINK_DIR = "/usr/local/bin" && -n ${1} ]]; then
SYMLINK_DIR=$1
fi
ensure_dir
if [[ ! -w "${SYMLINK_DIR}" ]]; then
SUDO="sudo"
else
SUDO=""
fi
if [[ -n $SUDO ]]; then
echo "Running in elevated mode. This might require sudo for operations in ${SYMLINK_DIR}."
fi
spinner() {
local i=0
local sp="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
while true; do
printf "\r[%s] Cloning repository..." "${sp:i++%${#sp}:1}"
sleep 0.1
done
}
cleanup() {
if [[ -n "$SPINNER_PID" ]]; then
kill "$SPINNER_PID" 2>/dev/null
if $TPUT_AVAILABLE; then
printf "\r%*s\r" "$(tput cols)" ""
else
printf "\r\033[2K"
fi
fi
exit
}
install_sources() {
echo -e "${YELLOW}Installing phpctl at ${NC}$INSTALL_DIR"
if [ -d "$INSTALL_DIR" ]; then
echo "The install directory is not empty. Attempting to remove it..."
rm -rf "$INSTALL_DIR"
fi
if [[ -n "$LOCAL_SOURCES_DIR" ]]; then
echo "Using local sources from: $LOCAL_SOURCES_DIR"
cp -r "$LOCAL_SOURCES_DIR" "$INSTALL_DIR"
else
GITHUB_REPO="https://github.com/opencodeco/phpctl.git"
trap cleanup EXIT INT TERM
spinner &
SPINNER_PID=$!
git clone --quiet "$GITHUB_REPO" "$INSTALL_DIR" &
PID=$!
wait "$PID"
_git_status=$?
kill "$SPINNER_PID" 2>/dev/null
# clear the spinner line one last time
if $TPUT_AVAILABLE; then
printf "\r%*s\r" "$(tput cols)" ""
else
printf "\r\033[2K"
fi
if [[ $_git_status -eq 0 ]]; then
echo "done."
else
echo "Failed to clone $GITHUB_REPO into $INSTALL_DIR."
echo "Error: git clone failed with status ${_git_status}"
exit 1
fi
fi
echo "${GREEN}Success: ${NC}Operation completed successfully."
}
install_sources
echo -n "Files will be symlinked to ${SYMLINK_DIR}."
if [[ -n $SUDO ]]; then
echo -n " ${RED}Sudo will be prompted to symlink the phpctl files.${NC}"
fi
echo -e -n " ${GREEN}Do you want to continue? (Y/n)${NC} "
read -r answer
if [ "$answer" != "${answer#[Nn]}" ]; then
echo -e "${RED}To use phpctl globally, link the cloned script to your bin directory, like:${NC}"
for file in "${INSTALL_DIR}"/bin/*; do
bin=$(basename "$file")
echo " ${SUDO} ln -sf ${INSTALL_DIR}/bin/$bin ${SYMLINK_DIR}/$bin"
done
echo -e "${YELLOW}Or add ${INSTALL_DIR}/bin to your PATH."
else
$SUDO "${INSTALL_DIR}/scripts/symlink-bins.sh" "${INSTALL_DIR}" "${SYMLINK_DIR}"
fi
echo "${GREEN}Installation complete!${NC}"