#!/bin/dash
#        file: hello_world2
#   copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2007-2018)
#     license: GNU General Public License, version 3
# description: example "hello world" script with function, debug and check
#     warning: normally it makes
#     warning: Be aware that the interactive step size in this example is only
#              reasonable to summanrized demonstrate shellia features.
#              Read shellia(7) NOTES, to learn about a reasonable size of
#              interactive steps.
#       usage: ./hello_world2 [-d] [-i|-s|-m] [--] <name>

# read shellia libraries
. /usr/share/shellia/ia

# create and use logfile
ia_logfile="$(mktemp)"
export ia_logfile

# write_file <path> <name>
write_file()
{
  eval "$ia_init"
  ia_add "echo \"hello $2\" >$1"
  ia -c -x
}

# show_file <path>
show_file()
{
  eval "$ia_init"
  ia_stdout "^hello" # only allow a greeting starting with hello
  ia_add "cat $1"
  ia -c -x
}

# initialize shellia
eval "$ia_init"

# get name from commandline
if [ $# -ne 1 ]; then
  echo "Usage $0 <name>" >&2
  exit 1
fi

ia_add "file=\"\$(mktemp)\""
ia_add "write_file <-i> -- \"\$file\" \"$1\""
ia_stdout "." # allow all kind of output already checked in show_file
ia_add "show_file <-i> -- \"\$file\""
ia_add "rm -f \"\$file\""
ia -c -x

# output logfile
echo "----"
cat $ia_logfile
echo "----"
rm $ia_logfile
