#!/bin/bash
#
# mahjongg_builder.sh v 1.01
#
# builds tile sets for gnome mahjongg from a set of images
# makes extensive use of ImageMagick http://www.imagemagick.org (version 6.2.5 was used)
# and is for GNOME Mahjongg (version 2.12.2 was used) http://www.gnome.org/projects/gnome-games/
#
# rednuht 03/2006 http://www.jumpstation.co.uk/scripts/mahjongg_builder/
#
# ensure bash is version >=2.04 
#

usage="Usage: $0 -s <source-folder> [-d <destination-folder>] [-S] [-i <mahjongg-pixmap-folder>]" 

echo "using Bash [$BASH_VERSION], should be greater than 2.04"
echo "using ["`convert -version | head -n 1`"], works with 6.2.5"

while getopts "s:d:Si:" opt; do
  case $opt in
    s ) sourceDir=$OPTARG ;;
    d ) destDir=$OPTARG ;;
    S ) doubleSize="false" ;;
    i ) installDir=$OPTARG ;;
    \? )  echo $usage 
          exit 1 ;;
  esac
done

if [ -z $sourceDir ]; then
  echo ":[sourceDir] not supplied, exiting"
  echo $usage 
  exit 1
fi

if [ ! -d $sourceDir ]; then
  echo ":[$sourceDir] is not accessable, exiting"
  echo $usage 
  exit 1
fi

