#!/bin/sh

cd "$(dirname $0)"
SCRIPT_DIR="$PWD"

XRESLOADER="$(ls -t ../target/xresloader-*-jar-with-dependencies.jar 2>/dev/null | head -n 1)"
if [ -z "$XRESLOADER" ] || [ ! -e "$XRESLOADER" ]; then
  XRESLOADER="$(ls -t ../target/xresloader-*.jar 2>/dev/null | head -n 1)"
fi

if [ -z "$XRESLOADER" ] || [ ! -e "$XRESLOADER" ]; then
  echo "xresloader not found."
  exit 0
fi

echo "Using xresloader=$XRESLOADER"

for proto_dir in proto_v2 proto_v3; do
  echo "Generate sample data for $proto_dir, one per cmd"
  XLSX_FILE="$(ls *.xlsx | grep -v 'grep' | grep -v "\\~\\$")"
  find "$proto_dir" -iname "UnreaImportSettings.json" -exec rm -fv "{}" ";"
  java -client -jar "$XRESLOADER" -t lua -p protobuf -o $proto_dir -f $proto_dir/kind.pb --pretty 2 -c kind_const.lua
  java -client -jar "$XRESLOADER" -t lua -p protobuf -o $proto_dir -f $proto_dir/kind.pb --pretty 2 --lua-module ProtoEnums.Kind -c kind_const_module.lua
  java -client -jar "$XRESLOADER" -t bin -p protobuf -o $proto_dir -f $proto_dir/kind.pb -s $XLSX_FILE -m scheme_kind -a 1.0.0.0
  java -client -jar "$XRESLOADER" -t lua -p protobuf -o $proto_dir -f $proto_dir/kind.pb --pretty 4 -s $XLSX_FILE -m scheme_kind -n "/(?i)\.bin\$/\.lua/" --data-version 1.0.0.0
  java -client -jar "$XRESLOADER" -t lua -p protobuf -o $proto_dir -f $proto_dir/kind.pb --lua-module ProtoData.Kind -s $XLSX_FILE -m scheme_kind -n "/(?i)\.bin\$/_module\.lua/" --data-version 1.0.0.0
  echo "Generate sample data for $proto_dir, batchmode using --stdin"
  CMDS='
        -t lua -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' --pretty 2 -i kind.desc.lua --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t json -p protobuf -o '$proto_dir'    -f '$proto_dir/kind.pb' --pretty 2 -i kind.desc.json --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t json -p protobuf -o '$proto_dir'    -f '$proto_dir/kind.pb' -s '$XLSX_FILE' -m scheme_kind -n "/(?i)\.bin$/\.json/" --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t xml -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' -s '$XLSX_FILE' -m scheme_kind -n "/(?i)\.bin$/\.xml/" --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t msgpack -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' -s '$XLSX_FILE' -m scheme_kind -n "/(?i)\.bin$/\.msgpack.bin/" --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t js -p protobuf -o '$proto_dir'      -f '$proto_dir/kind.pb' --pretty 2 -s '$XLSX_FILE' -m scheme_kind -n "/(?i)\.bin$/\.js/" --javascript-global sample.xresloader  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t js -p protobuf -o '$proto_dir'      -f '$proto_dir/kind.pb' --pretty 2 -m DataSource='$XLSX_FILE'|kind|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=role_cfg -m OutputFile=role_cfg.n.js -m KeyRow=2 -m KeyCase=lower -m KeyWordSplit=_ -m "KeyWordRegex=[A-Z_\$ \t]|[_\$ \t]|[a-zA-Z_\$]" --javascript-export nodejs  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t js -p protobuf -o '$proto_dir'      -f '$proto_dir/kind.pb' --pretty 2 -s '$XLSX_FILE' -m scheme_kind -n "/(?i)\.bin$/\.amd\.js/" --javascript-export amd  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml --pretty 2 -m DataSource='$XLSX_FILE'|arr_in_arr|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=arr_in_arr_cfg -m OutputFile=arr_in_arr_cfg.lua -m KeyRow=2   --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|arr_in_arr|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=arr_in_arr_cfg -m OutputFile=arr_in_arr_cfg.bin -m KeyRow=2  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t xml -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml --pretty 2 -m DataSource='$XLSX_FILE'|arr_in_arr|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=arr_in_arr_cfg -m OutputFile=arr_in_arr_cfg.xml -m KeyRow=2  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml --pretty 2 -m DataSource='$XLSX_FILE'|arr_in_arr|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=arr_in_arr_cfg -m OutputFile=arr_in_arr_cfg.hidden.lua --ignore-field-tags server_only -m KeyRow=2  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|arr_in_arr|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=arr_in_arr_cfg -m OutputFile=arr_in_arr_cfg.hidden.bin --ignore-field-tags server_only -m KeyRow=2  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' -s '$XLSX_FILE' --validator-rules custom_validator.yaml --data-validator-error-version 5 -m scheme_upgrade  --disable-excel-formular  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t json -p protobuf -o '$proto_dir'    -f '$proto_dir/kind.pb' -s '$XLSX_FILE' --validator-rules custom_validator.yaml --data-validator-error-version 5 -m scheme_upgrade -n "/(?i)\.bin$/\.json/" --pretty 2 --disable-excel-formular  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -p protobuf -o '$proto_dir'     -f '$proto_dir/kind.pb' -s '$XLSX_FILE' --validator-rules custom_validator.yaml --data-validator-error-version 5 -m scheme_upgrade -n "/(?i)\.bin$/\.lua/" --pretty 2 --disable-excel-formular  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -o '$proto_dir'     -f '$proto_dir/kind.pb' --pretty 2 -i kind_option.lua  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -o '$proto_dir'     -f '$proto_dir/kind.pb' --pretty 2 --lua-module ProtoOptions.Kind -i kind_option.mod.lua  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t js -o '$proto_dir'      -f '$proto_dir/kind.pb' --pretty 2 -i kind_option.js --javascript-export nodejs  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' --pretty 2 --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|test_oneof|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=event_cfg -m OutputFile=event_cfg.bin -m KeyRow=2  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' --pretty 2 --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|test_oneof|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=event_cfg -m OutputFile=event_cfg.lua -m KeyRow=2  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' --pretty 2 --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|test_oneof|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=event_cfg -m OutputFile=event_cfg.hidden.bin --ignore-field-tags server_only -m KeyRow=2  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' --pretty 2 --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|test_oneof|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=event_cfg -m OutputFile=event_cfg.hidden.lua --ignore-field-tags server_only -m KeyRow=2  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-json -o '$proto_dir/json' -f '$proto_dir/kind.pb' -c KindConst.json  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-json -o '$proto_dir/json' -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|arr_in_arr|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=arr_in_arr_cfg -m OutputFile=ArrInArrCfg.json -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-json -o '$proto_dir/json' -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml --data-validator-error-version 5 -m DataSource='$XLSX_FILE'|upgrade_10001|3,1 -m DataSource='$XLSX_FILE'|upgrade_10002|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=role_upgrade_cfg -m OutputFile=RoleUpgradeCfg.json -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-csv -o '$proto_dir/csv' -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|arr_in_arr|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=arr_in_arr_cfg -m OutputFile=ArrInArrCfgRec.csv -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec -m UeCfg-EnableDefaultLoader=false  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-csv -o '$proto_dir/csv' -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml --data-validator-error-version 5 -m DataSource='$XLSX_FILE'|upgrade_10001|3,1 -m DataSource='$XLSX_FILE'|upgrade_10002|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=role_upgrade_cfg -m OutputFile=RoleUpgradeCfgRec.csv -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-json -o '$proto_dir/json' -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|test_oneof|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=event_cfg -m OutputFile=EventCfg.json -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec -m UeCfg-IncludeHeader=Engine/CompositeDataTable.h  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-csv -o '$proto_dir/csv' -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|test_oneof|3,1 -m MacroSource='$XLSX_FILE'|macro|2,1 -m ProtoName=event_cfg -m OutputFile=EventCfgRec.csv -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec -m UeCfg-IncludeHeader=Engine/CompositeDataTable.h  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' --pretty 2 -m DataSource='$XLSX_FILE'|keep_or_strip_empty_list|3,1 -m ProtoName=keep_or_strip_empty_list_cfg -m OutputFile=keep_empty_list_cfg.lua -m KeyRow=2 --list-keep-empty  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-json -o '$proto_dir/json' -f '$proto_dir/kind.pb' -m DataSource='$XLSX_FILE'|keep_or_strip_empty_list|3,1 -m ProtoName=keep_or_strip_empty_list_cfg -m OutputFile=KeepEmptyListCfg.json -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec --list-keep-empty  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-csv -o '$proto_dir/csv' -f '$proto_dir/kind.pb' -m DataSource='$XLSX_FILE'|keep_or_strip_empty_list|3,1 -m ProtoName=keep_or_strip_empty_list_cfg -m OutputFile=KeepEmptyListCfg.csv -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec --list-keep-empty  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' -m DataSource='$XLSX_FILE'|keep_or_strip_empty_list|3,1 -m ProtoName=keep_or_strip_empty_list_cfg -m OutputFile=keep_empty_list_cfg.bin -m KeyRow=2 --list-keep-empty  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' --pretty 2 -m DataSource='$XLSX_FILE'|keep_or_strip_empty_list|3,1 -m ProtoName=keep_or_strip_empty_list_cfg -m OutputFile=strip_list_tail_cfg.lua -m KeyRow=2 --list-strip-empty-tail  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-json -o '$proto_dir/json' -f '$proto_dir/kind.pb' -m DataSource='$XLSX_FILE'|keep_or_strip_empty_list|3,1 -m ProtoName=keep_or_strip_empty_list_cfg -m OutputFile=StripListTailCfg.json -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec --list-strip-empty-tail  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t ue-csv -o '$proto_dir/csv' -f '$proto_dir/kind.pb' -m DataSource='$XLSX_FILE'|keep_or_strip_empty_list|3,1 -m ProtoName=keep_or_strip_empty_list_cfg -m OutputFile=StripListTailCfg.csv -m KeyRow=2 -m UeCfg-CodeOutput=|Public/ConfigRec|Private/ConfigRec --list-strip-empty-tail  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' -m DataSource='$XLSX_FILE'|keep_or_strip_empty_list|3,1 -m ProtoName=keep_or_strip_empty_list_cfg -m OutputFile=strip_list_tail_cfg.bin -m KeyRow=2 --list-strip-empty-tail  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t json -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' -m CallbackScript=cb_script.js -m DataSource='$XLSX_FILE'|process_by_script1|2,1 -m DataSource='$XLSX_FILE'|process_by_script2|2,1 -m ProtoName=process_by_script -m OutputFile=process_by_script.json -m KeyRow=1 --pretty 2 --list-strip-empty-tail  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin  -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' -m CallbackScript=cb_script.js -m DataSource='$XLSX_FILE'|process_by_script1|2,1 -m DataSource='$XLSX_FILE'|process_by_script2|2,1 -m ProtoName=process_by_script -m OutputFile=process_by_script.bin  -m KeyRow=1 --list-strip-empty-tail  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua  -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' -m CallbackScript=cb_script.js -m DataSource='$XLSX_FILE'|process_by_script1|2,1 -m DataSource='$XLSX_FILE'|process_by_script2|2,1 -m ProtoName=process_by_script -m OutputFile=process_by_script.lua  -m KeyRow=1 --pretty 2 --list-strip-empty-tail  --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin  -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' -m DataSource='$XLSX_FILE'|nest_alias|3,1 -m ProtoName=outer_alias_message -m OutputFile=next_alias.bin -m KeyRow=2 --require-mapping-all --list-keep-empty --enable-alias-mapping --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua  -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' -m DataSource='$XLSX_FILE'|nest_alias|3,1 -m ProtoName=outer_alias_message -m OutputFile=next_alias.lua -m KeyRow=2 --pretty 2 --require-mapping-all --list-keep-empty --enable-alias-mapping --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t bin  -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|field_alias_sheet|3,1 -m ProtoName=field_alias_message -m OutputFile=field_alias.bin -m KeyRow=2 --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
        -t lua  -p protobuf -o '$proto_dir' -f '$proto_dir/kind.pb' --validator-rules custom_validator.yaml -m DataSource='$XLSX_FILE'|field_alias_sheet|3,1 -m ProtoName=field_alias_message -m OutputFile=field_alias.lua -m KeyRow=2 --pretty 2 --data-source-mapping-mode sha256 --data-source-mapping-file '$proto_dir/data_source_mapping.txt'
    '

  echo "Run with --stdin: $CMDS"
  echo "$CMDS" | java -client -jar "$XRESLOADER" --stdin --data-version 1.0.0.0
  echo "Exit code: $?"
done
