~hnaguski/vtt

b3072a8ecfc76eda8c1529db4ff8286947bbb47a — Henry Naguski 8 months ago a6d6de4
put server information into environment variables
4 files changed, 26 insertions(+), 20 deletions(-)

M _vtt
M vtd
M vts
M vtu
M _vtt => _vtt +1 -1
@@ 1,7 1,7 @@
#compdef vtu vts vtd

_vtt_avaliable_packs() {
	compadd "$@" -- ${${${(f)"$(jq -r '.categories[].packs[].name' "${VTT_CACHE:-$HOME/.cache}"/rpcategories-${VTT_MCVER:-1.18}.json | sort)"}#??? }%%-[^- ]# *}
	compadd "$@" -- ${${${(f)"$(jq -r '.categories[].packs[].name' "${VTT_CACHE:-$HOME/.cache}/${VTT_REMOTE:-vanillatweaks.net}"/rpcategories-${VTT_MCVER:-1.18}.json | sort)"}#??? }%%-[^- ]# *}
}

_vtd() {

M vtd => vtd +12 -9
@@ 2,6 2,9 @@

set -e

VTT_PROTOCOL="${VTT_PROTOCOL:-https://}"
VTT_REMOTE="${VTT_REMOTE:-vanillatweaks.net}"
VTT_REMOTE_ZIPPACKS="${VTT_REMOTE_ZIPPACKS:-$VTT_PROTOCOL$VTT_REMOTE/assets/server/zipresourcepacks.php}"
VTT_CACHE="${VTT_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}}"
VTT_MCVER="${VTT_MCVER:-1.18}"
VTD_TMPDIR="${VTD_TMPDIR:-/tmp/vtd.$$}"


@@ 39,16 42,16 @@ downloadMany() {
	printf 'Generating json for the following packs (this is slow):\n'
	printf '%s\n' "$@"
	printf '%s\n' "$@" | while read -r pack; do
		jq -r --arg "pack" "$pack" '.categories[] | select(.packs[].name | test($pack; "i")) | "\(.packs[] | select(.name | test($pack; "i")).name) \(.category)"' "$VTT_CACHE/rpcategories-$VTT_MCVER.json" | while read -r packname category; do
		jq -r --arg "pack" "$pack" '.categories[] | select(.packs[].name | test($pack; "i")) | "\(.packs[] | select(.name | test($pack; "i")).name) \(.category)"' "$VTT_CACHE/$VTT_REMOTE/rpcategories-$VTT_MCVER.json" | while read -r packname category; do
			jq -r --arg "category" "$category" --arg "pack" "$packname" '.[$category] |= .+ [$pack]' "$VTD_TMPDIR/packs_template.json" >"$VTD_TMPDIR/packs_template_new.json"
			mv "$VTD_TMPDIR/packs_template_new.json" "$VTD_TMPDIR/packs_template.json"
		done
	done

	# get download link
	packdl=$(curl -s "https://vanillatweaks.net/assets/server/zipresourcepacks.php" --data-urlencode "packs=$(cat "$VTD_TMPDIR"/packs_template.json)" --data-urlencode "version=$VTT_MCVER" | jq -r .link)
	packdl=$(curl -s "$VTT_REMOTE_ZIPPACKS" --data-urlencode "packs=$(cat "$VTD_TMPDIR"/packs_template.json)" --data-urlencode "version=$VTT_MCVER" | jq -r .link)
	# download pack
	[ "$packdl" != null ] && wget -q --show-progress -O "$VTD_TMPDIR/$name.zip" "https://vanillatweaks.net$packdl"
	[ "$packdl" != null ] && wget -q --show-progress -O "$VTD_TMPDIR/$name.zip" "$VTT_REMOTE$packdl"

	if [ -d "$outputdir" ] || mkdir "$outputdir"; then
		mv "$VTD_TMPDIR/$name.zip" "$outputdir"


@@ 71,16 74,16 @@ downloadFromFile() {
	# name of the pack to the correct category array in the packs_template.json
	# file.
	while read -r line; do
		jq -r --arg "pack" "$line" '.categories[] | select(.packs[].name | test($pack; "i")) | "\(.packs[] | select(.name | test($pack; "i")).name) \(.category)"' "$VTT_CACHE/rpcategories-$VTT_MCVER.json" | while read -r pack category; do
		jq -r --arg "pack" "$line" '.categories[] | select(.packs[].name | test($pack; "i")) | "\(.packs[] | select(.name | test($pack; "i")).name) \(.category)"' "$VTT_CACHE/$VTT_REMOTE/rpcategories-$VTT_MCVER.json" | while read -r pack category; do
			jq -r --arg "category" "$category" --arg "pack" "$pack" '.[$category] |= .+ [$pack]' "$VTD_TMPDIR/packs_template.json" >"$VTD_TMPDIR/packs_template_new.json"
			mv "$VTD_TMPDIR/packs_template_new.json" "$VTD_TMPDIR/packs_template.json"
		done
	done <"$@"

	# get download link
	packdl=$(curl -s "https://vanillatweaks.net/assets/server/zipresourcepacks.php" --data-urlencode "packs=$(cat "$VTD_TMPDIR"/packs_template.json)" --data-urlencode "version=$VTT_MCVER" | jq -r .link)
	packdl=$(curl -s "$VTT_REMOTE_ZIPPACKS" --data-urlencode "packs=$(cat "$VTD_TMPDIR"/packs_template.json)" --data-urlencode "version=$VTT_MCVER" | jq -r .link)
	# download pack
	[ "$packdl" != null ] && wget -q --show-progress -O "$VTD_TMPDIR/$name.zip" "https://vanillatweaks.net$packdl"
	[ "$packdl" != null ] && wget -q --show-progress -O "$VTD_TMPDIR/$name.zip" "$VTT_REMOTE$packdl"

	if [ -d "$outputdir" ] || mkdir "$outputdir"; then
		mv "$VTD_TMPDIR/$name.zip" "$outputdir"


@@ 92,14 95,14 @@ downloadFromFile() {
downloadSingle() {
	mkdir -p "$VTD_TMPDIR/$name"
	# get name and category of input
	jq -r '.categories[] | select(.packs[].name | test("^'"$1"'$"; "i")) | "\(.packs[] | select(.name | test("^'"$1"'$";"i")).name) \"\(.category)\""' "$VTT_CACHE/rpcategories-$VTT_MCVER.json" | while read -r name category; do
	jq -r '.categories[] | select(.packs[].name | test("^'"$1"'$"; "i")) | "\(.packs[] | select(.name | test("^'"$1"'$";"i")).name) \"\(.category)\""' "$VTT_CACHE/$VTT_REMOTE/rpcategories-$VTT_MCVER.json" | while read -r name category; do
		# dirty hack to generate json string for single pack
		packs="{$category:[\"$name\"]}"

		# get download link
		packdl=$(curl -s "https://vanillatweaks.net/assets/server/zipresourcepacks.php" --data-urlencode "packs=$packs" --data-urlencode "version=$VTT_MCVER" | jq -r .link)
		packdl=$(curl -s "$VTT_REMOTE_ZIPPACKS" --data-urlencode "packs=$packs" --data-urlencode "version=$VTT_MCVER" | jq -r .link)
		# download the pack if the link exists
		[ "$packdl" != null ] && wget -q --show-progress -O "$VTD_TMPDIR/$name.zip" "https://vanillatweaks.net$packdl"
		[ "$packdl" != null ] && wget -q --show-progress -O "$VTD_TMPDIR/$name.zip" "$VTT_REMOTE$packdl"

		if [ -z "$noReplaceImage" ]; then
			zip -q -j -u "$VTD_TMPDIR/$name.zip" "$VTT_TEMPLATEDIR/$VTT_MCVER/$name/pack.png"

M vts => vts +3 -2
@@ 1,5 1,6 @@
#!/bin/sh

VTT_REMOTE="${VTT_REMOTE:-vanillatweaks.net}"
VTT_CACHE="${XDG_CACHE_HOME:-$HOME/.cache}"
VTT_MCVER="${VTT_MCVER:-1.18}"



@@ 18,7 19,7 @@ usage() {
listall() {
	# selects all names and descriptions from all the packs in all the
	# categories, then pipes the output to column to format it nicely
	jq -r '.categories[].packs[] | "\(.name);\(.description)"' "$VTT_CACHE/rpcategories-$VTT_MCVER.json" | column -td -s ";" -N "name,desc" -T desc
	jq -r '.categories[].packs[] | "\(.name);\(.description)"' "$VTT_CACHE/$VTT_REMOTE/rpcategories-$VTT_MCVER.json" | column -td -s ";" -N "name,desc" -T desc
}

search() {


@@ 26,7 27,7 @@ search() {
	# could be neat to also print the category for each of these, but maybe only
	# do this in 'info'
	jq -r '.categories[].packs[] | select((.name | test("'"$1"'"; "i")) or (.description | test("'"$1"'"; "i"))) | "\(.name);\(.description)"' \
		"$VTT_CACHE/rpcategories-$VTT_MCVER.json" | column -td -s ";" -N "name,desc" -T desc
		"$VTT_CACHE/$VTT_REMOTE/rpcategories-$VTT_MCVER.json" | column -td -s ";" -N "name,desc" -T desc
}

[ -z "$1" ] && usage 1

M vtu => vtu +10 -8
@@ 1,5 1,8 @@
#!/bin/sh

VTT_PROTOCOL="${VTT_PROTOCOL:-https://}"
VTT_REMOTE="${VTT_REMOTE:-vanillatweaks.net}"
VTT_REMOTE_JSON="${VTT_REMOTE_JSON:-$VTT_PROTOCOL$VTT_REMOTE/assets/resources/json}"
VTT_CACHE="${XDG_CACHE_HOME:-$HOME/.cache}"
VTT_MCVER="${VTT_MCVER:-1.18}"



@@ 12,19 15,18 @@ usage() {
}

cleanjson() {
	printf "Removing all rpcategories files from %s\n" "$VTT_CACHE"
	rm "$VTT_CACHE"/rpcategories-*.json
	printf "Removing all rpcategories files from %s\n" "$VTT_CACHE/$VTT_REMOTE"
	rm -r "${VTT_CACHE:-?}/$VTT_REMOTE"
}

update() {
	printf "Attempting to download vanillatweaks pack information for %s\n" "$VTT_MCVER"
	printf "Attempting to download vanillatweaks pack information for %s from %s\n" "$VTT_MCVER" "$VTT_PROTOCOL$VTT_REMOTE"

	if curl -s -S -R -f -o \
		"$VTT_CACHE/rpcategories-$VTT_MCVER.json" \
		"https://vanillatweaks.net/assets/resources/json/$VTT_MCVER/rpcategories.json"; then
		printf "Successfully downloaded rpcategories.json for %s\n" "$VTT_MCVER"
	mkdir -p "$VTT_CACHE/$VTT_REMOTE"
	if curl -s -S -R -f -o "$VTT_CACHE/$VTT_REMOTE/rpcategories-$VTT_MCVER.json" "$VTT_REMOTE_JSON/$VTT_MCVER/rpcategories.json"; then
		printf "Successfully downloaded rpcategories.json for %s from %s\n" "$VTT_MCVER" "$VTT_PROTOCOL$VTT_REMOTE"
	else
		printf "Failed to download rpcategories.json for %s\n" "$VTT_MCVER"
		printf "Failed to download rpcategories.json for %s from %s\n" "$VTT_MCVER" "$VTT_PROTOCOL$VTT_REMOTE"
	fi
}