OSDN Git Service

e7f1193a96cf54b303c1d2215c7be5d60ef90a3d
[immortalwrt/luci.git] / applications / luci-app-unblockneteasemusic / root / usr / share / unblockneteasemusic / update.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-3.0-only
3 # Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>
4
5 NAME="unblockneteasemusic"
6
7 check_core_if_already_running(){
8         running_tasks="$(ps -w |grep "$NAME" |grep "update.sh" |grep "update_core" |grep -v "grep" |awk '{print $1}' |wc -l)"
9         [ "${running_tasks}" -gt "2" ] && { echo -e "\nA task is already running." >> "/tmp/$NAME.log"; exit 2; }
10 }
11
12 clean_log(){
13         echo "" > "/tmp/$NAME.log"
14 }
15
16 check_core_latest_version(){
17         core_latest_ver="$(uclient-fetch -qO- 'https://api.github.com/repos/UnblockNeteaseMusic/server/commits?sha=enhanced&path=precompiled' | jsonfilter -e '@[0].sha')"
18         [ -z "${core_latest_ver}" ] && { echo -e "\nFailed to check latest core version, please try again later." >> "/tmp/$NAME.log"; exit 1; }
19         if [ ! -e "/usr/share/$NAME/core_local_ver" ]; then
20                 clean_log
21                 echo -e "Local version: NOT FOUND, latest version: ${core_latest_ver}." >> "/tmp/$NAME.log"
22                 update_core
23         else
24                 if [ "$(cat /usr/share/$NAME/core_local_ver)" != "${core_latest_ver}" ]; then
25                         clean_log
26                         echo -e "Local version: $(cat /usr/share/$NAME/core_local_ver 2>"/dev/null"), latest version: ${core_latest_ver}." >> "/tmp/$NAME.log"
27                         update_core
28                 else
29                         echo -e "\nLocal version: $(cat /usr/share/$NAME/core_local_ver 2>"/dev/null"), latest version: ${core_latest_ver}." >> "/tmp/$NAME.log"
30                         echo -e "You're already using the latest version." >> "/tmp/$NAME.log"
31                         exit 3
32                 fi
33         fi
34 }
35
36 update_core(){
37         echo -e "Updating core..." >> "/tmp/$NAME.log"
38
39         mkdir -p "/usr/share/$NAME/core" > "/dev/null" 2>&1
40         rm -rf /usr/share/$NAME/core/* > "/dev/null" 2>&1
41
42         for url in $(uclient-fetch -qO- "https://api.github.com/repos/UnblockNeteaseMusic/server/contents/precompiled" |jsonfilter -e '@[*].download_url')
43         do
44                 uclient-fetch "${url}" -qO "/usr/share/$NAME/core/${url##*/}"
45                 [ -s "/usr/share/$NAME/core/${url##*/}" ] || {
46                         echo -e "Failed to download ${url##*/}." >> "/tmp/$NAME.log"
47                         exit 1
48                 }
49         done
50
51         for cert in "ca.crt" "server.crt" "server.key"
52         do
53                 uclient-fetch "https://raw.githubusercontent.com/UnblockNeteaseMusic/server/enhanced/${cert}" -qO "/usr/share/$NAME/core/${cert}"
54                 [ -s "/usr/share/$NAME/core/${cert}" ] || {
55                         echo -e "Failed to download ${cert}." >> "/tmp/$NAME.log"
56                         exit 1
57                 }
58         done
59
60         [ -n "${update_core_from_luci}" ] && touch "/usr/share/$NAME/update_core_successfully"
61         echo -e "${core_latest_ver}" > "/usr/share/$NAME/core_local_ver"
62         [ -z "${non_restart}" ] && /etc/init.d/$NAME restart
63
64         echo -e "Succeeded in updating core." > "/tmp/$NAME.log"
65         echo -e "Current core version: ${core_latest_ver}.\n" >> "/tmp/$NAME.log"
66 }
67
68 case "$1" in
69         "update_core")
70                 check_core_if_already_running
71                 check_core_latest_version
72                 ;;
73         "update_core_non_restart")
74                 non_restart=1
75                 check_core_if_already_running
76                 check_core_latest_version
77                 ;;
78         "update_core_from_luci")
79                 update_core_from_luci=1
80                 check_core_if_already_running
81                 check_core_latest_version
82                 ;;
83         *)
84                 echo -e "Usage: ./update.sh update_core"
85                 ;;
86 esac