Upload Tizen:Base source
[external/bash.git] / examples / scripts.v2 / arc2tarz
1 #! /bin/bash
2 #
3 # original from:
4 # arc2tarz: convert arced file to tarred, compressed form.
5 # @(#) arc2tarz.ksh 1.0 92/02/16
6 # 91/03/28 john h. dubois iii (john@armory.com)
7 # 92/02/16 added -h option for help
8 #
9 # conversion to bash v2 syntax by Chet Ramey
10
11 unset ENV
12 Usage="Usage: $0 arcfile [-hcg] [ tarzfile ]"
13
14 phelp()
15 {
16 echo "$Usage
17 arcfile is the name of an arc file to convert to tarred, compressed form. 
18 The file must have a .arc extension, but only the base name needs to be
19 given.  If no output file name is given, it will be created in the current
20 directory with the name being the arcfile basename followed by .tar.EXT.
21 If the -c option is given, compress will be used, and EXT will be Z.
22 The default (also available with -g) is to use gzip, in which case EXT
23 is gz.  If the basename is too long the extension may be truncated.  All
24 uppercase letters in the names of files in the archive are moved to lowercase."
25 }
26
27 compress=gzip
28 ext=gz
29
30 while getopts "hcg" opt; do
31         case "$opt" in
32         h)      phelp; exit 0;;
33         c)      compress=compress; ext=Z;;
34         g)      compress=gzip ; ext=gz ;;
35         *)      echo "$Usage" 1>&2 ; exit 2;;
36         esac
37 done
38
39 shift $((OPTIND - 1))
40
41 if [ $# = 0 ]; then
42         phelp
43         exit 0
44 fi
45
46 [ -z "$TMP" ] && tmpdir=/tmp/arc2tarz.$$ || tmpdir=$TMP/arc2tarz.$$
47
48 case "$1" in
49 *.arc)  arcfile=$1 ;;
50 *)      arcfile=$1.arc ;;
51 esac
52
53 if [ ! -f $arcfile ] || [ ! -r $arcfile ]; then
54         echo "Could not open arc file \"$arcfile\"."
55         exit 1
56 fi
57
58 case "$arcfile" in
59 /*)     ;;
60 *)      arcfile=$PWD/$arcfile ;;
61 esac
62
63 basename=${arcfile%.arc}
64 basename=${basename##*/}
65 [ $# -lt 2 ] && tarzname=$PWD/$basename.tar.$ext || tarzname=$2
66
67 trap 'rm -rf $tmpdir $tarzname' 1 2 3 6 15
68
69 mkdir $tmpdir
70 cd $tmpdir
71 echo "unarcing files..."
72 arc -ie $arcfile
73
74 # lowercase
75 for f in *; do
76         new=$(echo $f | tr A-Z a-z)
77         if [ "$f" != "$new" ]; then
78                 mv $f $new
79         fi
80 done
81
82 echo "tarring/compressing files..."
83 tar cf - * | $compress > $tarzname
84 cd -
85 rm -rf $tmpdir