#! /bin/sh

OPTION=$1

if [ "X$OPTION" = "X-f" ]; then
  /bin/rm -fr /var/lib/defoma/locked
fi

if ! /bin/ln -s locknow /var/lib/defoma/locked > /dev/null 2>&1; then
  echo "Error: /var/lib/defoma/locked exists."
  echo "Another defoma process seems running, or you aren't root."
  echo "If you are root and defoma process isn't running undoubtedly,"
  echo "it is possible that defoma might have aborted."
  echo "Please run defoma-reconfigure -f to fix its broken status."
  exit 1
else
  /bin/rm -fr /var/lib/defoma/locked
fi

DELETES="postscript pspreview psprint x-postscript xfont"

cd /var/lib/defoma
if [ "`pwd`" != "/var/lib/defoma" ]; then
  echo "failed to chdir /var/lib/defoma."
  exit 1
fi

DIRS=""
for i in *.d; do
  [ "X$i" = "X*.d" ] && exit 0
  DIRS="$DIRS $i"
  cd $i
  /usr/bin/find -type f | /usr/bin/xargs rm -f
  /usr/bin/find -type l | /usr/bin/xargs rm -f
  cd ..
done

for i in $DELETES; do
  [ -f $i.font-cache ] && /bin/rm -f $i.font-cache
  [ -f $i.failed-font-cache ] && /bin/rm -f $i.failed-font-cache
done

/bin/rm -fr *.subst-cache

for i in $DIRS; do
  APP=${i%%.d}
  /usr/bin/defoma-app update $APP
done

exit 0
