#!/bin/bash

set -e

DEBS=/srv/local-apt-repository
REPO=/var/lib/local-apt-repository

cat > $REPO/Release <<__END__
Description: Local repository created by local-apt-repository
Origin: local-apt-repository
__END__

if ! test -d $DEBS
then
	# We still need ot create the files lest apt will complain
	> $REPO/Packages
	> $REPO/Sources
	exit 0
fi

# We want to cater for the possibility that something is added to $DEBS as we
# run. In this case, we want to wait a bit restart. But lets bound this to 10 runs.

for n in $(seq 0 10)
do
  if ! test -e  $REPO/stamp || find $DEBS -newer $REPO/stamp | fgrep -q ''
  then
    # lots of stuff happening here, lets wait a while
    if [ "$n" != "0" ]
    then
      sleep 30
    fi

    touch $REPO/stamp
    # we need ot rebuild
    dpkg-scanpackages -m $DEBS/ > $REPO/Packages
    dpkg-scansources $DEBS/ > $REPO/Sources
  fi
done

