OSDN Git Service

luci-app-unblockneteasemusic: bump to 2.13
[immortalwrt/luci.git] / applications / luci-app-unblockneteasemusic / root / usr / share / unblockneteasemusic / update.sh
index e7f1193..806f9ba 100755 (executable)
@@ -1,21 +1,26 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-3.0-only
-# Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>
+# Copyright (C) 2019-2022 Tianling Shen <cnsztl@immortalwrt.org>
 
 NAME="unblockneteasemusic"
+LOCK="/tmp/$NAME.update_core.lock"
 
-check_core_if_already_running(){
-       running_tasks="$(ps -w |grep "$NAME" |grep "update.sh" |grep "update_core" |grep -v "grep" |awk '{print $1}' |wc -l)"
-       [ "${running_tasks}" -gt "2" ] && { echo -e "\nA task is already running." >> "/tmp/$NAME.log"; exit 2; }
+check_core_if_already_running() {
+       if [ -e "$LOCK" ]; then
+               echo -e "\nA task is already running." >> "/tmp/$NAME.log"
+               exit 2
+       else
+               touch "$LOCK"
+       fi
 }
 
 clean_log(){
        echo "" > "/tmp/$NAME.log"
 }
 
-check_core_latest_version(){
+check_core_latest_version() {
        core_latest_ver="$(uclient-fetch -qO- 'https://api.github.com/repos/UnblockNeteaseMusic/server/commits?sha=enhanced&path=precompiled' | jsonfilter -e '@[0].sha')"
-       [ -z "${core_latest_ver}" ] && { echo -e "\nFailed to check latest core version, please try again later." >> "/tmp/$NAME.log"; exit 1; }
+       [ -n "${core_latest_ver}" ] || { echo -e "\nFailed to check latest core version, please try again later." >> "/tmp/$NAME.log"; rm -f "$LOCK"; exit 1; }
        if [ ! -e "/usr/share/$NAME/core_local_ver" ]; then
                clean_log
                echo -e "Local version: NOT FOUND, latest version: ${core_latest_ver}." >> "/tmp/$NAME.log"
@@ -28,22 +33,24 @@ check_core_latest_version(){
                else
                        echo -e "\nLocal version: $(cat /usr/share/$NAME/core_local_ver 2>"/dev/null"), latest version: ${core_latest_ver}." >> "/tmp/$NAME.log"
                        echo -e "You're already using the latest version." >> "/tmp/$NAME.log"
+                       rm -f "$LOCK"
                        exit 3
                fi
        fi
 }
 
-update_core(){
+update_core() {
        echo -e "Updating core..." >> "/tmp/$NAME.log"
 
-       mkdir -p "/usr/share/$NAME/core" > "/dev/null" 2>&1
-       rm -rf /usr/share/$NAME/core/* > "/dev/null" 2>&1
+       mkdir -p "/usr/share/$NAME/core"
+       rm -rf "/usr/share/$NAME/core"/*
 
        for url in $(uclient-fetch -qO- "https://api.github.com/repos/UnblockNeteaseMusic/server/contents/precompiled" |jsonfilter -e '@[*].download_url')
        do
                uclient-fetch "${url}" -qO "/usr/share/$NAME/core/${url##*/}"
                [ -s "/usr/share/$NAME/core/${url##*/}" ] || {
                        echo -e "Failed to download ${url##*/}." >> "/tmp/$NAME.log"
+                       rm -f "$LOCK"
                        exit 1
                }
        done
@@ -53,16 +60,18 @@ update_core(){
                uclient-fetch "https://raw.githubusercontent.com/UnblockNeteaseMusic/server/enhanced/${cert}" -qO "/usr/share/$NAME/core/${cert}"
                [ -s "/usr/share/$NAME/core/${cert}" ] || {
                        echo -e "Failed to download ${cert}." >> "/tmp/$NAME.log"
+                       rm -f "$LOCK"
                        exit 1
                }
        done
 
-       [ -n "${update_core_from_luci}" ] && touch "/usr/share/$NAME/update_core_successfully"
+       [ -z "${update_core_from_luci}" ] || touch "/usr/share/$NAME/update_core_successfully"
        echo -e "${core_latest_ver}" > "/usr/share/$NAME/core_local_ver"
-       [ -z "${non_restart}" ] && /etc/init.d/$NAME restart
+       [ -n "${non_restart}" ] || /etc/init.d/"$NAME" restart
 
        echo -e "Succeeded in updating core." > "/tmp/$NAME.log"
        echo -e "Current core version: ${core_latest_ver}.\n" >> "/tmp/$NAME.log"
+       rm -f "$LOCK"
 }
 
 case "$1" in
@@ -81,6 +90,6 @@ case "$1" in
                check_core_latest_version
                ;;
        *)
-               echo -e "Usage: ./update.sh update_core"
+               echo -e "Usage: $0/update.sh update_core"
                ;;
 esac