#!/bin/bash
## coding: utf-8
## 2020-3-25, by Careone
## filename: add-numbers-to-board.sh

#PDIR=`pwd`

APPNAME="addCoords2board"
APPVER="0.1~2020-0323"
APPDESC="在中国象棋棋盘四周，添加坐标 A-I, 0-9"

### 
DEBUG=1

# x0:     上下的横向坐标 A-I 的第一个字母 A 的起始x偏移量(从左到右)
# y0, y1: 上下的横向坐标 A-I 的y坐标(从左到右)

# x2, x3: 左右竖向坐标 9-0 的第一个数字 9 的起始x偏移量(从上到下)
# y2:     左右竖向坐标 9-0 的第一个数字 9 的起始y偏移量(从上到下)

declare -i WIDTH HEIGHT STEP
declare -i xx yy #如果自定义的棋盘尺寸，上下左右各有多余的像素，则除以2均分，需要额外的偏移量 
declare -i x0 y0 y1 
declare -i x2 x3 y2 
#STEP=49

declare -a strx stry boardSize
strx=( A B C D E F G H I )
stry=( 9 8 7 6 5 4 3 2 1 0 )

declare -a x y

txtfont="/usr/share/fonts/truetype/alibaba/Alibaba-PuHuiTi-Bold.ttf"
POINTSIZE=14
FILL_COL=black



file0="$HOME/xboard/xiangqi/board-done/test.png"
saveto="$HOME/xboard/xiangqi/xqboard-mono-9x10.png"

# tag 50:
	_addLineNumber4xuanFeng () {
#pwd

    echo "  * 添加数字 (1-9,九到一) 到棋盘图片"
    echo "    (主要适用于xiangqi-xuanfeng-26/40/54 系列主题)"

if [ -e "xy1.png" ]; then
  echo "  查找图片 xy1.png ..."
  for a in `ls -1 board*.png`; do
    echo "  * 更新棋盘图片 $a"
    convert "$a" "xy1.png" -composite "$a"
#    convert "$PDIR/$a" "$PDIR/xy1.png" -composite "$PDIR/$a"    
  done
    
fi
}
  
### head end ###

### body ###
echo -e " $APPNANE $APPVER\n  $APPDESC\n"
# -----------

if [ "$#" -ge 1 ] && [ -s "$1" ]; then
  file0="$1"
fi


saveto="$HOME/xboard/xiangqi/xqboard-`date +'%m%d_%H%M%S'`-9x10.png"
  echo -e " file:\t$file0\n saveto:\t$saveto\n"

if [ -s "$file0" ]; then
  identify "$file0"
  boardSize=( `identify "$file0" | cut -d' ' -f3 | sed '/x/s// /'` )
  WIDTH="${boardSize[0]}"; 
  HEIGHT="${boardSize[1]}";
  let "STEP = HEIGHT / 10";
  #let "STEP = WIDTH / 9"
  let "xx = WIDTH % 9 / 2 + STEP / 2"; let "yy = HEIGTH % 10 / 2 + STEP / 2"; 
fi

## tag 83:
# -----------------
# 坐标字母/数字可以自动定位。已对 POINTSIZE=14,
# 441x490, 513x570 的中国象棋棋盘大小测试过。
# 其它尺寸的棋盘，字母/数字定位可能略有偏差

# 横向坐标字母 A-I (从左到右)
let "x0 = xx - POINTSIZE / 3"; 
y0=14; let "y1 = HEIGHT - 5";

# 竖向坐标数字 9-0 (从上到下)
x2=5; let "x3 = WIDTH - xx + POINTSIZE";
y2="STEP / 2 + POINTSIZE / 3";
# -----------------

# 不同棋子大小的棋盘，部分坐标需要微调
  case "$STEP" in
    49) let "x0 += 1";
        let "y0 -= 3"; let "y1 += 4";
    	let "x2 -= 3";
    	;;
    *) : ;;
  esac 
