No empty .Rs/.Re
[netbsd-mini2440.git] / distrib / acorn32 / mksparkive.sh
blobe080bc63ca0bcfbe92d90aca8411701164ab835f
1 #!/bin/sh -e
2 # $NetBSD: mksparkive.sh,v 1.7 2004/11/10 03:55:28 jmc Exp $
4 # Copyright (c) 2004 The NetBSD Foundation, Inc.
5 # All rights reserved.
7 # This code is derived from software contributed to The NetBSD Foundation
8 # by Gavan Fantom
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
33 # Creates a spark format archive. Some metadata is included, notably
34 # filetypes, but permissions are not. Filename translation is performed
35 # according to RISC OS conventions.
37 # This script is intended to provide sufficient functionality to create
38 # an archive for distribution of the NetBSD/acorn32 bootloader which can be
39 # used directly in RISC OS.
42 if [ -z "${TOOL_SPARKCRC}" ]
43 then
44 TOOL_SPARKCRC=sparkcrc
47 if [ -z "${TOOL_STAT}" ]
48 then
49 TOOL_STAT=stat
52 if [ -z "${TOOL_MKTEMP}" ]
53 then
54 TOOL_MKTEMP=mktemp
58 # Target byte order is little endian.
60 print2()
62 if [ -z "$1" ]
63 then
64 exit 1
66 lowbyte=`expr $1 % 256 | xargs printf %02x`
67 highbyte=`expr $1 / 256 | xargs printf %02x`
68 printf "\x$lowbyte\x$highbyte"
71 print4()
73 if [ -z "$1" ]
74 then
75 exit 1
77 print2 `expr $1 % 65536`
78 print2 `expr $1 / 65536`
81 makeheader()
83 filename="$1"
84 statfilename="$2"
85 realfilename="$3"
86 filetype=`printf %03s "$4"`
87 compressed="$5"
88 # length is only passed to length4, so we don't need to worry about
89 # extracting only the length here.
90 length=`wc -c "$filename"`
91 eval `${TOOL_STAT} -s "$statfilename"`
92 # centiseconds since 1st Jan 1900
93 timestamp=`expr $st_mtime \* 100 + 220898880000`
94 lowtype=`echo "$filetype" | sed s/.//`
95 hightype=`echo "$filetype" | sed s/..\$//`
96 highdate=`expr $timestamp / 4294967296 | xargs printf %02x`
97 lowdate=`expr $timestamp % 4294967296`
99 # Header version number
100 if [ "$compressed" -ne 0 ]
101 then
102 printf \\xff
103 else
104 printf \\x82
106 # Filename
107 printf %-13.13s "$realfilename" | tr " ." \\0/
108 # Compressed file length
109 print4 $length
110 # File date stamp
111 print2 0
112 # File time stamp
113 print2 0
114 # CRC
115 if [ "$compressed" -ne 0 ]
116 then
117 print2 `${TOOL_SPARKCRC} "$statfilename"`
118 else
119 print2 `${TOOL_SPARKCRC} "$filename"`
121 # Original file length
122 if [ "$compressed" -ne 0 ]
123 then
124 print4 $st_size
125 else
126 print4 $length
128 # Load address (FFFtttdd)
129 printf \\x$highdate
130 printf \\x$lowtype
131 printf \\xf$hightype
132 printf \\xff
133 # Exec address (dddddddd)
134 print4 $lowdate
135 # Attributes
136 # Public read, owner read/write
137 print4 19
140 makearchive()
142 for file in "$@"
144 temp=`${TOOL_MKTEMP} -t $progname` || exit 1
145 trap "rm -f $temp" 0
146 # Archive marker
147 printf \\x1a
148 if [ -f "$file" ]
149 then
150 case "$file" in
151 -*) echo "Invalid filename" >&2
152 exit 1
154 *,???) type=`echo "$file" | \
155 sed "s/.*,\(...\)$/\1/"`
156 filename=`echo "$file" | \
157 sed "s/,...$//"`
159 *) type=fff
160 filename="$file"
162 esac
163 # The compressed data in a sparkive is the output from
164 # compress, minus the two bytes of magic at the start.
165 # Compress also uses the top bit of the first byte
166 # to indicate its choice of algorithm. Spark doesn't
167 # understand that, so it must be stripped.
168 compress -c "$file" | tail -c +3 >"$temp"
169 size1=`wc -c "$file" | awk '{print $1}'`
170 size2=`wc -c "$temp" | awk '{print $1}'`
171 if [ $size1 -ge $size2 ]
172 then
173 makeheader "$temp" "$file" "$filename" "$type" 1
174 nbits=`dd if="$temp" bs=1 count=1 2>/dev/null| \
175 od -t d1 | awk '{print $2}'`
176 if [ $nbits -ge 128 ]
177 then
178 nbits=`expr $nbits - 128`
180 printf \\x`printf %02x $nbits`
181 tail -c +2 "$temp"
182 else
183 makeheader "$file" "$file" "$filename" "$type" 0
184 cat "$file"
187 if [ -d "$file" ]
188 then
190 cd "$file"
191 makearchive `ls -A` >$temp
193 if [ $? -ne 0 ]
194 then
195 exit 1
197 makeheader "$temp" "$file" "$file" ddc 0
198 cat "$temp"
200 rm -f "$temp"
201 done
203 # Archive marker
204 printf \\x1a
205 # Archive terminator
206 printf \\x00
209 progname=`basename $0`
211 if [ $# -eq 0 ]
212 then
213 echo "Usage: $progname filename"
214 echo "$progname: Outputs an uncompressed sparkive to stdout."
217 makearchive "$@"