Skip to content

Commit e1331a9

Browse files
committed
system wide install if run as root
1 parent d30becf commit e1331a9

File tree

1 file changed

+82
-66
lines changed

1 file changed

+82
-66
lines changed

scripts/install.sh

Lines changed: 82 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,28 @@ case "$filename" in
3737
;;
3838
esac
3939

40-
INSTALL_DIR=$HOME/.feature-installer/bin
40+
print_message() {
41+
local level=$1
42+
local message=$2
43+
local color=""
44+
45+
case $level in
46+
info) color="${GREEN}" ;;
47+
warning) color="${YELLOW}" ;;
48+
error) color="${RED}" ;;
49+
esac
50+
51+
echo -e "${color}${message}${NC}"
52+
}
53+
54+
55+
# Set installation directory based on user privileges
56+
if [[ $EUID -eq 0 ]]; then
57+
INSTALL_DIR="/usr/local/bin"
58+
print_message info "Running as root - installing to system-wide location: $INSTALL_DIR"
59+
else
60+
INSTALL_DIR="$HOME/.feature-installer/bin"
61+
fi
4162
mkdir -p "$INSTALL_DIR"
4263

4364
if [ -z "$requested_version" ]; then
@@ -53,19 +74,6 @@ else
5374
specific_version=$requested_version
5475
fi
5576

56-
print_message() {
57-
local level=$1
58-
local message=$2
59-
local color=""
60-
61-
case $level in
62-
info) color="${GREEN}" ;;
63-
warning) color="${YELLOW}" ;;
64-
error) color="${RED}" ;;
65-
esac
66-
67-
echo -e "${color}${message}${NC}"
68-
}
6977

7078
check_version() {
7179
if command -v feature-installer >/dev/null 2>&1; then
@@ -98,7 +106,6 @@ download_and_install() {
98106
check_version
99107
download_and_install
100108

101-
102109
add_to_path() {
103110
local config_file=$1
104111
local command=$2
@@ -115,66 +122,69 @@ add_to_path() {
115122
fi
116123
}
117124

118-
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
119-
120-
current_shell=$(basename "$SHELL")
121-
case $current_shell in
122-
fish)
123-
config_files="$HOME/.config/fish/config.fish"
124-
;;
125-
zsh)
126-
config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
127-
;;
128-
bash)
129-
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
130-
;;
131-
ash)
132-
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
133-
;;
134-
sh)
135-
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
136-
;;
137-
*)
138-
# Default case if none of the above matches
139-
config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
140-
;;
141-
esac
125+
# Configure PATH in shell config files (skip for root installations)
126+
if [[ $EUID -ne 0 ]]; then
127+
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
142128

143-
config_file=""
144-
for file in $config_files; do
145-
if [[ -f $file ]]; then
146-
config_file=$file
147-
break
148-
fi
149-
done
150-
151-
if [[ -z $config_file ]]; then
152-
print_message error "No config file found for $current_shell. Checked files: ${config_files[@]}"
153-
exit 1
154-
fi
155-
156-
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
129+
current_shell=$(basename "$SHELL")
157130
case $current_shell in
158131
fish)
159-
add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
132+
config_files="$HOME/.config/fish/config.fish"
160133
;;
161134
zsh)
162-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
135+
config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
163136
;;
164137
bash)
165-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
138+
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
166139
;;
167140
ash)
168-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
141+
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
169142
;;
170143
sh)
171-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
144+
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
172145
;;
173146
*)
174-
print_message warning "Manually add the directory to $config_file (or similar):"
175-
print_message info " export PATH=$INSTALL_DIR:\$PATH"
147+
# Default case if none of the above matches
148+
config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
176149
;;
177150
esac
151+
152+
config_file=""
153+
for file in $config_files; do
154+
if [[ -f $file ]]; then
155+
config_file=$file
156+
break
157+
fi
158+
done
159+
160+
if [[ -z $config_file ]]; then
161+
print_message error "No config file found for $current_shell. Checked files: ${config_files[@]}"
162+
exit 1
163+
fi
164+
165+
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
166+
case $current_shell in
167+
fish)
168+
add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
169+
;;
170+
zsh)
171+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
172+
;;
173+
bash)
174+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
175+
;;
176+
ash)
177+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
178+
;;
179+
sh)
180+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
181+
;;
182+
*)
183+
print_message warning "Manually add the directory to $config_file (or similar):"
184+
print_message info " export PATH=$INSTALL_DIR:\$PATH"
185+
;;
186+
esac
187+
fi
178188
fi
179189

180190
if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
@@ -186,9 +196,15 @@ fi
186196
print_message info ""
187197
print_message info "Installation completed successfully!"
188198
print_message info ""
189-
print_message info "To use ${ORANGE}feature-installer${GREEN} immediately, run one of the following:"
190-
print_message info " 1. ${YELLOW}source ~/.bashrc${GREEN} (or your shell's config file)"
191-
print_message info " 2. ${YELLOW}export PATH=$INSTALL_DIR:\$PATH${GREEN}"
192-
print_message info " 3. ${YELLOW}$INSTALL_DIR/feature-installer${GREEN} (use full path)"
193-
print_message info ""
194-
print_message info "Or simply start a new terminal session."
199+
200+
if [[ $EUID -eq 0 ]]; then
201+
print_message info "${ORANGE}feature-installer${GREEN} has been installed system-wide to ${YELLOW}$INSTALL_DIR${GREEN}"
202+
print_message info "It should be immediately available in the PATH for all users."
203+
else
204+
print_message info "To use ${ORANGE}feature-installer${GREEN} immediately, run one of the following:"
205+
print_message info " 1. ${YELLOW}source ~/.bashrc${GREEN} (or your shell's config file)"
206+
print_message info " 2. ${YELLOW}export PATH=$INSTALL_DIR:\$PATH${GREEN}"
207+
print_message info " 3. ${YELLOW}$INSTALL_DIR/feature-installer${GREEN} (use full path)"
208+
print_message info ""
209+
print_message info "Or simply start a new terminal session."
210+
fi

0 commit comments

Comments
 (0)