š¦āš¦ Store and retrieve files on the Atmosphere
1#!/usr/bin/env bash
2
3# TODO: Validate checksum
4function atfile.update() {
5 cmd="$1"
6 unset error
7 is_git=0
8
9 if [ -x "$(command -v git)" ] && [[ -d "$_prog_dir/.git" ]] && [[ "$(atfile.util.get_realpath "$(pwd)")" == "$_prog_dir" ]]; then
10 is_git=1
11 fi
12
13 if [[ "$cmd" == "check-only" ]]; then
14 # shellcheck disable=SC2154
15 [[ $_disable_update_checking == 1 ]] && return
16 # shellcheck disable=SC2154
17 [[ $_disable_update_command == 1 ]] && return
18 # shellcheck disable=SC2154
19 [[ $is_git == 1 && $_enable_update_git_clobber == 0 ]] && return
20 # shellcheck disable=SC2154
21 [[ $_output_json == 1 ]] && return
22
23 last_checked="$(atfile.cache.get "update-check")"
24 current_checked="$(atfile.util.get_date "" "%s")"
25 check_sleep=3600
26 next_check=$(( last_checked + check_sleep ))
27
28 atfile.say.debug "Checking for last update check...\nā³ Last: $last_checked\nā³ Cur.: $current_checked\nā³ Next: $next_check"
29
30 if [[ $(( next_check < current_checked )) == 0 ]]; then
31 return
32 else
33 last_checked="$(atfile.cache.set "update-check" "$current_checked")"
34 fi
35 fi
36
37 [[ $_output_json == 1 ]] && atfile.die "Command not available as JSON"
38
39 # shellcheck disable=SC2154
40 update_did="$_devel_dist_username"
41
42 atfile.util.override_actor "$update_did"
43
44 atfile.say.debug "Getting latest release..."
45 latest_release_record="$(com.atproto.repo.getRecord "$update_did" "self.atfile.latest" "self")"
46 error="$(atfile.util.get_xrpc_error $? "$latest_release_record")"
47
48 [[ -n "$error" ]] && atfile.die "Unable to get latest version" "$error"
49
50 latest_version="$(echo "$latest_release_record" | jq -r '.value.version')"
51 latest_version_commit="$(echo "$latest_release_record" | jq -r '.value.commit')"
52 latest_version_date="$(echo "$latest_release_record" | jq -r '.value.releasedAt')"
53 parsed_latest_version="$(atfile.util.parse_version "$latest_version")"
54 parsed_running_version="$(atfile.util.parse_version "$_version")"
55 latest_version_record_id="atfile-$parsed_latest_version"
56 update_available=0
57
58 atfile.say.debug "Checking version...\nā³ Latest: $latest_version ($parsed_latest_version)\n ā³ Date: $latest_version_date\n ā³ Commit: $latest_version_commit\nā³ Running: $_version ($parsed_running_version)"
59
60 if [[ $(( parsed_latest_version > parsed_running_version )) == 1 ]]; then
61 update_available=1
62 fi
63
64 case "$cmd" in
65 "check-only")
66 if [[ $update_available == 0 ]]; then
67 atfile.say.debug "No updates found"
68 return
69 fi
70
71 echo "---"
72 if [[ $_os == "haiku" ]]; then
73 # BUG: Haiku Terminal has issues with emojis
74 # shellcheck disable=SC2154
75 atfile.say "Update available ($latest_version)\nā³ Run \`$_prog update\` to update"
76 else
77 atfile.say "š Update available ($latest_version)\n ā³ Run \`$_prog update\` to update"
78 fi
79 ;;
80 "install")
81 [[ $is_git == 1 && $_enable_update_git_clobber == 0 ]] &&\
82 atfile.die "Cannot update in Git repository"
83 [[ $_disable_update_command == 1 ]] &&\
84 atfile.die "Cannot update system-managed version: update from your package manager"
85
86 if [[ $update_available == 0 ]]; then
87 atfile.say "No updates found"
88 return
89 fi
90
91 # shellcheck disable=SC2154
92 temp_updated_path="$_prog_dir/${_prog}-${latest_version}.tmp"
93
94 atfile.say.debug "Touching temporary path ($temp_updated_path)..."
95 touch "$temp_updated_path"
96 # shellcheck disable=SC2181
97 [[ $? != 0 ]] && atfile.die "Unable to create temporary file (do you have permission?)"
98
99 atfile.say.debug "Getting blob URL for $latest_version ($latest_version_record_id)..."
100 blob_url="$(atfile.get_url "$latest_version_record_id")"
101 # shellcheck disable=SC2181
102 [[ $? != 0 ]] && atfile.die "Unable to get blob URL"
103
104 atfile.say.debug "Downloading latest release..."
105 curl -H "User-Agent: $(atfile.util.get_uas)" -s -o "$temp_updated_path" "$blob_url"
106 # shellcheck disable=SC2181
107 if [[ $? == 0 ]]; then
108 # shellcheck disable=SC2154
109 mv "$temp_updated_path" "$_prog_path"
110 # shellcheck disable=SC2181
111 if [[ $? != 0 ]]; then
112 atfile.die "Unable to update (do you have permission?)"
113 else
114 chmod +x "$_prog_path"
115
116 if [[ $_os == "haiku" ]]; then
117 atfile.say "Updated to $latest_version!" # BUG: Haiku Terminal has issues with emojis
118 else
119 atfile.say "š Updated to $latest_version!"
120 fi
121
122 last_checked="$(atfile.cache.set "update-check" "$(atfile.util.get_date "" "%s")")"
123
124 return
125 fi
126 else
127 atfile.die "Unable to download latest version"
128 fi
129 ;;
130 esac
131}