Browse Source

refactor(install-script): make compatible with ash

Hintay 1 ngày trước cách đây
mục cha
commit
956c313c27
1 tập tin đã thay đổi với 129 bổ sung26 xóa
  1. 129 26
      install.sh

+ 129 - 26
install.sh

@@ -51,8 +51,99 @@ FontSkyBlue="\033[36m";
 FontWhite="\033[37m";
 FontSuffix="\033[0m";
 
-curl() {
-    $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
+# Download tool
+DOWNLOADER=""
+
+# Check and select download tool
+check_download_tool() {
+    if type -P curl >/dev/null 2>&1; then
+        DOWNLOADER="curl"
+    elif type -P wget >/dev/null 2>&1; then
+        DOWNLOADER="wget"
+    else
+        echo -e "${FontRed}error: curl or wget is required but not installed.${FontSuffix}"
+        exit 1
+    fi
+    echo "info: Using $DOWNLOADER as the download tool."
+}
+
+download() {
+    if [ "$DOWNLOADER" = "wget" ]; then
+        # Emulate curl behavior using wget
+        # Replace common curl parameters with wget equivalents
+        local url=""
+        local output=""
+        local headers=""
+        local quiet=0
+        local all_args="$*"
+        
+        # Parse for URL first
+        for arg in $all_args; do
+            if echo "$arg" | grep -q -E "^(http|https|ftp)"; then
+                url="$arg"
+                break
+            fi
+        done
+        
+        # Handle typical curl options
+        while [ $# -gt 0 ]; do
+            case "$1" in
+                -L|-q|--retry|--retry-delay|--retry-max-time)
+                    # These parameters have similar alternatives in wget, but we'll ignore them for simplicity
+                    shift
+                    ;;
+                -x)
+                    shift
+                    if [ -n "$1" ]; then
+                        # Convert to wget proxy format
+                        if echo "$1" | grep -q -E "^http://"; then
+                            headers="$headers --proxy=on --proxy-type=http -e http_proxy=$1"
+                        elif echo "$1" | grep -q -E "^socks"; then
+                            headers="$headers --proxy=on --proxy-type=socks5 -e socks_proxy=$1"
+                        fi
+                    fi
+                    shift
+                    ;;
+                -o)
+                    shift
+                    output="-O $1"
+                    shift
+                    ;;
+                -R|-H)
+                    shift
+                    # Extract the header name and value
+                    header_name=$(echo "$1" | cut -d':' -f1)
+                    header_value=$(echo "$1" | cut -d':' -f2-)
+                    # Trim leading space if exists in value
+                    header_value=$(echo "$header_value" | sed 's/^ *//')
+                    # Add header with proper format for wget
+                    headers="$headers --header=\"$header_name: $header_value\""
+                    shift
+                    ;;
+                -s|-S)
+                    # Silent mode
+                    quiet=1
+                    shift
+                    ;;
+                *)
+                    shift
+                    ;;
+            esac
+        done
+        
+        if [ $quiet -eq 1 ]; then
+            headers="$headers -q"
+        fi
+        
+        if [ -n "$output" ]; then
+            eval command wget $headers $output \"$url\" 2>/dev/null
+        else
+            eval command wget $headers -O - \"$url\" 2>/dev/null
+        fi
+    else
+        # Use native curl
+        $(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
+    fi
 }
 
 ## Demo function for processing parameters
