!/usr/bin/env bash
mkdir ./Tsumego
VAR1=96
VAR2=97
NUM=1
p1=0
p2=0
while read -n1 c; do
    if [ "$c" == "0" ]; then
            VAR1=`expr $VAR1 + 1`
        if [ $VAR1 == 116 ]; then
                 VAR1=97
                 VAR2=`expr $VAR2 + 1`
        fi
    fi
    if [ "$c" == "-" ]; then
            VAR1=`expr $VAR1 + 1`
        if [ $VAR1 == 116 ]; then
                 VAR1=97
                 VAR2=`expr $VAR2 + 1`
        fi
    fi
    if [ "$c" == "!" ]; then
        printf '%b\n' ";W[\\$(printf '0%o' "$VAR1")\\$(printf '0%o' "$VAR2")]" >> ./Tsumego/"$p2""$p1""$NUM".sgf
    fi
    if [ "$c" == "@" ]; then
        printf '%b\n' ";B[\\$(printf '0%o' "$VAR1")\\$(printf '0%o' "$VAR2")]" >> ./Tsumego/"$p2""$p1""$NUM".sgf
    fi
    if [ "$c" == "g" ]; then
        VAR1=96
        VAR2=97
        echo "(" >> ./Tsumego/"$p2""$p1""$NUM".sgf
    fi
    if [ "$c" == "k" ]; then
        echo ")" >> ./Tsumego/"$p2""$p1""$NUM".sgf
        NUM=`expr $NUM + 1`
        if [ $NUM -gt 9 ]; then
                p1=""
        fi
        if [ $NUM -gt 99 ]; then
                p2=""
        fi

    fi
done < $1