X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=scripts%2Farchival%2Funpack.sh;h=04ce990bc0cb13db6957b4556ed0162649b7cf6a;hb=c23e331e8872360bb47049f0da925e0a2c6fd9df;hp=303937299475f38bb6335b4ca9e092c3870601e6;hpb=af22dabd2f3978f2d14d1abb44956a990c0edddb;p=feisty_meow.git diff --git a/scripts/archival/unpack.sh b/scripts/archival/unpack.sh index 30393729..04ce990b 100644 --- a/scripts/archival/unpack.sh +++ b/scripts/archival/unpack.sh @@ -11,25 +11,26 @@ ############## # # An arbitrary format archive unpacker, although really we are mainly supporting -# tar and zip currently, including compressed formats. +# tar, zip, 7z, and rar at this time. -unpack_file="$1"; shift -if [ -z "$unpack_file" ]; then +archive_file="$1"; shift +if [ -z "$archive_file" ]; then echo "This script takes one archive name (in .tar.gz, .zip, etc. formats) and" echo "unpacks the archive with the appropriate tool." exit 1 fi -if [ ! -f "$unpack_file" ]; then - echo "The file specified for unpacking cannot be located: $unpack_file" +if [ ! -f "$archive_file" ]; then + echo "The file specified cannot be located: $archive_file" exit 1 fi unpack_dir="$1"; shift if [ -z "$unpack_dir" ]; then - unpack_dir=$(echo unpacked_$(basename $unpack_file) | sed -e 's/^\([^\.]*\)\..*/\1/') + all_but_last="$(echo "$(basename "$archive_file")" | sed -e 's/\([\^.]*\)\.[^\.]*$/\1/')" + unpack_dir="arch_${all_but_last}" fi if [ ! -d "$unpack_dir" ]; then - mkdir "$unpack_dir" + mkdir -p "$unpack_dir" if [ $? -ne 0 ]; then echo "Could not create the unpacking directory: $unpack_dir" exit 1 @@ -41,37 +42,60 @@ ORIGINATING_FOLDER="$( \pwd )" pushd "$unpack_dir" &>/dev/null -if [ ! -f "$unpack_file" ]; then +if [ ! -f "$archive_file" ]; then # we're assuming we left it behind in our previous directory. - unpack_file="$ORIGINATING_FOLDER/$unpack_file" - if [ ! -f "$unpack_file" ]; then + archive_file="$ORIGINATING_FOLDER/$archive_file" + if [ ! -f "$archive_file" ]; then echo "Could not find file to unpack after shifting directories. Sorry." - echo "Tried to locate it as: $unpack_file" + echo "Tried to locate it as: $archive_file" exit 1 fi fi -if [[ $unpack_file =~ .*\.tar$ \ - || $unpack_file =~ .*\.tar\.gz$ \ - || $unpack_file =~ .*\.tar\.bz2$ \ - || $unpack_file =~ .*\.tgz$ ]]; then - tar -xf $unpack_file &>/dev/null -elif [[ $unpack_file =~ .*\.zip$ \ - || $unpack_file =~ .*\.jar$ \ - || $unpack_file =~ .*\.iar$ \ - || $unpack_file =~ .*\.oar$ ]]; then - unzip $unpack_file &>/dev/null +#hmmm: we could log to a file and spew the file if there's a failure, then +# remove the file after spewing or after successful run. +# this is a really commonly repeated pattern that would be nice to support +# in general. + +# record what happened. +save_err=1 +if [[ "$archive_file" =~ .*\.tar$ \ + || "$archive_file" =~ .*\.tar\.gz$ \ + || "$archive_file" =~ .*\.tar\.bz2$ \ + || "$archive_file" =~ .*\.tar\.xz$ \ + || "$archive_file" =~ .*\.iar$ \ + || "$archive_file" =~ .*\.oar$ \ + || "$archive_file" =~ .*\.tgz$ \ + || "$archive_file" =~ .*\.txz$ \ + || "$archive_file" =~ .*\.ova$ \ + || "$archive_file" =~ .*\.snarf$ \ + ]]; then + tar -xf "$archive_file" &>/dev/null + save_err=$? +elif [[ "$archive_file" =~ .*\.zip$ \ + || "$archive_file" =~ .*\.epub$ \ + || "$archive_file" =~ .*\.odt$ \ + || "$archive_file" =~ .*\.jar$ \ + || "$archive_file" =~ .*\.war$ \ + ]]; then + unzip "$archive_file" &>/dev/null + save_err=$? +elif [[ "$archive_file" =~ .*\.7z$ ]]; then + 7z x "$archive_file" &>/dev/null + save_err=$? +elif [[ "$archive_file" =~ .*\.rar$ ]]; then + unrar x "$archive_file" &>/dev/null + save_err=$? fi -save_err=$? popd &>/dev/null if [ $save_err -ne 0 ]; then - echo "There was a failure reported while unpacking: $unpack_file" + echo "There was a failure reported while unpacking: $archive_file" echo "into the directory: $unpack_dir" exit 1 else - echo "Unpacked file $(basename $unpack_file) into folder: $unpack_dir" + echo "Unpacked file $(basename "$archive_file") into folder: $unpack_dir" fi