@@ -102,9 +193,9 @@ judgment_parameters() {
         esac
         shift
     done
-    if ((INSTALL+HELP+REMOVE==0)); then
+    if [ "$(expr $INSTALL + $HELP + $REMOVE)" -eq 0 ]; then
         INSTALL='1'
-    elif ((INSTALL+HELP+REMOVE>1)); then
+    elif [ "$(expr $INSTALL + $HELP + $REMOVE)" -gt 1 ]; then
         echo 'You can only choose one action.'
         exit 1
     fi
@@ -133,7 +224,7 @@ systemd_cat_config() {
 
 check_if_running_as_root() {
     # If you want to run as another user, please modify $EUID to be owned by this user
-    if [[ "$EUID" -ne '0' ]]; then
+    if [ "$(id -u)" != "0" ]; then
         echo -e "${FontRed}error: You must run this script as root!${FontSuffix}"
         exit 1
     fi
@@ -229,7 +320,7 @@ install_software() {
 get_latest_version() {
     # Get latest release version number
     local latest_release
-    if ! latest_release=$(curl -x "${PROXY}" -sS -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/0xJacky/nginx-ui/releases/latest"); then
+    if ! latest_release=$(download -x "${PROXY}" -sS -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/0xJacky/nginx-ui/releases/latest"); then
         echo -e "${FontRed}error: Failed to get release list, please check your network.${FontSuffix}"
         exit 1
     fi
@@ -252,7 +343,7 @@ download_nginx_ui() {
     download_link="${RPROXY}https://github.com/0xJacky/nginx-ui/releases/download/$RELEASE_LATEST/nginx-ui-linux-$MACHINE.tar.gz"
 
     echo "Downloading Nginx UI archive: $download_link"
-    if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$TAR_FILE" "$download_link"; then
+    if ! download -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$TAR_FILE" "$download_link"; then
         echo 'error: Download failed! Please check your network or try again.'
         return 1
     fi
@@ -296,7 +387,7 @@ install_systemd_service() {
     local service_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.service"
 
     echo "Downloading Nginx UI service file: $service_download_link"
-    if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$ServicePath" "$service_download_link"; then
+    if ! download -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$ServicePath" "$service_download_link"; then
         echo -e "${FontRed}error: Download service file failed! Please check your network or try again.${FontSuffix}"
         return 1
     fi
@@ -314,7 +405,7 @@ install_openrc_service() {
     local openrc_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.rc"
 
     echo "Downloading Nginx UI OpenRC file: $openrc_download_link"
-    if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$OpenRCPath" "$openrc_download_link"; then
+    if ! download -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$OpenRCPath" "$openrc_download_link"; then
         echo -e "${FontRed}error: Download OpenRC file failed! Please check your network or try again.${FontSuffix}"
         return 1
     fi
@@ -335,7 +426,7 @@ install_initd_service() {
     local initd_download_link="${RPROXY}https://raw.githubusercontent.com/0xJacky/nginx-ui/main/resources/services/nginx-ui.init"
 
     echo "Downloading Nginx UI init.d file: $initd_download_link"
-    if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$InitPath" "$initd_download_link"; then
+    if ! download -x "${PROXY}" -R -H 'Cache-Control: no-cache' -L -o "$InitPath" "$initd_download_link"; then
         echo -e "${FontRed}error: Download init.d file failed! Please check your network or try again.${FontSuffix}"
         exit 1
     fi
@@ -439,18 +530,18 @@ remove_nginx_ui() {
     if [[ -n "$(pidof nginx-ui)" ]]; then
       stop_nginx_ui
     fi
-    local delete_files=('/usr/local/bin/nginx-ui' '/etc/systemd/system/nginx-ui.service' '/etc/systemd/system/nginx-ui.service.d')
+    delete_files="/usr/local/bin/nginx-ui /etc/systemd/system/nginx-ui.service /etc/systemd/system/nginx-ui.service.d"
     if [[ "$PURGE" -eq '1' ]]; then
-        [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
+        [[ -d "$DataPath" ]] && delete_files="$delete_files $DataPath"
     fi
     systemctl disable nginx-ui
-    if ! ("rm" -r "${delete_files[@]}"); then
+    if ! ("rm" -r $delete_files); then
       echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
       exit 1
     else
-      for i in "${!delete_files[@]}"
+      for file in $delete_files
       do
-        echo "removed: ${delete_files[$i]}"
+        echo "removed: $file"
       done
       systemctl daemon-reload
       echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
@@ -465,21 +556,21 @@ remove_nginx_ui() {
     if rc-service nginx-ui status | grep -q "started"; then
       stop_nginx_ui
     fi
-    local delete_files=('/usr/local/bin/nginx-ui' "$OpenRCPath")
+    delete_files="/usr/local/bin/nginx-ui $OpenRCPath"
     if [[ "$PURGE" -eq '1' ]]; then
-        [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
+        [[ -d "$DataPath" ]] && delete_files="$delete_files $DataPath"
     fi
 
     # Remove from runlevels
     rc-update del nginx-ui default
 
-    if ! ("rm" -r "${delete_files[@]}"); then
+    if ! ("rm" -r $delete_files); then
       echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
       exit 1
     else
-      for i in "${!delete_files[@]}"
+      for file in $delete_files
       do
-        echo "removed: ${delete_files[$i]}"
+        echo "removed: $file"
       done
       echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
       echo 'info: Nginx UI has been removed.'
@@ -493,9 +584,9 @@ remove_nginx_ui() {
     if [[ -n "$(pidof nginx-ui)" ]]; then
       stop_nginx_ui
     fi
-    local delete_files=('/usr/local/bin/nginx-ui' "$InitPath")
+    delete_files="/usr/local/bin/nginx-ui $InitPath"
     if [[ "$PURGE" -eq '1' ]]; then
-        [[ -d "$DataPath" ]] && delete_files+=("$DataPath")
+        [[ -d "$DataPath" ]] && delete_files="$delete_files $DataPath"
     fi
 
     # Remove from startup based on distro
@@ -505,13 +596,13 @@ remove_nginx_ui() {
         /usr/sbin/update-rc.d -f nginx-ui remove
     fi
 
-    if ! ("rm" -r "${delete_files[@]}"); then
+    if ! ("rm" -r $delete_files); then
       echo -e "${FontRed}error: Failed to remove Nginx UI.${FontSuffix}"
       exit 1
     else
-      for i in "${!delete_files[@]}"
+      for file in $delete_files
       do
-        echo "removed: ${delete_files[$i]}"
+        echo "removed: $file"
       done
       echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl"
       echo 'info: Nginx UI has been removed.'
@@ -550,6 +641,7 @@ show_help() {
 main() {
     check_if_running_as_root
     identify_the_operating_system_and_architecture
+    check_download_tool
     judgment_parameters "$@"
 
     # Parameter information
@@ -560,7 +652,18 @@ main() {
     TMP_DIRECTORY="$(mktemp -d)"
     TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
 
-    install_software 'curl' 'curl'
+    # Ensure the system has an available download tool
+    if [ "$DOWNLOADER" = "curl" ]; then
+        if ! type -P curl >/dev/null 2>&1; then
+            echo "info: curl is not found, trying to install..."
+            install_software 'curl' 'curl'
+        fi
+    elif [ "$DOWNLOADER" = "wget" ]; then
+        if ! type -P wget >/dev/null 2>&1; then
+            echo "info: wget is not found, trying to install..."
+            install_software 'wget' 'wget'
+        fi
+    fi
 
     # Install from a local file
     if [[ -n "$LOCAL_FILE" ]]; then