#!/bin/sh
#
# NB: assumes /bin/sh is really bash
# also needs GNU du
#
# picmrf-make - make slideshow-like .sna from N mrfs.
#
# doesn't do too much error-checking, so be careful.


putchar(){
byte_octal=`echo $1 8op | dc`
awk 'BEGIN { printf "\'$byte_octal'" }'
return 0
}



if [ "$1" = "" ]; then
  echo "usage: $0 picture_files >out.sna" 1>&2
  echo '' 1>&2
  echo "The pictures must be in .mrf format and no more than 256x192." 1>&2
  exit 1
fi

trap 'rm -f $tempfile.*' 0 1 3 15

tempfile=/tmp/tmp_psw_$$

# now we have the files in speccy format we can construct the snap.
# the order goes like this:
#	header*
#	screen (blank)
#	attrs (black)
#	code*
#	zero bytes, 1280-code_len
#	boss screen data*
#	byte, number of mrfs
#	mrf block - all the mrfs, each prefixed by its size.
#	zero bytes, 36861-mrf_block_len
#	little-endian word for start address, i.e. 5B00h*
# the asterisked ones above are embedded below as uuencoded data.
# the remainder are catted or dd'd.

# to avoid problems with not wanting shell expansion of the data
# but wanting expansion of the "begin" line (so we can specify a
# (hopefully) unique temp. name) we do the uudecoding in a slightly
# indirect manner.

putchar $# >$tempfile.byte

for i in $*; do
  #XXX should probably check file exists, is mrf, and is <=256x192.
  size=`du -b $i|cut -f 1`
  putchar $[$size%256] >>$tempfile.mrfs
  putchar $[$size/256] >>$tempfile.mrfs
  cat $i >>$tempfile.mrfs
  echo added $i 1>&2
done