destDir=${destDir:="tmp"}
tempDir=$destDir"/tmp"
installDir=${installDir:="/usr/share/pixmaps/mahjongg"}
doubleSize=${doubleSize:="true"}
# remove ending slash from destination
#echo "destDir [$destDir]"
#echo "len [${#destDir}]"
#echo "-1 [$((${#destDir} - 1))]"
#echo "char [${destDir:$((${#destDir} - 1))}]"
if [ ${destDir:$((${#destDir} - 1))} = "/" ]; then
#  echo "last char is slash, remove"
  destDir=${destDir:0:$((${#destDir} - 1))}
fi
# add an ending slash to source
#echo "sourceDir [$sourceDir]"
#echo "len [${#sourceDir}]"
#echo "-1 [$((${#sourceDir} - 1))]"
#echo "char [${sourceDir:$((${#sourceDir} - 1))}]"
if [ ${sourceDir:$((${#sourceDir} - 1))} != "/" ]; then
#  echo "last char is not a slash, adding"
  sourceDir=$sourceDir/
fi

echo "Mahjongg Builder"
if [ $doubleSize = "true" ]; then
  echo "Running in double size mode"
  x=106
  y=154
  offX=18
  offY=2
  sub=128
  offB=20
else
  echo "Running in single size mode"
  x=53
  y=77
  offX=9
  offY=1
  sub=64
  offB=10
fi

# function taken from http://www.cit.gu.edu.au/~anthony/graphics/imagick6/advanced/create_bullet
# in bash scripts the function must be defined first
# might want to allow size to be defined or a half size flag be passed
function buildBullet() {
INPUT=$1
COLOR=$2
OUTPUT=$3
  convert $INPUT -matte \
        \( +clone -fx A +matte -bordercolor black -border 5 \
           -blur 0x2 -shade 140x45 -normalize -blur 0x1 \
           -fill $COLOR -tint 100 \) \
        -gravity center -compose Src_Atop -composite \
        $OUTPUT
}

# check if there are 42 images ready to become titles
count=`ls $sourceDir*{.png,.jpg,.jpeg,.PNG,.JPG,.JPEG} 2> /dev/null | wc -l`
if echo "Found $count images" | grep "42"; then
  echo "images acounted for";
else
  echo "the source directory needs to have 42 images, this has $count";
  exit 1
fi


if [ -d $destDir ]; then
  echo "destination directory found"
else
	mkdir $destDir
  echo "destination directory created"
fi
if [ -d $tempDir ]; then
  echo "unsualy, temp dir already exists"
  flagTemp="false"
else
	mkdir $tempDir
  echo "temp directory created"
  flagTemp="true"
fi


# get the blank tile
convert $installDir/smooth.png -crop 64x88+2688x0 +repage "$tempDir/blnk.png"
if [ $doubleSize = "true" ]; then
  convert "$tempDir/blnk.png" -resize 128x176\! "$tempDir/blnk.png"
fi

# resize target images into temp dir
#
echo "resizing images"
echo "------------------------------------------|"
for image in $( ls $sourceDir*{.png,.jpg,.jpeg,.PNG,.JPG,.JPEG} 2> /dev/null ) 
do 
	convert $image -resize $x"x"$y\!  $tempDir/TILE_${image:${#sourceDir}}
  echo -n "."
done


# combine with blank to get 3d
#
echo ""
echo "building tiles"
echo "------------------------------------------|"
pushd $tempDir
for image in $(ls TILE_*{.png,.jpg,.jpeg,.PNG,.JPG,.JPEG} 2> /dev/null ) 
do 
	composite -geometry +$offX+$offY $image "blnk.png" -background none $image
  echo -n "."
done

# build set
# adding a blank on the end
#
echo ""
echo "combining tiles"
echo "------------------------------------------|"
for image in $(ls TILE_*{.png,.jpg,.jpeg,.PNG,.JPG,.JPEG} 2> /dev/null ) 
do 
	montage strip.png $image -geometry +0+0 -background none strip.png
  echo -n "."
done
echo ""
echo "adding blank"
montage strip.png blnk.png -geometry +0+0 -background none strip.png

# mark special tiles
echo "building marks"
echo "---------|"
if [ $doubleSize = "true" ]; then
  convert +antialias -size 15x15 xc:none -draw 'circle 7,7 3,3'  ball.png
else
  convert +antialias -size 8x8 xc:none -draw 'circle 4,4 2,2'  ball.png
fi
  echo -n "."
buildBullet ball.png red red_ball.png
  echo -n "."
buildBullet ball.png green green_ball.png
  echo -n "."
buildBullet ball.png blue blue_ball.png
  echo -n "."
buildBullet ball.png yellow yellow_ball.png
  echo -n "."
buildBullet ball.png maroon maroon_ball.png
if [ $doubleSize = "true" ]; then
  convert -size 12x12 xc:black   square.png
else
  convert -size 6x6 xc:black   square.png
fi
  echo -n "."
buildBullet square.png red red_square.png
  echo -n "."
buildBullet square.png green green_square.png
  echo -n "."
buildBullet square.png blue blue_square.png
  echo  "."
buildBullet square.png cyan cyan_square.png
echo "applying marks"
echo "---------|"
loc=$((($sub * 41) + $offB))
composite -geometry +$loc+5 red_ball.png strip.png -background none strip.png
  echo -n "."
loc=$((($sub * 40) + $offB))
composite -geometry +$loc+5 green_ball.png strip.png -background none strip.png
  echo -n "."
loc=$((($sub * 39) + $offB))
composite -geometry +$loc+5 blue_ball.png strip.png -background none strip.png
  echo -n "."
loc=$((($sub * 38) + $offB))
composite -geometry +$loc+5 yellow_ball.png strip.png -background none strip.png
  echo -n "."
loc=$((($sub * 37) + $offB))
composite -geometry +$loc+5 maroon_ball.png strip.png -background none strip.png
echo -n "."
loc=$((($sub * 36) + $offB))
composite -geometry +$loc+5 cyan_square.png strip.png -background none strip.png
echo -n "."
loc=$((($sub * 35) + $offB))
composite -geometry +$loc+5 red_square.png strip.png -background none strip.png
echo -n "."
loc=$((($sub * 34) + $offB))
composite -geometry +$loc+5 green_square.png strip.png -background none strip.png
echo -n "."
loc=$((($sub * 33) + $offB))
composite -geometry +$loc+5 blue_square.png strip.png -background none strip.png
echo "."



# build selected (add a bit of yellow)
#
echo "building selected"
cp strip.png selected.png
convert selected.png -fill yellow -colorize 25% selected.png

popd

# combine and complete
#
echo "combining"
montage $tempDir/strip.png $tempDir/selected.png -tile 1x2 -geometry +0+0 -background none $destDir/tileset.png

echo "Clean up"
# clear up all files
if [ $flagTemp = "true" ]; then
  echo "Attempting to remove [$tempDir]"
  rm -fr $tempDir
else
  echo "Temp directory has not been removed as it existed before script ran, "
  echo "please delete contents of $tempDir manually."
fi

echo "All finished"
if [ -d $installDir ]; then
  echo "Just copy the tileset.png from $destDir to the $installDir"
else
  echo "Just copy the tileset.png from $destDir to Mahjongg pixmaps directory."
fi
echo "you can rename it"

