|
1 | 1 | #!/bin/bash |
2 | 2 | set -e |
3 | 3 |
|
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="" |
9 | | - |
10 | | -# Parse macOS arguments |
11 | 4 | 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 | | - ;; |
23 | | - |
24 | | - *) |
25 | | - echo "❌ Unknown argument: $1" |
26 | | - exit 1 |
27 | | - ;; |
28 | | - esac |
29 | | - done |
30 | | - |
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 |
35 | 5 |
|
36 | | -fi |
| 6 | +if [[ "$OS_NAME" == "Linux" ]]; then |
| 7 | + echo "Stopping custom-http-server service on Linux..." |
| 8 | + sudo systemctl stop custom-http-server || true |
37 | 9 |
|
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" |
| 10 | + echo "Disabling the service..." |
| 11 | + sudo systemctl disable custom-http-server || true |
42 | 12 |
|
43 | | -if [[ "$OS_NAME" == "Darwin" ]]; then |
44 | | - TARGET_DIR="$DEFAULT_TARGET_DIR" |
45 | | - CONFIG_PATH="$DEFAULT_CONFIG_PATH" |
46 | | -fi |
| 13 | + echo "Killing any running Python server process..." |
| 14 | + sudo pkill -f "/opt/custom-http-server/custom_http_server.py" || true |
47 | 15 |
|
48 | | -echo "Installing Custom HTTP Server..." |
| 16 | + echo "Removing systemd service file..." |
| 17 | + sudo rm -f /etc/systemd/system/custom-http-server.service |
49 | 18 |
|
50 | | -# Create target and config directories |
51 | | -sudo mkdir -p "$TARGET_DIR" |
52 | | -sudo mkdir -p "$(dirname "$CONFIG_PATH")" |
| 19 | + echo "Removing config file..." |
| 20 | + sudo rm -f /etc/custom-http-server.conf |
53 | 21 |
|
54 | | -# Copy application and config files |
55 | | -sudo cp custom_http_server.py "$TARGET_DIR/" |
56 | | -sudo cp default-config.conf "$CONFIG_PATH" |
| 22 | + echo "Removing installed files from /opt/custom-http-server..." |
| 23 | + sudo rm -rf /opt/custom-http-server |
57 | 24 |
|
58 | | -if [[ "$OS_NAME" == "Linux" ]]; then |
59 | | - echo "✅ Installing on Linux." |
60 | | - sudo cp custom-http-server.service "$SERVICE_PATH" |
| 25 | + echo "Reloading systemd daemon..." |
61 | 26 | sudo systemctl daemon-reload |
62 | | - sudo systemctl enable custom-http-server |
63 | | - sudo systemctl start custom-http-server |
64 | 27 |
|
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" |
| 28 | + echo "Uninstall complete on Linux." |
68 | 29 |
|
69 | 30 | 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 |
| 31 | + echo "Stopping custom-http-server(com.custom_http_server.python) service on macOS..." |
83 | 32 |
|
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 |
| 33 | + PLIST="$HOME/Library/LaunchAgents/com.custom_http_server.plist" |
96 | 34 |
|
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 |
| 35 | + if [[ -f "$PLIST" ]]; then |
| 36 | + launchctl unload "$PLIST" 2>/dev/null || true |
| 37 | + echo "Unloaded service: $PLIST" |
103 | 38 | fi |
104 | 39 |
|
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") |
| 40 | + if [[ -f "$PLIST" ]]; then |
| 41 | + rm "$PLIST" |
| 42 | + echo "Removed plist: $PLIST" |
| 43 | + fi |
110 | 44 |
|
111 | | - bash "$SCRIPT_DIR/macos-launchd-setup.sh" "${LAUNCHD_ARGS[@]}" |
| 45 | + echo "Verifying removal..." |
| 46 | + launchctl list | grep custom_http_server || echo "Service fully removed." |
112 | 47 |
|
| 48 | + echo "Uninstall complete on macOS." |
113 | 49 |
|
114 | 50 | else |
115 | | - echo "❌ Unsupported OS: $OS_NAME" |
| 51 | + echo "Unsupported OS: $OS_NAME" |
116 | 52 | exit 1 |
117 | 53 | fi |
0 commit comments