Merge branch 'release-2.140.122'
[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 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
17
18 archive_file="$1"; shift
19 if [ -z "$archive_file" ]; then
20   echo "This script takes one archive name (in .tar.gz, .zip, etc. formats) and"
21   echo "lists the archive with the appropriate tool."
22   exit 1
23 fi
24 if [ ! -f "$archive_file" ]; then
25   echo "The file specified cannot be located: $archive_file"
26   exit 1
27 fi
28
29 if [ -z "$PAGER" ]; then
30   PAGER=$(whichable less)
31   if [ -z "$PAGER" ]; then
32     PAGER=$(whichable more)
33     if [ -z "$PAGER" ]; then
34       PAGER="cat"
35     fi
36   fi
37 fi
38
39 # save where we started out.
40 ORIGINATING_FOLDER="$( \pwd )"
41
42 if [ ! -f "$archive_file" ]; then
43   # we're assuming we left it behind in our previous directory.
44   archive_file="$ORIGINATING_FOLDER/$archive_file"
45   if [ ! -f "$archive_file" ]; then
46     echo "Could not find file to unpack after shifting directories.  Sorry."
47     echo "Tried to locate it as: $archive_file"
48     exit 1
49   fi
50 fi
51
52 save_err=1
53 if [[ $archive_file =~ .*\.tar$ \
54     || $archive_file =~ .*\.tar\.gz$ \
55     || $archive_file =~ .*\.tar\.bz2$ \
56     || $archive_file =~ .*\.iar$ \
57     || $archive_file =~ .*\.oar$ \
58     || $archive_file =~ .*\.tgz$ \
59     || $archive_file =~ .*\.ova$ \
60     || $archive_file =~ .*\.snarf$ \
61     ]]; then
62   tar -tf $archive_file | $PAGER
63   save_err=${PIPESTATUS[0]}
64 elif [[ $archive_file =~ .*\.zip$ \
65     || $archive_file =~ .*\.epub$ \
66     || $archive_file =~ .*\.odt$ \
67     || $archive_file =~ .*\.jar$ \
68     || $archive_file =~ .*\.war$ \
69     ]]; then
70   unzip -v $archive_file | $PAGER
71   save_err=${PIPESTATUS[0]}
72 elif [[ "$archive_file" =~ .*\.7z$ ]]; then
73   7z l "$archive_file" | $PAGER
74   save_err=${PIPESTATUS[0]}
75 elif [[ "$archive_file" =~ .*\.rar$ ]]; then
76   rar l "$archive_file" | $PAGER
77   save_err=${PIPESTATUS[0]}
78 fi
79
80 if [ $save_err -ne 0 ]; then
81   echo "There was a failure reported while listing: $archive_file"
82   exit 1
83 fi
84