#!/usr/bin/env bash

# This script generate automatically an up to date OPCODES and Gas appendix based on
# https://raw.githubusercontent.com/trailofbits/evm-opcodes/master/README.md
# from https://github.com/trailofbits/evm-opcodes

# ^[^|]*(\|[^|]*){3}$

ORIGIN_FILE=https://raw.githubusercontent.com/trailofbits/evm-opcodes/master/README.md

# OPCODES MARKER in the converted asciidoc file
OPCODES_BEGIN_MARKER=Table
echo "The OPCODES_BEGIN_MARKER is $OPCODES_BEGIN_MARKER."

OPCODES_END_MARKER=instruction-details
echo "The OPCODES_END_MARKER is $OPCODES_END_MARKER."

echo "Getting the origin file, converting it and spliting it ..."
wget -q -O - $ORIGIN_FILE | pandoc --columns=200 -t asciidoc | csplit - -q --prefix=appdx-evm-opcodes-gas- -b "%02d.asciidoc" /$OPCODES_BEGIN_MARKER/ /$OPCODES_END_MARKER/

echo "Concatenating header OPCODES and footer ..."
cat appdx-evm-opcodes-gas-header.asciidoc appdx-evm-opcodes-gas-01.asciidoc appdx-evm-opcodes-gas-footer.asciidoc > appdx-evm-opcodes-gas.asciidoc

echo "Removing temp files ..."
rm appdx-evm-opcodes-gas-0*

echo "The new appendix for OPCODES and Gas was autogenerated succesfully!"