#!/bin/sh
# Automates the process of installing additional cards and tables

sp=/usr/share/pokerth/data/gfx

insertxml_table () {
    mv ~/.pokerth/config.xml ~/.pokerth/config.tmp
    # Create element, then add attribute
    xml ed -s /PokerTH/Configuration/GameTableStylesList -t elem -n GameTableStyles -v "" \
           -s "/PokerTH/Configuration/GameTableStylesList/GameTableStyles[last()]" -t attr -n value -v "$1" \
            ~/.pokerth/config.tmp > ~/.pokerth/config.xml
}

insertxml_cards () {
    mv ~/.pokerth/config.xml ~/.pokerth/config.tmp
    xml ed -s /PokerTH/Configuration/CardDeckStylesList -t elem -n CardDeckStyles -v "" \
           -s "/PokerTH/Configuration/CardDeckStylesList/CardDeckStyles[last()]" -t attr -n value -v "$1" \
            ~/.pokerth/config.tmp > ~/.pokerth/config.xml
}

insert () {
# Test if xml element exists
if [ "$2" = "table" ]; then 
  data=`xml sel -t -c "/PokerTH/Configuration/GameTableStylesList/GameTableStyles[@value='$1']" ~/.pokerth/config.xml`
else
  data=`xml sel -t -c "/PokerTH/Configuration/CardDeckStylesList/CardDeckStyles[@value='$1']" ~/.pokerth/config.xml`
fi

# If xml does not contain the data, insert in xml if xmlfile exists
if [ -z "$data" ]; then
 Insert in config if xml file exists
    if [ -e "$1" ]; then
      if [ "$2" = "table" ]; then 
        insertxml_table "$1"
      else
        insertxml_cards "$1"
      fi
    fi
fi
}

# Add new tables or cards in the form: insert path-to-xml table|cards
insert "$sp/gui/table/goldenpalms_table/goldenpalmstablestyle.xml" table
insert "$sp/gui/table/mute_table_02/mute_table_02.xml" table
insert "$sp/gui/table/stardust/starduststyle.xml" table
insert "$sp/gui/table/xanax_table_01/xanax_table_01.xml" table
insert "$sp/gui/table/mute_table_01/mute_table_01.xml" table
insert "$sp/gui/table/xanax_table_02/xanax_table_02.xml" table
insert "$sp/gui/table/Red_Felt_ver_0.2/PokerTH Table Red Felt 0.2.xml" table
insert "$sp/gui/table/xanax_table_03/xanax_table_03.xml" table

insert "$sp/cards/stardustcards/stardustdeckstyle.xml" cards
insert "$sp/cards/xanax_card_deck_04/xanax_card_deck_04.xml" cards
insert "$sp/cards/xanax_card_deck_01/xanax_card_deck_01.xml" cards
insert "$sp/cards/xanax_card_deck_02/xanax_card_deck_02.xml" cards

/usr/bin/pokerth-real
cd /
