OSDN Git Service

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