Update soundtest
[grml-scripts.git] / usr_bin / fex
1 #!/bin/zsh
2 # Filename:      fex
3 # Purpose:       extract archives via smart frontend
4 # Authors:       grml-team ( grml.org), (c) Matthias Kopfermann, (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Sam Nov 25 21:37:22 CET 2006 [mika]
8 ################################################################################
9
10 zmodload zsh/files
11 autoload -U colors && colors
12 emulate zsh
13
14 FILENAME=$0
15 ARGV=$1
16 alarmcol=$fg_bold[red]
17 defcol=$fg[default]$bg[default]
18 col1=$fg_bold[green]
19 col2=$fg_bold[magenta]
20 col3=$fg_bold[cyan]
21 col4=$fg[cyan]
22
23 usage() { print "Usage: $FILENAME file_to_extract" && exit  ; }
24  (( $ARGC != 1 )) && usage
25  [[ $ARGV == "-h" || $ARGV == "--help" ]] && usage
26  file=$1
27  FILEPRG=$( which file )
28  [[ $( tar --version 3>&1 ) != *(GNU|star)* ]] && echo "sorry, GNU tar or star is needed" && exit
29  test -z $FILEPRG && echo $0 needs a program named file to detect the filetype && exit
30
31 qprompt="
32         Really decompress $alarmcol${file}$defcol? Press y or Y for YES
33         or any other key for NO $defcol
34 "
35
36 nothing_to_do="
37         ${col1}Okay, I will not uncompress the file $col2${file}$defcol as requested.
38 "
39 decision="read -q '?$qprompt'"
40
41 local -A filetype; filetype=(\
42         bz2cpio "cpio archive *bzip2*"
43         cpiogz  "cpio archive *gzip*"
44         tarbz2  "*tar archive*bzip2*"
45         targz   "*tar archive*gzip*"
46         tarz    "*POSIX tar archive \(compress\'d data 16 bits\)*"
47         bz2     "*\(bzip2 compressed data*"
48         tar     "*tar*"
49         gz      "*gzip*"
50         7zip    "*7-zip archive data*"
51         arj     "ARJ archive data*"
52         zip     "*Zip archive data*"
53         Z       "*compress'd data 16 bits*"
54         deb     "Debian binary package*"
55         uu      "*uuencoded*xxencoded*"
56         lzo     "lzop compressed data - *"
57         cpio    "cpio archive"
58         rar     "RAR archive data*"
59         cab     "CAB file"
60         #rpm    "$file: *RPM*"
61 )
62
63 tarlist='/usr/bin/env tar -tvf'                 ; tarextract='/usr/bin/env tar -C $whereto -xvf'
64 tarbz2list='/usr/bin/env tar -tvjf'             ; tarbz2extract='/usr/bin/env tar -C ${whereto:-$PWD} -xvjf'
65 targzlist='/usr/bin/env tar -tvzf'              ; targzextract='/usr/bin/env tar -C $whereto -xvzf'
66 gzlist='/usr/bin/env gzip -lv'                  ; gzextract='/usr/bin/env gzip -dc'
67 tarzlist='/usr/bin/env tar -tvZf'               ; tarzextract='/usr/bin/env tar -C $whereto -xvZf'
68 tarcompresslist='/usr/bin/env tar -tvZf'        ; tarcompressextract='/usr/bin/env tar -$ whereto -xvZf'
69 #cpiobz2list='bzip2 -cd $file | cpio -ivt'; cpiobz2extract='bzip2 -cd $CWD/$file  | cpio -idv'
70 #cpiogzlist='gzip -cd $file | cpio -tv'
71 #cpiogzextract='gzip -cd $file | cpio -ivd'
72 cpiogz='/usr/bin/env star tzvf'                 ; cpiogzextract='/usr/bin/env star xzvf'
73 _7ziplist='/usr/bin/env 7zr l'                  ; _7zipextract='/usr/bin/env 7zr e -o$whereto'
74 arjlist='/usr/bin/env arj l'                    ; arjextract='/usr/bin/env arj e -e $CWD/$file'
75 compresslist='/usr/bin/env uncompress -l'       ; compressextract='/usr/bin/env uncompress -cv'
76 lhalist='/usr/bin/env lha -l'                   ; lhaextract='/usr/bin/env lha -xw=$whereto'
77 deblist='/usr/bin/env dpkg-deb -c'              ; debextract='/usr/bin/env dpkg -X'
78 lzolist='/usr/bin/env lzop -l'                  ; lzoextract='/usr/bin/env lzop -dN -p$whereto'
79 cpiolist='/usr/bin/env cpio -t'                 ; cpioextract='/usr/bin/env cpio -idv'
80 rarlist='/usr/bin/env unrar lb'                 ; rarextract='/usr/bin/env unrar e'
81 cablist='/usr/bin/env cabextract -l'            ; cabextract='/usr/bin/env cabextract -d $whereto'
82 #rpmlist='/usr/bin/env rpm2cpio  #rpmextract=
83 ziplist='/usr/bin/env unzip -l'                 ; zipextract='/usr/bin/env unzip -d $whereto'
84
85
86 # ------------------------------------------------- functions
87 put_there() {
88         vared -p "
89         ${col2}Where should I extract to?
90         ${col4}( Non existing directories will be created,
91         completion works as do usual editing commands )$defcol
92         " whereto  && test -d $whereto \
93         || builtin mkdir -p $whereto
94 }
95 # -------------------------------------------end of functions
96
97 ((  ${+PAGER} )) || local PAGER=less
98
99
100  if [[ -f $file ]]
101  then
102         filetest=$( $FILEPRG -bz $file )
103         echo "This is a $col2$filetest$defcol"
104         sleep 1
105  whereto=$PWD
106          case "$filetest" in
107           ( ${~filetype[bz2cpio]} )
108                 $=cpiobz2list  |&  $PAGER && eval $decision \
109                 && put_there \
110                 && CWD=$PWD \
111                 && ( cd $whereto &&  print "${col1}extracting to $whereto$defcol" && ${(e)=cpiobz2extract}  ) \
112                 || print $nothing_to_do ;;
113          ( ${~filetype[cpiogz]} )
114                 ${(e)=cpiogzlist}  |  $PAGER && eval $decision \
115                 && put_there \
116                 && CWD=$PWD \
117                 && ( cd $whereto &&  print "${col1}extracting to $whereto$defcol" && ${(e)=cpiogzextract}  ) \
118                 || print $nothing_to_do ;;
119          ( ${~filetype[tarbz2]} )
120                 $=tarbz2list $file \
121                        |
122                      $PAGER && eval $decision && put_there \
123                      && ${(e)=tarbz2extract} $file \
124                       || print $nothing_to_do ;;
125          ( ${~filetype[targz]} )
126                 $=targzlist $file  | $PAGER && eval $decision \
127                 && put_there \
128                 && ${(e)=targzextract} $file \
129                 || print $nothing_to_do ;;
130          ( ${~filetype[tarz]} )
131                 $=tarzlist $file  | $PAGER && eval $decision \
132                 && put_there \
133                 && print "${col3}extracting to $whereto:$defcol" \
134                 && ${(e)=tarzextract} $file \
135                 || print $nothing_to_do ;;
136          ( ${~filetype[bz2]} )
137                 bzip2 -tv $file  | $PAGER && eval $decision \
138                 && put_there \
139                 && bzip2 -dc $file > $whereto/${file:t:r} \
140                 && print "extracting to $whereto/${file:t:r}" || print $nothing_to_do ;;
141          ( ${~filetype[gz]} )
142                 $=gziplist  $file | $PAGER && eval $decision \
143                 && put_there \
144                 && dest="$whereto/${file:t:r}" \
145                 && gzip -dc $file > $dest && print "extracting to $dest" || print $nothing_to_do ;;
146          ( ${~filetype[tar]} )
147                 $=tarlist $file | $PAGER && eval $decision \
148                 && put_there \
149                 && ${(e)=tarextract} $file || print $nothing_to_do ;; #ok
150          ( ${~filetype[7zip]} )
151                 $=_7ziplist $file | $PAGER && eval $decision \
152                 && put_there \
153                 && ${(e)=_7zipextract} $file || print $nothing_to_do ;; # -o cannot stand a space, attention! #ok
154          ( ${~filetype[arj]} )
155                 $=arjlist $file | $PAGER && eval $decision \
156                 && put_there \
157                 && CWD=$PWD \
158                 && ( cd $whereto && arj e -e $CWD/$file ) || print $nothing_to_do ;;
159          ( ${~filetype[zip]} )
160                 ${=ziplist} $file | $PAGER && eval $decision \
161                 && put_there \
162                 && ${(e)=zipextract} $file || print $nothing_to_do ;; #ok
163          ( ${~filetype[Z]} )
164                 $=compresslist $file | $PAGER && eval $decision \
165                 && put_there \
166                 && dest="$whereto/${file:t:r}" \
167                 && ${(e)=compressextract} $file > $dest && print "extracting to $dest" || print $nothing_to_do ;; #ok
168          ( ${~filetype[lha]} )
169                 $=lhalist $file   | $PAGER && eval $decision \
170                 && put_there \
171                 && ${(e)=lhaextract} $file || print $nothing_to_do ;; #ok
172          ( ${~filetype[rar]} )
173                  $=rarlist $file | $PAGER && eval $decision \
174                 && put_there \
175                 && CWD=$PWD \
176                 && (cd $whereto && ${(e)=rarextract} $CWD/$file ) || print $nothing_to_do ;;  #ok
177          ( ${~filetype[lzo]} )
178                 $=lzolist $file | $PAGER && eval $decision \
179                 && put_there \
180                 && ${(e)=lzoextract} $file || print $nothing_to_do ;; #ok
181          ( ${~filetype[cpio]} )
182                 $=cpiolist < $file | $PAGER && eval $decision \
183                 && put_there \
184                 && CWD=$PWD \
185                 && ( cd $whereto &&  ${(e)=cpioextract} < $CWD/$file ) || print $nothing_to_do ;; #ok
186          ( ${~filetype[deb]} )
187                 ${(e)=deblist} $file | $PAGER && eval $decision \
188                 && put_there \
189                 && ${(e)=debextract} $file $whereto || print $nothing_to_do ;; #ok
190          ( ${~filetype[uu]} )
191                 put_there \
192                 && CWD=$PWD \
193                 && ( cd $whereto && uudecode $CWD/$file ) || print $nothing_to_do ;;
194          ( ${~filetype[cab]} )
195                 $=cablist $file | $PAGER && eval $decision \
196                 put_there \
197                 &&  ${(e)=cabextract} $file || print $nothing_to_do ;;
198         #( ${~filetype[rpm]} )
199         #       temp=$( dd if=/dev/urandom bs=1 count=30 2>/dev/null | uuencode - | tr -d '[[:punct:]]' | sed '2!d' )
200         #       temp=${(C)${${${(f)"$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | uuencode - 2>/dev/null)"}[2]}//[[:punct:]]}}
201         # (cd $temp &&  rpm2cpio $file && CPIO=*.cpio && cpio -ivt < $CPIO )\
202         #       put_there \
203         #
204         #       && ( cd $whereto && cpio -ivd < $temp/$file ) || print $nothing_to_do
205         #       rm -r $temp ;;
206          ( * )
207                 echo "${alarmcol}Error. Not the expected arguments or other problem!$defcol"
208                 echo "Usage: $0 file" ; exit 1 ;;
209          esac
210  else
211          echo "'$file' is not a valid file"
212  fi
213
214 ## END OF FILE #################################################################