#!/bin/sh
# Modified for bug in old imagemagic say -geometry 640x361+32+32 instead
# of the correct -geometry 640x360+32+32
#
help(){
    cat <<END
Usage: $0 [options] <filename.png ...>
Options:
    -h        Print this help message
END
    exit 0
}

while getopts h name "$@"
do
    case $name in
*)
    help ;;
    esac
done
let shiftind=$OPTIND-1
shift $shiftind
if test "$#" = "0"
then
    help
fi

if test ! -d tmp 
then
    mkdir tmp
fi
for i in "$@"
do
    j=`echo $i | sed "s/.png//"`
    convert -sample 100x50% -resize 640x360! $i tmp/$i
done
(

cd tmp
convert -background transparent -fill yellow \
    -pointsize 100 label:back prevy.png
convert -background transparent -fill red \
    -pointsize 100 label:back prevr.png
convert -background transparent -fill yellow \
    -pointsize 100 label:next nexty.png
convert -background transparent -fill red \
    -pointsize 100 label:next nextr.png
convert -size 640x360 xc:transparent blank.png
convert -size 2956x1900 xc:black black.png
convert -size 2956x1900 xc:transparent trans.png
n=1
while test $# -ge 1
do
    c=1
    files=""
    while test $# -ge 1 -a $c -le 9
    do
        files=$files" $1"
        let c=c+1
        shift
    done
    echo Menu $n using $files
    blank=""
    for i in $files
    do
        blank=$blank" blank.png"
    done
    montage -background black \
        -tile 3x3 \
        -frame 16x16 \
        -mattecolor red \
        -geometry 640x361+32+32 \
        $files out.png
    montage -background transparent \
        -tile 3x3 \
        -bordercolor transparent \
        -frame 16x16 \
        -mattecolor yellow \
        -geometry 640x361+32+32 \
        $blank outh.png
    composite -compose src-over \
        -geometry +0-100 \
        -gravity center \
        out.png black.png main-$n.png
    composite -compose src-over \
        -geometry +0-100 \
        -gravity center \
        outh.png trans.png main-${n}h.png
    if test $# -ge 1
    then
        composite -compose src-over \
            -geometry +1720+1550 \
            nextr.png main-$n.png main-$n.png
        composite -compose src-over \
            -geometry +1720+1550 \
            nexty.png main-${n}h.png main-${n}h.png
    fi
    if test $n -ge 2
    then
        composite -compose src-over \
            -geometry +965+1550 \
            prevr.png main-$n.png main-$n.png
        composite -compose src-over \
            -geometry +965+1550 \
            prevy.png main-${n}h.png main-${n}h.png
    fi
    convert -resize 720x480! \
        -depth 8 \
        main-$n.png ../menu-$n.png
    convert -resize 720x480! \
        -depth 8 \
        main-${n}h.png tmp.ppm
    ppmquant 4 tmp.ppm >tmp4.ppm
    convert -transparent black \
        tmp4.ppm ../menu-${n}h.png
    let n=n+1
done
)
