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