#!/bin/bash
version=1.2.2

EXT=txz   # pkg extension
SRC=src   # src file extension
SRCS=true # true = list sources separately (requires properly named
          # src file in SLKBUILD dir
# A list of methods name which will be executed in the order listed here
# if there is no arch found in the SLKBUILD. The first successfully
# determined arch will be used.
GUESSARCH_METHODS="guessarchByComment guessarchByUnameM"

usage() {
  cat <<EOF
$(basename $0) [-sug -con <SLKBUILD|dir containing SLKBUILD>]...
generates a suitable sourceforge report from a/multiple SLKBUILD(s) and
outputs it to STDOUT

OPTIONS:
  -v                        outputs version information
  -sug                      include suggests file
  -con                      include conflicts file
  -[32|64|486|686|noarch]   use as arch i486/x86_64/i486/i686/noarch
                            If not passed arch must be be included in
                            the SLKBUILD (optionally as comment) or it's
                            guessed by uname -m
  
EXAMPLES:
  $(basename $0) -sug -con -32 anki/ -sug -32 -64 wxPython/
  Generates the sourceforge report for anki with sug(gests) and
  con(flicts) file and for wxPython only with sug(gests).
  For anki it will only generate a report for i486 and for wxPython for
  both i486 and x86_64.
  
  $(basename $0) conky/SLKBUILD
  Generates the sourceforge report for conky. If there's no arch
  variable set in its SLKBUILD, the script first tries whether there's
  a commented out arch occurence and uses that if available. Otherwise
  the uname -m output of the running system is used.
EOF
exit
}

version() {
  echo "Version: $version"
  exit
}

[ "$1" == "-v" ] && version
slkbuilds="$@"


# everything we don't want to keep for the next slkbuild
cleanup() {
  unset pkgname pkgver pkgrel arch sourcetemplate pkg url CON SUG ARCHS
  thisSRC=$SRC
  thisSRCS=$SRCS
}

import() {
  eval $(grep -xEe '[\ \t]*(pkgname|pkgver|pkgrel|arch|sourcetemplate|url)=.*' "$1" | tr '\n' ';')
  if test -z "$pkgname" || test -z "$pkgver" || test -z "$pkgrel"; then
		echo "Couldn't extract pkgname, pkgver or pkgrel from \`$slkbuild'" >&2
		continue
	fi
}

parse() {
  pkg=$pkgname-$pkgver-$arch-$pkgrel
  srcfile=$(dirname $1)/$pkg.src  # assume that there is a srcfile in the SLKBUILD dir
  sourcetemplate=$(echo $sourcetemplate | sed -e 's,/$,,')  # strip trailing /
  if [ ! -r $srcfile ]; then  # if the srcfile is not accesible we can't add its content
    thisSRC=src; thisSRCS=""
  fi
}

generate() {
  cat <<EOF
'''Homepage:'''
$url

'''Package:'''
EOF
  for ext in md5 $EXT dep $thisSRC $SUG $CON; do
    echo $sourcetemplate/$pkg.$ext
  done
  cat <<EOF

'''Log:'''
$sourcetemplate/build-$pkg.log
EOF

  if [ "$thisSRCS" == "true" ]; then
    # we want sources to be displayed seperatly
    cat <<EOF

'''Buildscript and source:'''
$(cat $srcfile)
EOF
  fi
}

validate() {
  [ -d $slkbuild ] && slkbuild=$slkbuild/SLKBUILD
  [ -r $slkbuild ] || continue
}

guessarchByComment() {
  eval $(grep 'arch=.*' $slkbuild | head -n1 | sed -e 's/#//')
}

guessarchByUnameM() {
  case "$(uname -m)" in
    i?86) export arch=i486 ;;
    arm*) export arch=arm ;;
    *) export arch=$( uname -m ) ;;
  esac
  echo "Automatically setting arch to $arch" >&2
}

# check how many slkbuilds we have
length=0
for slkbuild in $slkbuilds; do
  validate
  ((length++))
done

# if none -> usage
[ $length -eq 0 ] && usage

#otherwise process them
cleanup
i=1
for slkbuild in $slkbuilds; do
  [ "$slkbuild" == "-sug" ] && SUG=sug  # handle -sug
  [ "$slkbuild" == "-con" ] && CON=con  # and -con
  [ "$slkbuild" == "-32" ] && ARCHS+="i486 "
  [ "$slkbuild" == "-486" ] && ARCHS+="i486 "
  [ "$slkbuild" == "-686" ] && ARCHS+="i686 "
  [ "$slkbuild" == "-noarch" ] && ARCHS+="noarch "
  [ "$slkbuild" == "-64" ] && ARCHS+="x86_64 "
  
  validate  
  import $slkbuild  # import variables from SLKBUILD
  
  
  # if there's no arch, first try guessing it
  if [ -z "$arch" ] && [ -n "$GUESSARCH_METHODS" ]; then
    # methods are tried in order and we break after the first success 
    for method in $GUESSARCH_METHODS; do
      $method $slkbuild
      [ -n "$arch" ] && break
    done
  fi
  
  # we could somehow acquire the arch and it's not overwritten by argument
  if [ -n "$arch" ] && [ -z "$ARCHS" ]; then
    parse $slkbuild
    generate $slkbuild
  fi
  
  # arch was passed by argument so we use that one
  if [ -n "$ARCHS" ]; then    
    # precalculate count of archs
    archlength=0
    j=1
    for arch in $ARCHS; do
      ((archlength++))
    done
    
    # generate a report for each arch we're passed
    for arch in $ARCHS; do
      parse $slkbuild
      generate $slkbuild
      # for the last arch no newlines
      [ $archlength -eq $j ] || echo -ne "[[BR]]\n[[BR]]\n[[BR]]\n"
      ((j++))
    done
  fi
  
  if [ -z "$arch" ] && [ -z "$ARCHS" ]; then
    echo "\`$slkbuild' failed: Couldn't acquire arch" >&2
  fi
  
  
  cleanup
  
  # for the last slkbuild no newlines
  [ $length -eq $i ] || echo -ne "[[BR]]\n[[BR]]\n[[BR]]\n"
  ((i++))
done
