stargazing/notwa-util/acquire
2022-09-23 12:29:57 -07:00

32 lines
1.2 KiB
Bash

#!/usr/bin/env sh
: \
&& die() \
{ : \
&& printf '\033[1m%s:\033[m %s\n' acquire "$@" \
&& exit 64 \
;} \
&& { [ -d "/tmp" ] || die '/tmp must be mounted (or just exist)' ;} \
&& { [ -n "$dest" ] || die 'dest must be set' ;} \
&& { [ -n "$local_fn" ] || die 'local_fn must be set' ;} \
&& { [ -n "$remote_fn" ] || die 'remote_fn must be set' ;} \
&& { [ -n "$remote_url" ] || die 'remote_url must be set' ;} \
&& { [ -n "$sha256" ] || die 'sha256 must be set' ;} \
&& { : \
&& printf '%s %s\n' "$sha256" "$local_fn" | sha256sum -c - >/dev/null 2>&1 \
|| wget -q "$remote_url" -O "$local_fn" \
&& printf '%s %s\n' "$sha256" "$local_fn" | sha256sum -c - \
|| { sha256sum "$local_fn"; exit 1 ;} \
;} \
&& temp="$(mktemp -d)" \
&& tar -axof "$local_fn" -C "$temp" \
&& find "$temp" -mindepth 1 -maxdepth 1 >/tmp/files \
&& find "$temp" -mindepth 1 -maxdepth 1 -type d >/tmp/dirs \
&& { [ -d "$dest" ] || mkdir "$dest" ;} \
&& if [ "$(wc -l </tmp/files)" = 1 ] && [ "$(wc -l </tmp/dirs)" = 1 ] \
;then : \
&& find "$temp" -mindepth 2 -maxdepth 2 -exec cp -rp {} "$dest" + \
;else : \
&& cp -rp "$temp" "$dest" \
;fi \
;