#!/bin/bash
MODE=$1
if [[ -z $2 ]];then HEADTAIL_LIMIT=0; else HEADTAIL_LIMIT=$2;fi
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../.."
source scripts/funcs

echo "## Control flow

The exchange rate pipeline with 04_install project had a simple linear workflow.
handoff can implement more complex workflow logic with foreach and fork.
"

Prompt "Ready? (Hit q at prompt to quit anytime. Any other key to proceed.)"

echo "### foreach

Sometimes, you may want to repeat the same task for different entities like accounts.
Once you produce a list of the entity keys such as account ID, handoff can
repeat tasks in a for-each loop.

05_foreach project shows the structure of foreach control flow:
"

Cat "05_foreach/project.yml" $DIM 0

Prompt

echo '
The first task generate_files writes out a list of integers (1, 2, 3, 4, 5).
Then the second command `foreach` receives each ID stored in `_line` variable.
Under the foreach, a sub-task make_id_files is defined to carry out the task
with the input.

The second task, called verify_result, verifies the number of output files
generated by make_id_files.
'

echo "
Now let's run:
"

Command 'handoff run local -p 05_foreach -w workspace_05'

Prompt

echo "
You can verify that 5 output files are created:
"

Run "ls -l workspace_05/artifacts/out_*.txt" $DIM 0

Prompt

echo "### fork

fork command let you fork the pipline. 06_fork project shows an example of
using fork as an extension to our previous exchange rates project 04_install.

project.yml looks like:
"

Prompt

Cat "06_fork/project.yml" $DIM 0

Prompt

echo "
The difference from 04_install is that there are two target-csv
receiving the stdout by tap-exchangerates. The first task, wide-format, dumps
CSV just like 04_install project. The output wide-format table has all the
currencies listed in the columns.

The second task, long-format, dumps CSV by converting to a long format. The
output has date, symbol, and rates columns:

Let's verify. First install workpace:
"

Command 'handoff workspace install -p 06_fork -w workspace_06'

echo "
Now let's run:
"

Command "handoff run local -p 06_fork -w workspace_06 -v start_date=$DATE_MINUS_7"

echo "
The long format CSV file looks like this:
"

Run 'for f in `ls workspace_06/artifacts/exchange_rate_long_format*.csv`; do head -n 3 $f; done' $DIM 3

Prompt

Thanks

echo "
Now that we know how to develop and run the pipeline locally, we will gradually
start thinking about how to deploy this in the cloud *severlessly*.
We will learn how to save and fetch the configurations to the remote storage.
Before doing that, we will cover how to set up AWS account and profile in the
next section.
"

Continue scripts/aws_get_started/04_set_up_aws_account
