#!/bin/bash
set -a 
# local wiki data dir
cd $dir/data

# clean_web web_name remove_older_than_N_days
clean_web () {
  web=$1
  days=$2
  if test -d data/$web; then
    cd data/$web
    for i in `find . -type f -name \*.txt -mtime +100`; do
      case "$i" in ./Web*): ;;
	*) rm -rf "$i"* ../../pub/$web/${i%.txt} ;;
      esac
    done
    if test -d ../../pub/$web; then
      cd ../../pub/$web
      for i in `find . -type f -mtime +100`; do
	rm -rf "$i"
	i="${i#./}"
	remove_attachement ../../data/$web "${i%%/*}" "${i#*/}"
      done
    fi
  fi
}

remove_attachement () {
  topic="$1/$2".txt; file="$3"
  mv $topic .topic
  fgrep -v "META:FILEATTACHMENT{name=\"$file\"" <.topic >$topic
  rm -f .topic
}

cd $dir
clean_web Trash 10
cd $dir
clean_web Tmp 100
