#!/usr/local/bin/ruby -w
#$Id: changedports.rb 13 2006-08-21 19:02:09Z viciu $

EMAIL='ports@example.com'
MAILHOST='localhost'
HOSTNAME=`hostname`.chomp

TEMPDIR='/tmp/'


require 'pp'
require 'net/smtp'
require 'getoptlong'

Field = Struct.new(:f, :s)	#versions from first and second (:f and :s)

def compareFiles (old, new)
  ports = {};
  noMatch ="";
  begin
     first = File.open(old, "r+")
     second = File.open(new, "r+")
  rescue Exception => msg
     puts "Something wrong... ("+msg+")"
  end


  while ( line = first.gets )
    line = line.chomp
    next if line == "pkgdb.db" 
    
    if line =~ /(.+)-([\d\.,\w_]*)/ then
  #    puts " #$1 v. #$2"
      ports[$1]=Field.new($2,"")
    else
      noMatch << line + "\n"
    end

  end


  while ( line = second.gets )
    line = line.chomp
    next if line == "pkgdb.db" 
    if line =~ /(.+)-([\d\.,\w_]*)/ then
  #    puts " #$1 v. #$2"
      if ports.has_key?($1)
        ports[$1][:s] = $2
      else
        ports[$1]=Field.new("",$2)
      end
    end

  end
  #dumping diff
  
  diffFound = false
  lines = [];
  lines = <<MSG
From: #{EMAIL}
Subject: #{HOSTNAME} ports change
To: #{EMAIL}

MSG

  if  noMatch.length > 0   

    lines << <<MSG
Following file(s) in /var/db/pkg have not been recognized as valid 
package name:

MSG
    lines << noMatch + "\n"
  end
  
  lines << "Changes in installed ports:\n\n"
  ports.keys.sort.each {|x| 
    if not ports[x][:f]  ==  ports[x][:s] 
      if ( not "" == ports[x][:f] and not "" == ports[x][:s] )
        lines << "#{x} have been upgraded from ver "+ports[x][:f]+" to "+ports[x][:s]+"\n"
      else
        if "" == ports[x][:f] 
          lines << "#{x}-#{ports[x][:s]} was installed\n"
        else
          lines << "#{x}-#{ports[x][:f]} was removed\n"
        end
      end
      diffFound = true
    end
  }
  
  if diffFound
    Net::SMTP.start(MAILHOST, 25) do |smtp|
            smtp.send_mail( lines, EMAIL, EMAIL )
    end
  end
end

#******************************
def usage()

  name = File.basename($0)
  puts <<EOF

Simple FreeBSD port watcher. Run it periodically and will report changes
(new versions, installs, removal) in ports on this machine. Uses constant 
temporary file names - may be security concern (condition race).

  Usage: #{name} [options]
  
  --help [-h] 	- this message
  
Edit this script to give proper SMTP server and email address, and if 
needed change temporary directory.

(c) 2006 Witold Rugowski
http://nhw.pl
EOF
 exit 0
end

#######################################################################
#######################################################################

opts = GetoptLong.new(
  ["--help",		"-h",	GetoptLong::NO_ARGUMENT	]
)

opts.each do |opt, arg|
  case opt
    when "--help"
      usage()
  end
end



`ls /var/db/pkg/ > #{TEMPDIR}mylist`

if not File.exists?("#{TEMPDIR}oldlist") 
  `ls /var/db/pkg/ > #{TEMPDIR}oldlist`
end


compareFiles( "#{TEMPDIR}oldlist", "#{TEMPDIR}mylist")

`cat #{TEMPDIR}mylist > #{TEMPDIR}oldlist`


