1#!/bin/sh
2
3# emulate-gnu-tar -- emulate the options of GNU tar that librep uses
4# in its tar-file handling code.
5
6# $Id$
7
8compression_mode=""
9command=""
10tarfile=""
11to_stdout=""
12
13version="1.0"
14
15original_directory=`pwd`
16
17usage () {
18  cat <<EOF
19usage: emulate-gnu-tar [OPTIONS..] COMMAND
20
21Supported options include:
22
23	--compress
24	--gzip
25	--bzip2
26	--xz
27	--lzma
28
29Supported commands include:
30
31	--version
32	--list --file TARFILE --verbose
33	--extract --file TARFILE -C DIR
34	--extract --file TARFILE --to-stdout FILE
35
36EOF
37}
38
39absolutify () {
40  case "$1" in
41    /*)
42      echo $1
43      ;;
44    *)
45      echo "$original_directory/$1"
46      ;;
47  esac
48}
49
50while [ x"$1" != x ]; do
51  case $1 in
52    --version)
53      cat <<EOF
54tar (GNU tar) $version
55This isn't really GNU tar. It's just a wrapper script used by librep to
56make proprietary tars look somewhat like GNU tar. Don't use it.
57EOF
58      exit 0
59      ;;
60
61    --compress)
62      compression_mode=compress
63      ;;
64
65    --gzip)
66      compression_mode=gzip
67      ;;
68
69    --bzip2)
70      compression_mode=bzip2
71      ;;
72
73    --xz)
74      compression_mode=xz
75      ;;
76
77    --lzma)
78      compression_mode=lzma
79      ;;
80
81    --file)
82      tarfile=`absolutify "$2"`
83      shift
84      ;;
85
86    --verbose)
87      ;;
88
89    -C)
90      cd "$2"
91      shift
92      ;;
93
94    --to-stdout)
95      to_stdout=$2
96      shift
97      ;;
98
99    --extract)
100      command=extract
101      ;;
102
103    --list)
104      command=list
105      ;;
106
107    *)
108      echo "unknown option: $1" >&2
109      exit 1
110      ;;
111  esac
112  shift
113done
114
115if [ "x$command" = x ]; then
116  usage
117  exit 1
118fi
119
120case "$compression_mode" in
121  gzip)
122    input="gzip -d -c \"$tarfile\" |"
123    ;;
124
125  compress)
126    input="compress -d -c \"$tarfile\" |"
127    ;;
128
129  bzip2)
130    input="bzip2 -d -c \"$tarfile\" |"
131    ;;
132
133  xz)
134    input="xz -d -c \"$tarfile\" |"
135    ;;
136
137  lzma)
138    input="lzma -d -c \"$tarfile\" |"
139    ;;
140
141  *)
142    input="cat \"$tarfile\" |"
143    ;;
144esac
145
146case "$command" in
147  list)
148    eval "$input tar tvf -"
149    ;;
150
151  extract)
152    if [ "x$to_stdout" = "x" ]; then
153      eval "$input tar xf -"
154      exit $?
155    else
156      # Extract the file to a temporary directory, then cat it..
157      tmpdir="/tmp/rep-emulate-gnu-tar.$$.output"
158      mkdir "$tmpdir" || exit $?
159      cd "$tmpdir"
160      eval "$input tar xf - $to_stdout" || ( rm -rf $tmpdir && exit $? )
161      cat "$to_stdout"
162      cd "$original_directory"
163      rm -rf "$tmpdir"
164      exit 0
165    fi
166    ;;
167
168  *)
169    echo "Unimplemented command: $command"
170    exit 1
171    ;;
172esac
173