Patch-ID: bash40-021
[bash.git] / examples / scripts.v2 / arc2tarz
blob285bede473eaf51a1e3278c75ffbd907a23575e3
1 #! /bin/bash
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
9 # conversion to bash v2 syntax by Chet Ramey
11 unset ENV
12 Usage="Usage: $0 arcfile [-hcg] [ tarzfile ]"
14 phelp()
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."
27 compress=gzip
28 ext=gz
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
39 shift $((OPTIND - 1))
41 if [ $# = 0 ]; then
42 phelp
43 exit 0
46 [ -z "$TMP" ] && tmpdir=/tmp/arc2tarz.$$ || tmpdir=$TMP/arc2tarz.$$
48 case "$1" in
49 *.arc) arcfile=$1 ;;
50 *) arcfile=$1.arc ;;
51 esac
53 if [ ! -f $arcfile ] || [ ! -r $arcfile ]; then
54 echo "Could not open arc file \"$arcfile\"."
55 exit 1
58 case "$arcfile" in
59 /*) ;;
60 *) arcfile=$PWD/$arcfile ;;
61 esac
63 basename=${arcfile%.arc}
64 basename=${basename##*/}
65 [ $# -lt 2 ] && tarzname=$PWD/$basename.tar.$ext || tarzname=$2
67 trap 'rm -rf $tmpdir $tarzname' 1 2 3 6 15
69 mkdir $tmpdir
70 cd $tmpdir
71 echo "unarcing files..."
72 arc -ie $arcfile
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
80 done
82 echo "tarring/compressing files..."
83 tar cf - * | $compress > $tarzname
84 cd -
85 rm -rf $tmpdir