#!/bin/bash

CHAIN=mainnet
ACTION=
ACCOUNT=
PRI_PASS=
PUB_PASS=
NUM_ADDR=10

# Step 0: Parsing args.
usage() {
  cat <<EOF 1>&2
Usage: $0 [-c CHAIN(mainnet|testnet|simnet)] -a ACTION(create|import) [-m MNEMONICS] [-r NUM_ADDR]
          -x PRIVATE_PASSPHRASE -y PUBLIC_PASSPHRASE ACCOUNT
EOF
}

exit_abnormal() {
  usage
  exit 1
}

while getopts "c:a:x:y:m:r:" opts; do
  case "${opts}" in
    c)
      CHAIN=${OPTARG}
      if [[ "$CHAIN" != "mainnet" && "$CHAIN" != "testnet"  && "$CHAIN" != "simnet" ]]; then
        exit_abnormal
      fi
      ;;
    a)
      ACTION=${OPTARG}
      if [[ "$ACTION" != "create" && "$ACTION" != "import" ]]; then
        exit_abnormal
      fi
      ;;
    x)
      PRI_PASS=${OPTARG}
      ;;
    y)
      PUB_PASS=${OPTARG}
      ;;
    m)
      MNEMONICS=${OPTARG}
      ;;
    r)
      NUM_ADDR=${OPTARG}
      ;;
    *)
      exit_abnormal
      ;;
  esac
done
shift $((OPTIND-1))
ACCOUNT=$1

if [[ -z "$ACTION" || -z "$ACCOUNT" || -z "$PRI_PASS" || -z "$PUB_PASS" ]]; then
  exit_abnormal
fi

# Step 1: Set variables.
SCRIPT=$(perl -e "use Cwd qw(abs_path);print abs_path('$0'),\"\n\";")
SCRIPT_DIR="$(dirname "$SCRIPT")"
RELEASE_DIR=$ABEWALLET_RELEASE_HOME
PROG="$RELEASE_DIR/abewallet"
LIB_DIR="$RELEASE_DIR/lib"

# Step 2: Make a temporary working directory and generate scripts.
WORK_DIR=$(mktemp -d -t abelwallet.$ACCOUNT.XXXXX)
JOB_SCRIPT_NAME="make-account-$ACCOUNT@$CHAIN.sh"
TMP_WALLET_DATA_DIR="$WORK_DIR/wallet-data"

CHAIN_OPT=""
if [[ "$CHAIN" == "testnet" ]]; then
  CHAIN_OPT="--testnet"
elif [[ "$CHAIN" == "simnet" ]]; then
  CHAIN_OPT="--simnet"
fi

cat <<EOF >"$WORK_DIR/$JOB_SCRIPT_NAME"
#!/bin/bash

if [[ "$OSTYPE" == *"darwin"* ]]; then
  export DYLD_LIBRARY_PATH="$LIB_DIR"
elif [[ "$OSTYPE" == *"linux"* ]]; then
  export LD_LIBRARY_PATH="$LIB_DIR"
else
  >&2 echo "ERROR: Unknown platform ($OSTYPE)."
  exit 1;
fi

"$PROG" \\
  --appdata="$TMP_WALLET_DATA_DIR" \\
  --logdir="$TMP_WALLET_DATA_DIR/logs" \\
  --noninteractivecreate $CHAIN_OPT \\
  \$*
EOF
chmod +x "$WORK_DIR/$JOB_SCRIPT_NAME"

# Step 3: Define and execute.
do_create_account() {
  $WORK_DIR/$JOB_SCRIPT_NAME \
    --mypassword=$PRI_PASS \
    --mywalletpass=$PUB_PASS
}

do_import_account() {
  $WORK_DIR/$JOB_SCRIPT_NAME \
    --mypassword=$PRI_PASS \
    --mywalletpass=$PUB_PASS \
    --myversion=0 \
    --withmnemonic \
    --mymnemonic=$MNEMONICS \
    --myrestorenumber=$NUM_ADDR
}

cd "$WORK_DIR"
if [[ "$ACTION" == "create" ]]; then
  output="$(do_create_account 2>"$WORK_DIR/script.err")"
elif [[ "$ACTION" == "import" ]]; then
  output="$(do_import_account 2>"$WORK_DIR/script.err")"
fi

# Step 4: Parse output to get result.
mnemonics=
initial_address=
next_line="nothing"
while IFS= read -r line; do
  if [[ "$(echo $line| grep -o , | grep -c ,)" == "23" ]]; then
    mnemonics="$line"
    next_line="nothing"
    continue
  fi

  if [[ "$next_line" == "initial_address" ]]; then
    initial_address="$line"
    break
  fi

  if [[ "$line" =~ "Please remember the initial address:" ]]; then
    next_line="initial_address"
  fi
done <<<"$output"

# Step 5: Output result to stdout.
echo "$WORK_DIR"
echo $mnemonics
echo ${initial_address:0:20}