# the header
echo "begin 600 $tempfile.hdr" >$tempfile.0
cat <<'EOF' >>$tempfile.0
;``````````````````````````````#^_P$'
`
end
EOF
uudecode $tempfile.0

# screen
dd if=/dev/zero bs=6144 count=1 of=$tempfile.scrn

# attrs
dd if=/dev/zero bs=768 count=1 of=$tempfile.attrs

# boss screen data
echo "begin 600 $tempfile.boss" >$tempfile.0
cat <<'EOF' >>$tempfile.0
M`````````````````````?X!_@$`````````````````````````&#P\```\
M```#_`/\`P`````````````````````````H0D(`"$(```?X!_@'````````
M``````````````````@"/``(#```#_`/\`\`````````````````````````
M"#Q"`#X"```?X!_@'P`````````````````````````(0$(`"$(``#_`/\`_
M`````````````````````````#Y^/``(/```?X!_@'\`````````````````
M``````````````````#_`/\`_P````````````````````````"`````````
M`````````0```````````````````````(`````````````````!````````
M````````````````@`````````````````$```````````````````````"`
M`````````````````0```````````````````````(`````````````````!
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````@$````0```````````$`````````
M``````````````"``#P`?#P\/CP``````0```````````````````````(`\
M`!```!``$``````!````````````````````````@`@\`'P\/#X\``````$`
M``````````````````````"``````````````````0``````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M`````````````(!`.#@$.!P````````!````````````````````````@`A"
M`$)"0`A"``````$```````````````````````"`0C@0'$00.#@X'````0``
M`````````````````````(`80@!"0D`(0@`````!````````````````````
M````@`````````````````$`````````````````````````````````````
M````````````````````````````````````````````````````````````
M``````````````````````````````````````````````````````"`0$0$
M/$0@`````````0```````````````````````(`(#`!\0CP(0``````!````
M````````````````````@$`$$"!$$`001"````$`````````````````````
M``"`*#P`?$(\"$```````0```````````````````````(``````````````
M```!````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````@$!$/$1X(`````````$`````
M``````````````````"`/@(`0GX""$```````0``````````````````````
M`(!`/!`@1!`\$$0@```!````````````````````````@$A"`$)^`@A`````
M``$```````````````````````"``````````````````0``````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M`````````````````(!`1$1$0"`````````!````````````````````````
M@`A"`$)"0@A"``````$```````````````````````"`0D00($001!!$(```
M`0```````````````````````(!^0@!"0D((0@`````!````````````````
M````````@`````````````````$`````````````````````````````````
M````````````````````````````````````````````````````````````
M``````````````````````````````````````````````````````````"`
M?C@\/#P@`````````0```````````````````````(`(/`!\0CP^/``````!
M````````````````````````@#P\#!PX##P,."````$`````````````````
M``````"`"#P`?$(\/CP``````0```````````````````````(``````````
M```````!````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````@`````````````````$`
M``````````````````````"``````````````````0``````````````````
M`````(`````````````````!````````````````````````@```````````
M``````$```````````````````````#__________________P``````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````/`````````````````````````````````````````!"&#P\
M/```&#P\/```&#P\?@`\```0```$`'P0`````)DH0D)"```H0D)````H0D("
M`$)H.#@<.`0`0A`<````H0A"/`(```A"/'P```A"/`0`0E1`$"`$/`!"$"``
M``"A"#Y"/```"#Y"0@``"#Y""`!^5#@0(#Q$`'P0(````)D(`D)`"``(`D)"
M"``(`D(0`$)4!!`@1$0`0!`@&```0CX\/'X(`#X\/#P(`#X\/!``0E1X#"`\
M/`!`#!P8```\`````!```````!``````````````````````````````````
M````````````````````````````````````>``0`````#P`````!`!"````
M```0$``@$`````````!$'`!$.#@`0@``.'@$`&8``#A$.``0."`0.```````
M`$(@,$1$0`!"$``$1#P`6A``!$0$,!`$/!!$````````0B`0*'@X`'X``#Q$
M1`!"```\*#P0$#PB$'@```````!$(!`H0`0`0@``1$1$`$(``$0H1!`01"(0
M0!@``````'@@.!`\>`!"$``\1#P`0A``/!`\.`P\/`P\&```````````````
M```````````````````````````````````_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S]'1T='1T='1T)R=&QH
M0#\_/S\_/S\_/S\_/S\_/S\_/VAH:&AH:&AH:&AH:&AH/S\_/S\_/S\_/S\_
M/S\_/S\_>'AX>'AX>'AX>'AX>'@_/S\_/S\_/S\_/S\_/S\_/S]X>'AX>'AX
M>'AX>'AX>#\_/S\_/S\_/S\_/S\_/S\_/WAX>'AX>'AX>'AX>'AX/S\_/S\_
M/S\_/S\_/S\_/S\_>'AX>'AX>'AX>'AX>'@_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_/S\_
M/S\_/S\_/S\_/S@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
=.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@`
`
end
EOF
uudecode $tempfile.0

# code (source is picmrf.z)
# this corresponds to this version of picmrf.z:
#XXX use uudecode when finished

cat /home/rus/src/speccy/picmrf.bin >$tempfile.code
#echo "begin 600 $tempfile.code" >$tempfile.0
#cat <<'EOF' >>$tempfile.0
#XXX
#end
#EOF
#uudecode $tempfile.0

# lookup table zero block
dd if=/dev/zero bs=512 count=1 of=$tempfile.zero0

# the code zero block
# first find out how big it needs to be (i.e. work out 1280-code_len)
# I had some fun :-/ getting the next bit to work, so excuse the
# rather bizarre use of sed...
code_len=`wc -c $tempfile.code | cut -d/ -f 1 | sed 's/ *//'`
dd if=/dev/zero bs=$[1280-$code_len] count=1 of=$tempfile.zero1

# the mrf zero block
mrf_len=`wc -c $tempfile.mrfs | cut -d/ -f 1 | sed 's/ *//'`
dd if=/dev/zero bs=$[36861-$mrf_len] count=1 of=$tempfile.zero2

# finally, the 5B00h word.
echo "begin 600 $tempfile.wrd" >$tempfile.0
cat <<'EOF' >>$tempfile.0
"`%L`
`
end
EOF
uudecode $tempfile.0

rm -f $tempfile.0

# assemble them all to stdout.

cat $tempfile.hdr $tempfile.scrn $tempfile.attrs \
	$tempfile.code $tempfile.zero1 \
	$tempfile.boss $tempfile.zero0 $tempfile.byte \
	$tempfile.mrfs $tempfile.zero2 $tempfile.wrd

echo remaining space: 1>&2
ls -l $tempfile.zero2 1>&2

# also dels $tempfile.*
exit 0
