2
0
Fork 0
rootfs/usr/local/bin/sh.zio.install-package

105 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
if [[ "$(realpath "$(dirname "$(realpath -s "$0")")/../../../")" == "/" ]]; then
. /usr/local/libexec/zio/helpers/bash.sh
else
. "$(dirname "$(realpath -s "$0")")/../libexec/zio/helpers/bash.sh"
fi
requested_package="$1"
tmp_path_hash="$(date +%s)"
function get_install_path() {
install_path="/usr/local/bin/$1"
if [[ -f "$install_path" ]] && [[ "$(file "$install_path")" != *": empty" ]]; then
chmod +x "$install_path"
fi
echo "$install_path"
}
function get_name() {
package_id="$1"
package_name=""
case $package_id in
"caddy") package_name="Caddy" ;;
*) package_name="$package_id" ;;
esac
echo "$package_name"
}
function get_tmp_path() {
tmp_path="/tmp/$1_${tmp_path_hash}"
if [[ -f "$tmp_path" ]] && [[ "$(file "$tmp_path")" != *": empty" ]]; then
chmod +x "$tmp_path"
fi
echo "$tmp_path"
}
function download() {
package_id="$1"
url="$2"
package_name="$(get_name "$package_id")"
package_tmp_path="$(get_tmp_path "$package_id")"
say info "Downloading: $package_name ($package_tmp_path)"
wget "$url" -O "$(get_tmp_path "$package_id")"
}
function install() {
package_id="$1"
package_version="$2"
package_name="$(get_name "$package_id")"
if [[ -z "$package_version" ]]; then
say info "Installing: $package_name ($(get_install_path "$package_id"))"
mv "$(get_tmp_path "$package_id")" "$(get_install_path "$package_id")"
else
if [[ "$package_version" != "v"* ]]; then
package_version="v$package_version"
fi
package_id_version="$package_id-$package_version"
say info "Installing: $package_name $package_version ($(get_install_path "$package_id_version"))"
rm -f "$(get_install_path "$package_id")"
mv "$(get_tmp_path "$package_id")" "$(get_install_path "$package_id_version")"
say info "Symlinking: $(get_install_path "$package_id_version")$(get_install_path "$package_id")"
ln -s "$(get_install_path "$package_id_version")" "$(get_install_path "$package_id")"
fi
}
function download_caddy() {
package_id="caddy"
download "caddy" "https://caddyserver.com/api/download?os=linux&arch=amd64&p=github.com%2Fcaddy-dns%2Fcloudflare"
caddy_version="$($(get_tmp_path "$package_id") version)"
caddy_version_array=($caddy_version)
caddy_version="${caddy_version_array[0]}"
install "$package_id" "$caddy_version"
}
function download_generic() {
package_id="$requested_package"
download "$requested_package" "$1"
install "$requested_package"
}
test_root
test_prog "wget"
case $requested_package in
"caddy") download_caddy ;;
*) die "Package '$requested_package' cannot be installed by this tool" ;;
esac