#!/bin/csh -f

set PORTSTREE = "freebsd-haskell"
set HACKAGEDB = "lang/ghc/bsd.hackage.mk"

set HS_PORTS  = `cat $PORTSTREE/$HACKAGEDB | grep _port= | tr -d "\t " | sed 's/\([^=]*\)=\([^#]*\)\(.*\)/\2/' | sort`

set REPORT  = /dev/stdout
set RPTBODY = /tmp/hs-portwatch.ports

printf "Please, stand by...\n" >> /dev/stderr

fetch -m http://hackage.haskell.org/packages/archive/log >& /dev/null

printf "The list of ports to be updated:\n" >> $REPORT
echo > $RPTBODY

set updates = 0

foreach HPORT ($HS_PORTS)
  set PORT        = $PORTSTREE/$HPORT/Makefile
  set PORTNAME    = `grep "^PORTNAME=" $PORT | cut -f2-`
  set PORTVERSION = `grep "^PORTVERSION=" $PORT | cut -f2-`
  set MAINTAINER  = `grep "^MAINTAINER=" $PORT | cut -f2-`
  set HSNAME      = `echo $PORTNAME | tr "[:lower:]" "[:upper:]"`
  set HSVERSION   = `cat log | tr "[:lower:]" "[:upper:]" | grep " $HSNAME " | sed "s/^.*$HSNAME //" | tr "\n" " " | perl snort.perl | head -1`

  if ( "$HSVERSION" != "" && "$PORTVERSION" != "$HSVERSION" ) then
    printf "\t%-32s %-12s -> %-12s (%s)\n" $PORTNAME $PORTVERSION $HSVERSION $MAINTAINER >> $RPTBODY
    @ updates++
  endif
end

sort -f $RPTBODY >> $REPORT
rm -f $RPTBODY

set s = ""

if ( $updates > 1 ) then
  set s = "s"
endif

printf "\n%d port%s total.\n" $updates $s >> $REPORT
