From d4d0593927678da56e797191af2e4f23618e45a1 Mon Sep 17 00:00:00 2001
From: aclist <92275929+aclist@users.noreply.github.com>
Date: Fri, 13 Dec 2024 09:49:39 +0900
Subject: [PATCH] fix: add fallback logic to installscript

---
 install.sh | 47 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 7 deletions(-)
 mode change 100644 => 100755 install.sh

diff --git a/install.sh b/install.sh
old mode 100644
new mode 100755
index 22ec6ab..13f96cc
--- a/install.sh
+++ b/install.sh
@@ -1,8 +1,41 @@
 #!/usr/bin/env bash
-curl -L "https://codeberg.org/aclist/dzgui/raw/branch/dzgui/dzgui.sh" > dzgui.sh
-chmod +x dzgui.sh
-xdg_file="$HOME/.local/share/applications/dzgui.desktop"
-share="$HOME/.local/share/dzgui"
-[[ -f $xdg_file ]] && rm $xdg_file
-[[ -d $share ]] && rm -rf "$share"
-./dzgui.sh
+get_response_code(){
+    local url="$1"
+    curl -Ls -I -o /dev/null -w "%{http_code}" "$url"
+}
+abort(){
+    printf "Remote resource not available. Try again later.\n"
+    exit 1
+}
+fetch(){
+    local file="dzgui.sh"
+    local author="aclist"
+    local repo="dztui"
+    local branch="dzgui"
+    local url
+    local res
+    gh_url="https://raw.githubusercontent.com/$author/$repo/$branch/$file"
+    cb_url="https://codeberg.org/$author/$repo/raw/branch/$branch/$file"
+
+    url="$gh_url"
+    printf "Checking the remote resource at '%s'\n" "$url"
+    res=$(get_response_code "$url")
+    if [[ $res -ne 200 ]]; then
+        url="$cb_url"
+        printf "Checking the remote resource at '%s'\n" "$url"
+        res=$(get_response_code "$url")
+        if [[ $res -ne 200 ]]; then
+            abort
+        fi
+    fi
+
+    curl -L "$url" > dzgui.sh
+    chmod +x dzgui.sh
+    xdg_file="$HOME/.local/share/applications/dzgui.desktop"
+    share="$HOME/.local/share/dzgui"
+    [[ -f $xdg_file ]] && rm $xdg_file
+    [[ -d $share ]] && rm -rf "$share"
+    ./dzgui.sh
+}
+
+fetch