#!/bin/sh
set -e

check_sug_con () {
	sug=""
	if [ "$(ls -A *.sug 2>/dev/null)" ]; then
	  sug="-sug"
	fi

	con=""
	if [ "$(ls -A *.con 2>/dev/null)" ]; then
	  con="-con"
	fi
}

for script in `find . -type f -name SLKBUILD` ;do
  (
    cd $(dirname $script)
    dir="$HOME/bugtracker-posts"
    post="$dir/$(basename $(pwd)).post"
    
    if [ ! -d $dir ]; then
      mkdir -p $dir
    fi
    
    if [ -f $post ]; then
      rm $post
    fi

    if [ -f README ]; then
      cat README >> $post
    fi

    check_sug_con

    if [ "$(ls -A *-i486-*.t?z 2>/dev/null)" ]; then
      echo -e "\n'''__32Bit:__'''" >> $post
      slkbuild-postgen -32 $sug $con SLKBUILD >> $post
    fi
    
    if [ "$(ls -A *-x86_64-*.t?z 2>/dev/null)" ]; then
      echo -e "\n'''__64Bit:__'''" >> $post
      slkbuild-postgen -64 $sug $con SLKBUILD >> $post
    fi

    if [ "$(ls -A *-noarch-*.t?z 2>/dev/null)" ]; then
      echo -e "\n'''__noarch:__'''" >> $post
      slkbuild-postgen $sug $con SLKBUILD >> $post
    fi
  )
done
