📦➔🦋 Store and retrieve files on the Atmosphere
1#!/usr/bin/env bash
2
3function atfile.install() {
4 override_path="$1"
5 override_version="$2"
6 override_did="$3"
7
8 # shellcheck disable=SC2154
9 [[ $_output_json == 1 ]] && atfile.die "Command not available as JSON"
10
11 uid="$(id -u)"
12 # shellcheck disable=SC2154
13 conf_dir="${_path_envvar%/*}"
14 install_file="atfile"
15 unset found_version
16 unset install_dir
17 unset source_did
18
19 atfile.util.check_prog "curl"
20 atfile.util.check_prog "jq"
21
22 # shellcheck disable=SC2154
23 if [[ $_os_supported == 0 ]]; then
24 atfile.die "Unsupported OS (${_os//unknown-/})"
25 fi
26
27 case $_os in
28 "linux-termux")
29 # NOTE: The correct path of '$PREFIX/local/bin' would be more correct,
30 # however, '$PREFIX/local' doesn't exist by default on Termux (and thus,
31 # not in $PATH), so we'll install it in '$PREFIX/bin' instead
32 # shellcheck disable=SC2153
33 install_dir="$PREFIX/bin"
34 ;;
35 "haiku")
36 install_dir="/boot/system/non-packaged/bin"
37 ;;
38 *)
39 if [[ $uid == 0 ]]; then
40 install_dir="/usr/local/bin"
41 else
42 # shellcheck disable=SC2154
43 install_dir="$_path_home/.local/bin"
44 fi
45 ;;
46 esac
47
48 if [[ $# -gt 0 ]]; then
49 atfile.say.debug "Overridden variables\n↳ Path: $override_path\n↳ Version: $override_version\n↳ DID: $override_did"
50 fi
51
52 atfile.say.debug "Setting up..."
53
54 [[ -n "$override_path" ]] && install_dir="$override_path"
55 mkdir -p "$install_dir"
56 mkdir -p "$conf_dir"
57 # shellcheck disable=SC2154
58 touch "$conf_dir/$_file_envvar"
59
60 if [[ -f "$install_dir/$install_file" ]]; then
61 atfile.die "Already installed ($install_dir/$install_file)"
62 fi
63
64 atfile.say.debug "Resolving latest version..."
65
66 # shellcheck disable=SC2154
67 if [[ -z "$override_did" ]]; then
68 if [[ $_meta_did == "{:"*":}" ]]; then
69 # shellcheck disable=SC2154
70 source_did="$(atfile.util.get_envvar "${_envvar_prefix}_FORCE_META_DID")"
71 else
72 source_did="$_meta_did"
73 fi
74 else
75 source_did="$override_did"
76 fi
77
78 atfile.util.override_actor "$source_did"
79 # BUG: $_fmt_blob_url_default is unpopulated
80 _fmt_blob_url="[server]/xrpc/com.atproto.sync.getBlob?did=[did]&cid=[cid]"
81
82 if [[ -z "$override_version" ]]; then
83 latest_version_record="$(com.atproto.repo.getRecord "$source_did" "self.atfile.latest" "self")"
84 error="$(atfile.util.get_xrpc_error $? "$latest_version_record")"
85 [[ -n "$error" ]] && atfile.die "Unable to fetch latest version"
86
87 found_version="$(echo "$latest_version_record" | jq -r '.value.version')"
88 else
89 found_version="$override_version"
90 fi
91
92 parsed_found_version="$(atfile.util.parse_version "$found_version")"
93
94 found_version_key="atfile-$parsed_found_version"
95 found_version_record="$(com.atproto.repo.getRecord "$source_did" "blue.zio.atfile.upload" "$found_version_key")"
96 error="$(atfile.util.get_xrpc_error $? "$found_version_record")"
97 [[ -n "$error" ]] && atfile.die "Unable to fetch record for '$found_version'"
98
99 found_version_blob="$(echo "$found_version_record" | jq -r ".value.blob.ref.\"\$link\"")"
100 # shellcheck disable=SC2154
101 blob_url="$(atfile.util.build_blob_uri "$source_did" "$found_version_blob" "$_server")"
102
103 atfile.say.debug "Found latest version\n↳ Version: $found_version ($parsed_found_version)\n↳ Source: $source_did\n↳ Blob: $found_version_blob"
104
105 atfile.say.debug "Downloading...\n↳ Source: $blob_url\n↳ Dest.: ${install_dir}/$install_file"
106
107 curl -s -o "${install_dir}/$install_file" "$blob_url"
108 # shellcheck disable=SC2181
109 [[ $? != 0 ]] && atfile.die "Unable to download"
110
111 atfile.say.debug "Installing...\n↳ OS: $_os\n↳ Install: $install_dir/$install_file\n↳ Config: $conf_dir/$_file_envvar"
112
113 chmod +x "${install_dir}/$install_file"
114 [[ $? != 0 ]] && atfile.die "Unable to set as executable"
115
116 if [[ ! -f "$conf_dir/$_file_envvar" ]] || [[ ! -s "$conf_dir/$_file_envvar" ]]; then
117 atfile.say.debug "Creating config file..."
118
119 echo -e "ATFILE_USERNAME=<your-username>\nATFILE_PASSWORD=<your-password>" > "$conf_dir/$_file_envvar"
120 # shellcheck disable=SC2320
121 [[ $? != 0 ]] && die "Unable to create config file ($conf_dir/$_file_envvar)"
122 fi
123
124 atfile.say "😎 Installed ATFile"
125 atfile.say " ↳ Version: $found_version"
126 atfile.say " ↳ Paths"
127 atfile.say " ↳ Install: $install_dir/$install_file"
128 atfile.say " ↳ Config: $conf_dir/$_file_envvar"
129 atfile.say " ↳ Source: atfile://$source_did/$found_version_key"
130 atfile.say " ---"
131 atfile.say " Before running, set your credentials in the config file!"
132 atfile.say " Run '$install_file help' to get started"
133 # ------------------------------------------------------------------------------
134
135 atfile.util.override_actor_reset
136}