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

prog=$( basename $0 .bash )
PREFIX=/var/local
ROLE=base
. /usr/local/etc/testforge/testforge.bash || exit 1
. /usr/local/bin/usr_local_base.bash || exit 2
USAGE="$0 role-directory"
#echo 1
[ $# -ge 1 ] || { ERROR $USAGE ; exit 3 ; }

[ -n "$TESTFORGE_ANSIBLE_SRC" ] || TESTFORGE_ANSIBLE_SRC=/g/TestForge/src/ansible
base=`cat /etc/hostname`
json="$TESTFORGE_ANSIBLE_SRC"/tmp/Hosts/$base.json
if [ -d "$TESTFORGE_ANSIBLE_SRC" ] ; then
    if [ ! -s $json ] || [ "$TESTFORGE_ANSIBLE_SRC"/hosts.yml -nt $json ] ; then
	/usr/local/sbin/gentoo_hosts_json.bash || exit 4$?
    fi
fi

BOX_OS_FLAVOR=`/usr/local/sbin/gentoo_hosts_json.bash` || exit 4$?

# "BOX_PYTHON3_MINOR": "3.10"
PYTHON3_VER=`grep BOX_PYTHON3_MINOR <$json|sed -e 's/.*\.//' -e 's/,//' -e 's/"//'`

TASKS="Gentoo/$BOX_OS_FLAVOR"
YAML_HEAD="# -*- mode: yaml; tab-width: 0; coding: utf-8-unix -*-
# This is an automatically generated file: do not edit

---

"""
TEXT_HEAD="# -*- mode: text; tab-width: 8; coding: utf-8-unix -*-

"""

for dir in $* ; do
    [ -d  $dir ] || { ERROR $dir ; exit 5 ; }
    [ -d  $dir/vars ] || { WARN not a roles directory $dir/vars ; }
    YAML_BODY="- name: \"\\1\"\n  blockinfile:\n    dest: \\1\n    create: true\n    marker: \"# {mark} Ansible Managed Block $dir \\2\"\n    block: |"
    for port in use mask unmask license accept_keywords ; do
        # DBUG  $dir $port
	cp /dev/null $dir/vars/$port.txt
	grep '^  *- .*/' $dir/vars/Gentoo2.yml \
	  | sed -e 's@^  *- @@' -e 's@ .*@@' \
	  | uniq \
	  | while read elt ; do
	    base=`basename $elt`
	    # override with $dir/tasks/$TASKS/portage.yml
	    file=$dir/tasks/$TASKS/portage.yml
	    [ -f $file ] && \
		grep -Hq "^ *[=>]*$elt" $file && \
		DBUG $port - "^ *[=>]*$elt" is already in $file && \
		continue
	    file=$dir/vars/$port.txt
	    [ -f $file ] && \
		grep -Hq "^ *[=>]*$elt" $file && \
		DBUG "^ *[=>]*$elt"  is already in $file
		# && continue
	    grep "^[=<>]*$elt" /etc/portage/package.$port/2*txt \
	      | sed -e 's@^@# @' -e 's@.txt:@.txt '$base'\n@' \
	      | sed -e 's@^\([^#][^ ]*\)@      \1%@' -e 's@^#@\n#@' \
	      >> $dir/vars/$port.txt || exit 6
	    #? grep "^#.*required by $elt" /etc/portage/package.$port/2*txt
	    
	done
#echo 2
	if [ ! -s $dir/vars/$port.txt ] ; then
	    rm -f $dir/vars/$port.txt $dir/tasks/$TASKS/$port.yml
	    continue
	  fi

	INFO $port lines `wc -l $dir/vars/$port.txt`
        [ -d $dir/tasks/$TASKS ] || { ERROR no dir $dir/tasks/$TASKS ; continue ; }
#echo 3
	to_yaml=$dir/tasks/$TASKS/$port.yml.new
	# overwrite if its there as .new
	echo "$YAML_HEAD" > $to_yaml
	# sed -e "s/\"\n    block: |/ $base\0/"
	sed -e 's@^# \([^ ]*\) \([^ /]*\)$@'"$YAML_BODY"'@' $dir/vars/$port.txt \
	    >> $to_yaml || exit 7
	# | sed -e "s/BLOCK $dir/BLOCK $dir $base/"
	if [ "$port" = "use" -o "$port" = "license" ] ; then
	    sed -e 's@^    [=<>][=<>]*\([^:<>=]*\)-[0-9][-0-9.]*%@    \1@' -i $to_yaml || exit 8
	    sed -e 's@^    [>=][>=]*\([^%]*\)%@    \1@' -i $to_yaml || exit 9
	elif [ "$port" = "mask" -o "$port" = "unmask" -o "$port" = "accept_keywords" ] ; then
	    sed -e 's@^    \([^:]*\)/\([^%]*\)%@    \1/\2@' -i $to_yaml || exit 10
	fi
        sed -e 's@^    \([^:\n]*\)/\([^%\n]*\)%@    \1/\2@' -i $to_yaml || exit 11
	echo >> $to_yaml
#echo 5
	grep % $to_yaml && ERROR "% in $to_yaml" && exit 12
	grep -q "^- [importinclude]*_tasks:.*$port.yml" $dir/tasks/$TASKS/main.yml || { \
	   echo "- include_tasks: $TASKS/$port.yml" >> $dir/tasks/$TASKS/main.yml
	   WARN "ADDED $port.yml to $dir/tasks/$TASKS/main.yml"
        }
	if [ ! -f $dir/tasks/$TASKS/$port.yml ] ; then
	   mv $to_yaml $dir/tasks/$TASKS/$port.yml
	   INFO created $dir/tasks/$TASKS/$port.yml
	elif diff -qw $to_yaml $dir/tasks/$TASKS/$port.yml ; then
	    rm -f $to_yaml
	    # DBUG unchanged $to_yaml
	else
	    : diff is verbose
	  fi
     done | sed -e 's/ and / /' -e 's/ differ$//' -e 's/^Files/mv/'
done
exit 0