#
if [ "$DEBUG" = 1 ]; then
   
  echo -e "\n L44:\tWIDTH=$WIDTH; HEIGHT=$HEIGHT; STEP=$STEP;\n\tx0=$x0; y0=$y0"
  echo -e "\tPOINTSIZE=$POINTSIZE; FILL_COL=$FILL_COL"
  sleep 1
fi

## ----
  x[0]="$x0"

  for a in `seq 1 1 8`; do
    let "b = a - 1"
    let "x[a] = x[b] + STEP"
    #echo -en " ${x[a]}"
  done 
## ----

## ----
  y[0]="$y2"

  for a in `seq 1 1 9`; do
    let "b = a - 1"
    let "y[a] = y[b] + STEP"
    #echo -en " ${y[a]}"
  done 
## ----

  convert "$file0" -fill $FILL_COL -font "$txtfont" -pointsize $POINTSIZE \
    -draw "text ${x[0]},$y0 '${strx[0]}'" \
     -draw "text ${x[1]},$y0 '${strx[1]}'" \
      -draw "text ${x[2]},$y0 '${strx[2]}'" \
       -draw "text ${x[3]},$y0 '${strx[3]}'" \
        -draw "text ${x[4]},$y0 '${strx[4]}'" \
         -draw "text ${x[5]},$y0 '${strx[5]}'" \
          -draw "text ${x[6]},$y0 '${strx[6]}'" \
           -draw "text ${x[7]},$y0 '${strx[7]}'" \
            -draw "text ${x[8]},$y0 '${strx[8]}'" \
    -draw "text ${x[0]},$y1 '${strx[0]}'" \
     -draw "text ${x[1]},$y1 '${strx[1]}'" \
      -draw "text ${x[2]},$y1 '${strx[2]}'" \
       -draw "text ${x[3]},$y1 '${strx[3]}'" \
        -draw "text ${x[4]},$y1 '${strx[4]}'" \
         -draw "text ${x[5]},$y1 '${strx[5]}'" \
          -draw "text ${x[6]},$y1 '${strx[6]}'" \
           -draw "text ${x[7]},$y1 '${strx[7]}'" \
            -draw "text ${x[8]},$y1 '${strx[8]}'" \
    -draw "text $x2,${y[0]} '${stry[0]}'" \
     -draw "text $x2,${y[1]} '${stry[1]}'" \
      -draw "text $x2,${y[2]} '${stry[2]}'" \
       -draw "text $x2,${y[3]} '${stry[3]}'" \
        -draw "text $x2,${y[4]} '${stry[4]}'" \
         -draw "text $x2,${y[5]} '${stry[5]}'" \
          -draw "text $x2,${y[6]} '${stry[6]}'" \
           -draw "text $x2,${y[7]} '${stry[7]}'" \
            -draw "text $x2,${y[8]} '${stry[8]}'" \
             -draw "text $x2,${y[9]} '${stry[9]}'" \
    -draw "text $x3,${y[0]} '${stry[0]}'" \
     -draw "text $x3,${y[1]} '${stry[1]}'" \
      -draw "text $x3,${y[2]} '${stry[2]}'" \
       -draw "text $x3,${y[3]} '${stry[3]}'" \
        -draw "text $x3,${y[4]} '${stry[4]}'" \
         -draw "text $x3,${y[5]} '${stry[5]}'" \
          -draw "text $x3,${y[6]} '${stry[6]}'" \
           -draw "text $x3,${y[7]} '${stry[7]}'" \
            -draw "text $x3,${y[8]} '${stry[8]}'" \
             -draw "text $x3,${y[9]} '${stry[9]}'" \
  "$saveto"

echo " Done"
sleep 1

if [ -s "$saveto" ]; then
  identify "$saveto"
  sleep 1
  eog "$saveto" &
fi

exit 0;

### -----------------

exit 0

