#!/bin/bash

LANG=C
# FRCODE=/usr/local/libexec/frcode
# DB=~/.pkglocate.database

DB="$1"
REBUILD="$2"
LEOPARD=`sw_vers -productVersion | grep 10.5`

if [ -z "${DB}" ];
then
    echo "No database specified";
    exit -1;
fi


if [ ! -z "${LEOPARD}" ];
then

    # echo "On 10.5"

    if [ -e "${DB}" ] && [ -z "${REBUILD}" ];
    then

        #echo Updating

        find /Library/Receipts/boms/ -name '*.pkg.*.bom' -newer "${DB}" | while read line; do 
           t=${line#/Library/Receipts/boms//com.apple.pkg.}
           pkgname=${t%.bom}
          lsbom -bcdfls "$line" | perl -pe "s|^|$pkgname\t|"
        done

    else

        #echo Creating

        find /Library/Receipts/boms/ -name '*.pkg.*.bom' | while read line; do 
          t=${line#/Library/Receipts/boms//com.apple.pkg.}
          pkgname=${t%.bom}
          lsbom -bcdfls "$line" | perl -pe "s|^|$pkgname\t|"
        done

    fi

else

    # echo "Not on 10.5"

    if [ -e "${DB}" ] && [ -z "${REBUILD}" ];
    then

        #echo Updating

        find /Library/Receipts -name Archive.bom -newer "${DB}" | while read line; do 
           t=${line#/Library/Receipts/}
           pkgname=${t%.pkg/Contents/Archive.bom}
          lsbom -bcdfls "$line" | perl -pe "s|^|$pkgname\t|"
        done

    else

        #echo Creating

        find /Library/Receipts -name Archive.bom | while read line; do 
          t=${line#/Library/Receipts/}
          pkgname=${t%.pkg/Contents/Archive.bom}
          lsbom -bcdfls "$line" | perl -pe "s|^|$pkgname\t|"
        done

    fi

fi