#!/bin/bash
#
# program to create the icons
# uses imagemagick and the bc
#
# 17. May 2004
# Diether Knof <dknof@gmx.de>

scaling=80

if [ ! -e ../cards/club/queen.png ]; then
  echo "could not find '../cards/club/queen.png' -- wrong directory?"
  exit 1
fi
PATH=$PATH:../../../bin/

# get the width and the height of the cards
#width=`xpmtoppm ../cards/club/queen.png | pnmfile | awk '{ print $4 }'`
#height=`xpmtoppm ../cards/club/queen.png | pnmfile | awk '{ print $6 }'`
width=`identify -format "%w" ../cards/club/queen.png`
height=`identify -format "%h" ../cards/club/queen.png`
height_half=$(( ${height} / 2 ))

overlap_width=$(( ${width} / 4 ))

overlap() {
  convert pixel.png -resize $(( ${width} + ($# - 1) * ${overlap_width} ))x${height_half}\! overlap.png

  for (( i = 0; $# != 0 ; i++ )); do
    composite -geometry +$(( $i * ${overlap_width} ))+0 ../cards/$1 overlap.png o.png \
    && mv o.png overlap.png
    shift
  done # for i

  cat overlap.png
}

echo -e "P4\n1 1\n" > pixel.pbm
convert -transparent white pixel.pbm pixel.png
rm pixel.pbm


# re.png
overlap club/queen.png \
> re.png

thickness_x=`echo "${width} / 10" | bc`
thickness_y=`echo "${height} / 10" | bc`
convert -fill red -draw "polygon \
0,$(( $height_half - ${thickness_y} )) \
0,$(( $height_half )) \
${thickness_x},$(( $height_half )) \
$width,${thickness_y} \
$width,0 \
$(( $width - ${thickness_x} )),0 \
" re.png t.png
composite -compose In t.png re.png contra.png

# Soli
overlap club/jack.png spade/jack.png heart/jack.png diamond/jack.png \
> solo.jack.png
overlap club/queen.png spade/queen.png heart/queen.png diamond/queen.png \
> solo.queen.png
overlap club/king.png spade/king.png heart/king.png diamond/king.png \
> solo.king.png
overlap club/queen.png spade/queen.png heart/jack.png diamond/jack.png \
> solo.queen-jack.png
overlap club/king.png spade/king.png heart/jack.png diamond/jack.png \
> solo.king-jack.png
overlap club/king.png spade/king.png heart/queen.png diamond/queen.png \
> solo.king-queen.png
overlap club/king.png spade/king.png heart/queen.png diamond/jack.png \
> solo.koehler.png

overlap spade/queen.png heart/jack.png club/ace.png club/king.png \
> solo.club.png
overlap club/queen.png heart/jack.png spade/ace.png spade/king.png \
> solo.spade.png
overlap club/queen.png diamond/jack.png heart/ace.png heart/king.png \
> solo.heart.png
overlap club/queen.png heart/jack.png diamond/ace.png diamond/king.png \
> solo.diamond.png

overlap club/ace.png spade/ace.png heart/ace.png diamond/ace.png \
> solo.meatless.png

# marriage
overlap club/queen.png club/queen.png \
> marriage.png
overlap club/queen.png club/queen.png diamond/jack.png spade/ace.png \
> marriage.foreign.png
overlap club/queen.png club/queen.png heart/jack.png diamond/ace.png \
> marriage.trump.png
overlap club/queen.png club/queen.png heart/ace.png spade/ace.png \
> marriage.color.png
overlap club/queen.png club/queen.png club/ace.png club/ace.png \
> marriage.club.png
overlap club/queen.png club/queen.png spade/ace.png spade/ace.png \
> marriage.spade.png
overlap club/queen.png club/queen.png heart/ace.png heart/ace.png \
> marriage.heart.png

# genscher
overlap diamond/king.png diamond/king.png \
> genscher.png

# poverty
overlap heart/queen.png spade/jack.png diamond/ten.png \
> poverty.png
overlap heart/queen.png spade/jack.png diamond/ten.png \
> poverty.3.png
overlap heart/queen.png diamond/ten.png club/ace.png \
> poverty.2.png
overlap heart/queen.png club/ace.png heart/king.png \
> poverty.1.png
overlap club/ace.png heart/king.png spade/ten.png \
> poverty.0.png

# five nines
overlap club/nine.png spade/nine.png heart/nine.png diamond/nine.png diamond/nine.png \
> five_nines.png

# six kings
overlap club/king.png club/king.png spade/king.png heart/king.png diamond/king.png diamond/king.png \
> six_kings.png

# fox highest trump
overlap diamond/ace.png diamond/ten.png diamond/king.png diamond/nine.png \
> fox_highest_trump.png

# swines
overlap club/ace.png club/ace.png \
> swines.club.png
overlap spade/ace.png spade/ace.png \
> swines.spade.png
overlap heart/ace.png heart/ace.png \
> swines.heart.png
overlap diamond/ace.png diamond/ace.png \
> swines.diamond.png

# hyperswines
overlap club/nine.png club/nine.png \
> hyperswines.club.png
overlap spade/nine.png spade/nine.png \
> hyperswines.spade.png
overlap heart/nine.png heart/nine.png \
> hyperswines.heart.png
overlap diamond/nine.png diamond/nine.png \
> hyperswines.diamond.png

# swines and hyperswines
overlap club/ace.png club/nine.png \
> swines-hyperswines.club.png
overlap spade/ace.png spade/nine.png \
> swines-hyperswines.spade.png
overlap heart/ace.png heart/nine.png \
> swines-hyperswines.heart.png
overlap diamond/ace.png diamond/nine.png \
> swines-hyperswines.diamond.png

# dollen
overlap heart/ten.png heart/ten.png \
> dollen.png

# specialpoints
overlap club/ace.png spade/ten.png diamond/ace.png heart/ten.png \
> doppelkopf.png

# no_*
for limit in 120 90 60 30 0; do
  echo "%!PS-Adobe-3.0
  %%LanguageLevel: 2
  %%
  %%Title: no 120
  %%For: Diether Knof
  %%CreationDate: 20-06-2002
  %%Creator: Diether Knof
  %%
  %%Orientation: Portrait
  %%BoundingBox: 0 0 92 45
  %%Pages: 1
  %%
  %%EndComments

  %Groesse ~ 842x595
  92 45 /hoehe exch def /breite exch def

  %
  % change encoding to ISO8859-1  -  reiner@schildi.xnc.com
  %
  % <fontname> ISOfindfont => <font>
  %
  /ISOfindfont {
  dup 100 string cvs (ISO-) exch concatstrings cvn exch
  findfont dup maxlength dict begin
  { 1 index /FID ne {def}{pop pop} ifelse } forall
    /Encoding ISOLatin1Encoding def
    currentdict
    end definefont
  } def

  /Times-ItalicBold ISOfindfont
  setfont

  /Zentriere {
  % Text
  /Groesse exch def
  /Text exch def
  gsave
  %    /StandardSymL ISOfindfont
  ISOfindfont
  Groesse scalefont
  setfont
  breite Text stringwidth pop sub 2 div
  1 moveto
  Text show
  grestore
} def

%%Page: 1 1

/Times-Bold (limit) 63 Zentriere

gsave
/DeviceRGB setcolorspace
1 0 0 setcolor
newpath
2 10 moveto
breite 2 sub hoehe 10 sub lineto
closepath
10 setlinewidth
stroke
grestore

showpage

quit"\
| sed "s/limit/${limit}/" \
> no_${limit}.ps
convert -colors 3 -transparent white no_${limit}.ps no_${limit}.png
rm -f no_${limit}.ps
done

# no_*_reply
for limit in 120 90 60 30 0; do
  echo "%!PS-Adobe-3.0
  %%LanguageLevel: 2
  %%
  %%Title: no 120 reply
  %%For: Diether Knof
  %%CreationDate: 14-05-2004
  %%Creator: Diether Knof
  %%
  %%Orientation: Portrait
  %%BoundingBox: 0 0 92 45
  %%Pages: 1
  %%
  %%EndComments

  %Groesse ~ 842x595
  92 45 /hoehe exch def /breite exch def

  %
  % change encoding to ISO8859-1  -  reiner@schildi.xnc.com
  %
  % <fontname> ISOfindfont => <font>
  %
  /ISOfindfont {
  dup 100 string cvs (ISO-) exch concatstrings cvn exch
  findfont dup maxlength dict begin
  { 1 index /FID ne {def}{pop pop} ifelse } forall
    /Encoding ISOLatin1Encoding def
    currentdict
    end definefont
  } def

  /Times-ItalicBold ISOfindfont
  setfont

  /Zentriere {
  % Text
  /Groesse exch def
  /Text exch def
  gsave
  %    /StandardSymL ISOfindfont
  ISOfindfont
  Groesse scalefont
  setfont
  breite Text stringwidth pop sub 2 div
  1 moveto
  Text show
  grestore
} def

%%Page: 1 1

/Times-Bold (limit) 63 Zentriere

showpage

quit"\
| sed "s/limit/${limit}/" \
> no_${limit}_reply.ps
convert -colors 3 -transparent white no_${limit}_reply.ps no_${limit}_reply.png
rm -f no_${limit}_reply.ps
done

# scale all icons
for f in re.png contra.png solo*.png marriage*.png genscher.png poverty*.png five_nines.png six_kings.png fox_highest_trump.png swines.*.png hyperswines.*.png swines-hyperswines.*.png no_*.png
do
  #convert -scale $(( $width * $scaling / 100 ))x$(( $height * $scaling / 100 )) \
  convert -scale $scalingx$scaling% \
  $f t.png \
  && mv t.png $f
done


rm pixel.png
rm overlap.png


# sharpen and minimize
for f in *.png; do \
  convert -sharpen 1 $f t.png \
  && mv t.png $f
done

