added tbz extension for tar bzip2
[feisty_meow.git] / scripts / archival / unpack.sh
1 #!/bin/bash
2
3 ##############
4 # Name   : unpack
5 # Author : Chris Koeritz
6 # Rights : Copyright (C) 2012-$now by Feisty Meow Concerns, Ltd.
7 ##############
8 # This script is free software; you can modify/redistribute it under the terms
9 # of the GNU General Public License. [ http://www.gnu.org/licenses/gpl.html ]
10 # Feel free to send updates to: [ fred@gruntose.com ]
11 ##############
12 #
13 # An arbitrary format archive unpacker, although really we are mainly supporting
14 # tar, zip, 7z, and rar at this time.
15
16 archive_file="$1"; shift
17 if [ -z "$archive_file" ]; then
18   echo "This script takes one archive name (in .tar.gz, .zip, etc. formats) and"
19   echo "unpacks the archive with the appropriate tool."
20   exit 1
21 fi
22 if [ ! -f "$archive_file" ]; then
23   echo "The file specified cannot be located: $archive_file"
24   exit 1
25 fi
26 unpack_dir="$1"; shift
27 if [ -z "$unpack_dir" ]; then
28   all_but_last="$(echo "$(basename "$archive_file")" | sed -e 's/\([\^.]*\)\.[^\.]*$/\1/')"
29   unpack_dir="arch_${all_but_last}"
30 fi
31
32 if [ ! -d "$unpack_dir" ]; then
33   mkdir -p "$unpack_dir"
34   if [ $? -ne 0 ]; then
35     echo "Could not create the unpacking directory: $unpack_dir"
36     exit 1
37   fi
38 fi
39
40 # save where we started out.
41 ORIGINATING_FOLDER="$( \pwd )"
42
43 pushd "$unpack_dir" &>/dev/null
44
45 if [ ! -f "$archive_file" ]; then
46   # we're assuming we left it behind in our previous directory.
47   archive_file="$ORIGINATING_FOLDER/$archive_file"
48   if [ ! -f "$archive_file" ]; then
49     echo "Could not find file to unpack after shifting directories.  Sorry."
50     echo "Tried to locate it as: $archive_file"
51     exit 1
52   fi
53 fi
54
55 #hmmm: we could log to a file and spew the file if there's a failure, then
56 #  remove the file after spewing or after successful run.
57 #  this is a really commonly repeated pattern that would be nice to support
58 #  in general.
59
60 # record what happened.
61 save_err=1
62 if [[ "$archive_file" =~ .*\.tar$ \
63     || "$archive_file" =~ .*\.tar\.gz$ \
64     || "$archive_file" =~ .*\.tar\.bz2$ \
65     || "$archive_file" =~ .*\.tar\.xz$ \
66     || "$archive_file" =~ .*\.iar$ \
67     || "$archive_file" =~ .*\.oar$ \
68     || "$archive_file" =~ .*\.tbz$ \
69     || "$archive_file" =~ .*\.tgz$ \
70     || "$archive_file" =~ .*\.txz$ \
71     || "$archive_file" =~ .*\.ova$ \
72     || "$archive_file" =~ .*\.snarf$ \
73     ]]; then
74   tar -xf "$archive_file" &>/dev/null
75   save_err=$?
76 elif [[ "$archive_file" =~ .*\.zip$ \
77     || "$archive_file" =~ .*\.epub$ \
78     || "$archive_file" =~ .*\.odt$ \
79     || "$archive_file" =~ .*\.jar$ \
80     || "$archive_file" =~ .*\.war$ \
81     ]]; then
82   unzip "$archive_file" &>/dev/null
83   save_err=$?
84 elif [[ "$archive_file" =~ .*\.7z$ ]]; then
85   7z x "$archive_file" &>/dev/null
86   save_err=$?
87 elif [[ "$archive_file" =~ .*\.rar$ ]]; then
88   unrar x "$archive_file" &>/dev/null
89   save_err=$?
90 fi
91
92 popd &>/dev/null
93
94 if [ $save_err -ne 0 ]; then
95   echo "There was a failure reported while unpacking: $archive_file"
96   echo "into the directory: $unpack_dir"
97   exit 1
98 else
99   echo "Unpacked file $(basename "$archive_file") into folder: $unpack_dir"
100 fi
101
102