2d5b6b1b79baeebb872c72f33b56765b5efc083a
[feisty_meow.git] / scripts / archival / list_arch.sh
1 #!/bin/bash
2
3 ##############
4 # Name   : list_arch
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 lister, although really we are mainly supporting
14 # tar and zip currently, including compressed formats.
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 "lists 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
27 if [ -z "$PAGER" ]; then
28   PAGER=$(which less)
29   if [ -z "$PAGER" ]; then
30     PAGER=$(which more)
31     if [ -z "$PAGER" ]; then
32       PAGER="cat"
33     fi
34   fi
35 fi
36
37 # save where we started out.
38 ORIGINATING_FOLDER="$( \pwd )"
39
40 if [ ! -f "$archive_file" ]; then
41   # we're assuming we left it behind in our previous directory.
42   archive_file="$ORIGINATING_FOLDER/$archive_file"
43   if [ ! -f "$archive_file" ]; then
44     echo "Could not find file to unpack after shifting directories.  Sorry."
45     echo "Tried to locate it as: $archive_file"
46     exit 1
47   fi
48 fi
49
50 save_err=1
51 if [[ $archive_file =~ .*\.tar$ \
52     || $archive_file =~ .*\.tar\.gz$ \
53     || $archive_file =~ .*\.tar\.bz2$ \
54     || $archive_file =~ .*\.iar$ \
55     || $archive_file =~ .*\.oar$ \
56     || $archive_file =~ .*\.tgz$ \
57     || $archive_file =~ .*\.ova$ \
58     || $archive_file =~ .*\.snarf$ \
59     ]]; then
60   tar -tf $archive_file | $PAGER
61   save_err=${PIPESTATUS[0]}
62 elif [[ $archive_file =~ .*\.zip$ \
63     || $archive_file =~ .*\.epub$ \
64     || $archive_file =~ .*\.odt$ \
65     || $archive_file =~ .*\.jar$ \
66     || $archive_file =~ .*\.war$ \
67     ]]; then
68   unzip -v $archive_file | $PAGER
69   save_err=${PIPESTATUS[0]}
70 elif [[ "$archive_file" =~ .*\.7z$ ]]; then
71   7z l "$archive_file" | $PAGER
72   save_err=${PIPESTATUS[0]}
73 elif [[ "$archive_file" =~ .*\.rar$ ]]; then
74   rar l "$archive_file" | $PAGER
75   save_err=${PIPESTATUS[0]}
76 fi
77
78 if [ $save_err -ne 0 ]; then
79   echo "There was a failure reported while listing: $archive_file"
80   exit 1
81 fi
82