Skip to content

Commit b8d2305

Browse files
authored
Update uninstall.sh
1 parent b9cf0e2 commit b8d2305

File tree

1 file changed

+108
-15
lines changed

1 file changed

+108
-15
lines changed

custom-http-server/uninstall.sh

Lines changed: 108 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,117 @@
11
#!/bin/bash
2+
set -e
23

3-
echo "Stopping custom-http-server service..."
4-
sudo systemctl stop custom-http-server
4+
# Default values
5+
DEFAULT_TARGET_DIR="/usr/local/custom_http_server"
6+
DEFAULT_CONFIG_PATH="/usr/local/etc/custom-http-server.conf"
7+
DEFAULT_PORT=""
8+
DEFAULT_PATH=""
59

6-
echo "Disabling custom-http-server service..."
7-
sudo systemctl disable custom-http-server
10+
# Parse macOS arguments
11+
OS_NAME="$(uname)"
12+
if [[ "$OS_NAME" == "Darwin" ]]; then
13+
while [[ $# -gt 0 ]]; do
14+
case "$1" in
15+
-path)
16+
DEFAULT_PATH="$2"
17+
shift 2
18+
;;
19+
-port)
20+
DEFAULT_PORT="$2"
21+
shift 2
22+
;;
823

9-
echo "Killing any remaining custom-http-server Python processes..."
10-
sudo pkill -f "/opt/custom-http-server/custom_http_server.py"
24+
*)
25+
echo "❌ Unknown argument: $1"
26+
exit 1
27+
;;
28+
esac
29+
done
1130

12-
echo "Removing systemd service file..."
13-
sudo rm -f /etc/systemd/system/custom-http-server.service
31+
if [[ -z "$DEFAULT_PATH" && -z "$DEFAULT_PORT" ]]; then
32+
echo "❌ Error: At least one of -path or -port must be specified."
33+
exit 1
34+
fi
1435

15-
echo "Removing config file..."
16-
sudo rm -f /etc/custom-http-server.conf
36+
fi
1737

18-
echo "Removing installed files from /opt/custom-http-server..."
19-
sudo rm -rf /opt/custom-http-server
38+
# Set platform-specific values
39+
TARGET_DIR="/opt/custom_http_server"
40+
CONFIG_PATH="/etc/custom-http-server.conf"
41+
SERVICE_PATH="/etc/systemd/system/custom-http-server.service"
2042

21-
echo "Reloading systemd daemon..."
22-
sudo systemctl daemon-reload
43+
if [[ "$OS_NAME" == "Darwin" ]]; then
44+
TARGET_DIR="$DEFAULT_TARGET_DIR"
45+
CONFIG_PATH="$DEFAULT_CONFIG_PATH"
46+
fi
2347

24-
echo "Uninstall complete."
48+
echo "Installing Custom HTTP Server..."
49+
50+
# Create target and config directories
51+
sudo mkdir -p "$TARGET_DIR"
52+
sudo mkdir -p "$(dirname "$CONFIG_PATH")"
53+
54+
# Copy application and config files
55+
sudo cp custom_http_server.py "$TARGET_DIR/"
56+
sudo cp default-config.conf "$CONFIG_PATH"
57+
58+
if [[ "$OS_NAME" == "Linux" ]]; then
59+
echo "✅ Installing on Linux."
60+
sudo cp custom-http-server.service "$SERVICE_PATH"
61+
sudo systemctl daemon-reload
62+
sudo systemctl enable custom-http-server
63+
sudo systemctl start custom-http-server
64+
65+
echo "✅ Installed and started custom-http-server as a systemd service."
66+
echo "Edit $CONFIG_PATH to change path or port."
67+
echo "Logs: sudo journalctl -u custom-http-server -f"
68+
69+
elif [[ "$OS_NAME" == "Darwin" ]]; then
70+
echo "✅ Installing on macOS."
71+
echo "Note: macOS does not support automatic install/uninstall like Linux (systemd)."
72+
echo "⚠️ To run manually:"
73+
echo " python3 $TARGET_DIR/custom_http_server.py --path /Users/<your-username> --port <your-port>"
74+
echo "Replace <your-username> with your actual macOS username."
75+
echo "Choose a port that's not already in use (e.g., 8080, 8888, 3000, etc.)."
76+
echo "To make it a background service, create a launchd plist manually."
77+
78+
# Fallback to config value if port was not passed
79+
if [[ -z "$DEFAULT_PORT" ]]; then
80+
echo "Reading port from config: $CONFIG_PATH"
81+
DEFAULT_PORT=$(grep '^SERVE_PORT=' "$CONFIG_PATH" | cut -d= -f2)
82+
fi
83+
84+
# Fallback to config value if path was not passed
85+
if [[ -z "$DEFAULT_PATH" ]]; then
86+
echo "Reading path from config: $CONFIG_PATH"
87+
TEMP_PATH=$(grep '^SERVE_PATH=' "$CONFIG_PATH" | cut -d= -f2)
88+
89+
# Skip using /root if on macOS
90+
if [[ "$OS_NAME" == "Darwin" && "$TEMP_PATH" == "/root" ]]; then
91+
echo "⚠️ Ignoring '/root' path on macOS. Please specify --path manually."
92+
else
93+
DEFAULT_PATH="$TEMP_PATH"
94+
fi
95+
fi
96+
97+
98+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
99+
if [[ ! -x "$SCRIPT_DIR/macos-launchd-setup.sh" ]]; then
100+
echo "❌ Error: macos-launchd-setup.sh is missing or not executable."
101+
ls -l "$SCRIPT_DIR/macos-launchd-setup.sh"
102+
exit 1
103+
fi
104+
105+
chmod +x "$SCRIPT_DIR/macos-launchd-setup.sh"
106+
# bash "$SCRIPT_DIR/macos-launchd-setup.sh" -path "$DEFAULT_PATH" -port "$DEFAULT_PORT"
107+
LAUNCHD_ARGS=()
108+
[[ -n "$DEFAULT_PATH" ]] && LAUNCHD_ARGS+=("-path" "$DEFAULT_PATH")
109+
[[ -n "$DEFAULT_PORT" ]] && LAUNCHD_ARGS+=("-port" "$DEFAULT_PORT")
110+
111+
bash "$SCRIPT_DIR/macos-launchd-setup.sh" "${LAUNCHD_ARGS[@]}"
112+
113+
114+
else
115+
echo "❌ Unsupported OS: $OS_NAME"
116+
exit 1
117+
fi

0 commit comments

Comments
 (0)