#!/bin/sh
# -*- mode: sh; tab-width: 8; coding: utf-8-unix -*-

prog=$( basename $0 .bash )
PREFIX=/usr/local
ROLE=base
. /usr/local/bin/usr_local_base.bash || exit 2

umask 0022
[ "$#" -gt 0 ] && inidir=$1 || inidir=/usr/local/etc/testforge
[ -f $inidir ] || mkdir -p $inidir

if [ -f $inidir ] ; then
    inifile=$inidir
  else
    inifile=$inidir/testforge.ini
  fi

# echo -n "DEBUG: $prog "; ls -l $inifile
[ -e $inifile ] || { ERROR no file $inifile ; exit 1 ; }
[ -s $inifile ] || { ERROR empty file $inifile ; exit 2 ; }

bashfile=$( echo $inifile | sed -e 's/.ini$/.bash/' )
if [ ! -s $bashfile ] || [ $inifile -nt $bashfile ] ; then
    INFO "$inifile > $bashfile"
    /usr/local/bin/fact_to_bash.bash < $inifile > $bashfile || exit 3
    echo 'export PATH=$PATH:/sbin:/usr/local/bin:/var/local/bin' >> $bashfile
    echo -n "DEBUG: $prog bashfile"; ls -l $bashfile
fi

ymlfile=$( echo $inifile | sed -e 's/.ini$/.yml/' )
if [ ! -s $ymlfile ] || [ $inifile -nt $ymlfile ] ; then
    INFO "$inifile > $ymlfile"
    /usr/local/bin/fact_to_yaml.bash < $inifile > $ymlfile || exit 4
    echo -n "DEBUG: $prog ymlfile "; ls -l $ymlfile
  fi
. $bashfile || exit $?

exec bash /usr/local/bin/base_sheebang_after_pip.bash
