#!/usr/bin/env sh # should work in any POSIX shell on a UNIX-like environment, so pretty much any modern linux/BSD with essential CLI utilities # note that busybox wget may not support https URLs (the version from arch linux's repos doesn't) and will not be able to download from ContentDB content_path="${LUANTI_USER_PATH:-$HOME/.minetest}" mkdir -p "$content_path" print_message () { printf '[\033[%sm%s\033[0m] %s\n' "$1" "$2" "$3" } dl_cmd=none wget &>/dev/null [ $? -ne 127 ] && dl_cmd='wget -qO' curl &>/dev/null [ $? -ne 127 ] && dl_cmd='curl -sLo' if [ "$dl_cmd" = none ]; then print_message 31 ERROR 'curl or wget is needed to download packages!' exit 1 fi print_help () { echo "$0 Actions: install - install a package remove - remove a package update - update a package help - show this help" } install_package () { local tmp_dir="$(mktemp -d)" local pkg_name="${1##*/}" print_message 34 INFO "Downloading package info for '${pkg_name}'" $dl_cmd "${tmp_dir}/${pkg_name}" "https://content.luanti.org/api/packages/${1}" # this could in theory fail (especially if changes are made to the JSON structure), though i doubt it would happen by accident local pkg_type="$(grep -oE '"type":"(game|mod|txp)"' "${tmp_dir}/${pkg_name}" | grep -oE '(game|mod|txp)' | tail -n1)" case "$pkg_type" in game) : "games" ;; mod) : "mods" ;; txp) : "textures" ;; '') print_message 31 ERROR "No such package '$pkg_name'" return 1 ;; *) print_message 31 ERROR "Unknown package type '$pkg_type'" return 1 ;; esac local pkg_dir="${content_path}/${_}/" mkdir -p "$pkg_dir" if [ -d "${pkg_dir}/${pkg_name}" ]; then print_message 34 INFO "Package '${pkg_name}' is already installed, not installing." return 0 fi print_message 34 INFO "Downloading package '$pkg_name'" $dl_cmd "${tmp_dir}/${pkg_name}.zip" "https://content.luanti.org/packages/${1}/download" print_message 34 INFO "Installing package '$pkg_name'" unzip -q -d "$pkg_dir" "${tmp_dir}/${pkg_name}.zip" rm -r "$tmp_dir" return 0 } remove_package () { local tmp_dir="$(mktemp -d)" local pkg_name="${1##*/}" print_message 34 INFO "Downloading package info for '${pkg_name}'" $dl_cmd "${tmp_dir}/${pkg_name}" "https://content.luanti.org/api/packages/${1}" local pkg_type="$(grep -oE '"type":"(game|mod|txp)"' "${tmp_dir}/${pkg_name}" | grep -oE '(game|mod|txp)' | tail -n1)" rm -r "$tmp_dir" case "$pkg_type" in game) : "games" ;; mod) : "mods" ;; txp) : "textures" ;; '') print_message 31 ERROR "No such package '$pkg_name'" return 1 ;; *) print_message 31 ERROR "Unknown package type '$pkg_type'" return 1 ;; esac local pkg_dir="${content_path}/${_}/${pkg_name}" if [ -d "$pkg_dir" ]; then print_message 34 INFO "Removing package '$pkg_name'" rm -r "$pkg_dir" return 0 fi print_message 34 INFO "Package '$pkg_name' not installed, not removing." return 0 } # this is just a wrapper to call remove_package and install_package on a package.. update_package () { local pkg_name="${1##*/}" print_message 34 INFO "[1/2] Removing package '$pkg_name'" remove_package $1 print_message 34 INFO "[2/2] Installing package '$pkg_name'" install_package $1 } case $1 in install) case $2 in */*) install_package $2 print_message 32 OK 'Operation complete.' ;; *) print_message 31 ERROR "Invalid syntax for 'install'. Correct syntax: '$0 install /'" ;; esac ;; remove) case $2 in */*) remove_package $2 print_message 32 OK 'Operation complete.' ;; *) print_message 31 ERROR "Invalid syntax for 'remove'. Correct syntax: '$0 remove /package>'" ;; esac ;; update) case $2 in */*) update_package $2 print_message 32 OK 'Operation complete.' ;; *) print_message 31 ERROR "Invalid syntax for 'update'. Correct syntax: '$0 update /package>'" ;; esac ;; help) print_help ;; *) print_message 31 ERROR 'Invalid usage. Usage:' print_help ;; esac