summaryrefslogtreecommitdiff
path: root/config/update_shoplist.sh
blob: ab42825243572ef1e43dd4a48ce6cc74a44e747f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash

# this script merges new items into an existing shoplist and keeps the current values

ITEM_DB_DIR="/mnt/E/TMW/git.themanaworld.org/#HoraK-FDF/legacy/serverdata/world/map/db"
SHOP_LIST="shoplist.txt"
CURRENT_SHOP_LIST="shoplist.txt"

SORT_MODE="1"
# 0 = no sort
# 1 = sort by id
# 2 = sort by item name

if [ ! -e "$ITEM_DB_DIR" ]; then
 echo "warning the directory \""$ITEM_DB_DIR"\" does not exist ..."
 echo "... exiting!"
 exit 1
fi

if [ -e "$SHOP_LIST" ]; then
 echo "warning the file "$SHOP_LIST" allready exists ..."
 echo "... enter y/Y to overwrite it : "
 read KEY
 if [ ! $KEY = "y" ] && [ ! $KEY = "Y" ]; then
  echo "... exiting!"
  exit 0
 fi
fi

NEW_ITEMS=`cat "$ITEM_DB_DIR"/item_db* | grep -o -E "^[1-9][0-9]*[,][ ]+[a-zA-Z]*[,]" | sed -r -e 's/[,][ ]*/ /g' -e 's/[ ]*$//g' -e 's/ / 0 0 0 0 /g'`
OLD_ITEMS=`cat "$CURRENT_SHOP_LIST"`

OLD_IFS=$IFS
IFS=$'\n'

FIRST_CYCLE=1

for i in $NEW_ITEMS; do
 ITEM_ID=`echo $i | cut -d ' ' -f 1`
 IS_IN_OLD_ITEMS=`echo "$OLD_ITEMS" | grep -E "^"$ITEM_ID"[ ]"`
 if [ ! "$IS_IN_OLD_ITEMS" = "" ]; then
  # item_id  buy_amount buy_price sell_amount sell_price comment/itemname(optional)
  BUY_AMOUNT=`echo "$IS_IN_OLD_ITEMS" | cut -d ' ' -f 2`
  BUY_PRICE=`echo "$IS_IN_OLD_ITEMS" | cut -d ' ' -f 3`
  SELL_AMOUNT=`echo "$IS_IN_OLD_ITEMS" | cut -d ' ' -f 4`
  SELL_PRICE=`echo "$IS_IN_OLD_ITEMS" | cut -d ' ' -f 5`
  ITEM_NAME=`echo "$i" | cut -d ' ' -f 6`
  MERGED_ITEMS=""$MERGED_ITEMS""$ITEM_ID" "$BUY_AMOUNT" "$BUY_PRICE" "$SELL_AMOUNT" "$SELL_PRICE" "$ITEM_NAME""$'\n'
 else
  MERGED_ITEMS=""$MERGED_ITEMS""$i""$'\n'
 fi
done

IFS=$OLD_IFS

case $SORT_MODE in
 0)
  echo -n "$MERGED_ITEMS" > "$SHOP_LIST"
 ;;
 1)
  echo -n "$MERGED_ITEMS" | sort -t " " -k 1 -g > "$SHOP_LIST"
 ;;
 2)
  echo -n "$MERGED_ITEMS" | sort -t " " -k 2 > "$SHOP_LIST"
 ;;
 *)
  echo "wrong value for variable SORT_MODE!"
  exit 1
esac

exit 0