aboutsummaryrefslogtreecommitdiff
path: root/snag/scripts
diff options
context:
space:
mode:
authorJack Jamison <jackqjamison@gmail.com>2026-07-24 10:35:44 -0400
committerJack Jamison <jackqjamison@gmail.com>2026-07-24 11:23:08 -0400
commit17d0447a55b971f66e383e5f3b0c12213949dbc9 (patch)
tree3233ed04bb362ddb502dd391fa26b1d28939156c /snag/scripts
parentcfcc59d16d7dd59a4c6470e6c94289f453d7dbb3 (diff)
unpack source function unpacks tarballs (very hard)
Diffstat (limited to 'snag/scripts')
-rwxr-xr-xsnag/scripts/extract_tar_contents.sh38
1 files changed, 38 insertions, 0 deletions
diff --git a/snag/scripts/extract_tar_contents.sh b/snag/scripts/extract_tar_contents.sh
new file mode 100755
index 0000000..a50a1a7
--- /dev/null
+++ b/snag/scripts/extract_tar_contents.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+set -e
+
+# extracts a tarball's contents to the output directory.
+# if the tarball contains just one directory, extract those contents
+#
+# we can rewrite this script in c eventually, if we want
+
+if [ -z "$1" -o -z "$2" ]; then
+ echo "Usage: $0 <tarball> <output_directory>" >&2
+ exit 1
+fi
+
+tarball="$1"
+target="$2"
+
+is_one_directory() {
+ if [ $(echo $1 | wc -l) -ne 1 ]; then
+ return 1
+ fi
+ case "$1" in
+ */) return 0 ;;
+ *) return 1 ;;
+ esac
+}
+
+# get top level paths in tarball
+tlps=$(tar -tf "$tarball" | grep -E '^(./)?[^/]+/?$')
+
+if is_one_directory "$tlps"; then
+ # if only one top level directory, extract it and rename it
+ tar xf "$tarball"
+ mv "$tlps" "$target"
+else
+ # if a bunch of files or directories, extract them in a directory
+ mkdir $target
+ tar xf "$tarball" -C "$target"
+fi