#!/bin/bash
# Thank you slackware

waitfile="/tmp/updatinggtkfile"

if [[ -f $waitfile ]] ; then

  until [[ ! -f $waitfile ]] ; do
    echo "Waiting for previous GTK cache modules to finish updating"
    sleep 2
  done

else

  touch "$waitfile"

  if [ -x /bin/fc-cache ]; then
    echo "Updating X font indexes..."
   /bin/fc-cache -f &
  fi

  if [ -x /bin/update-desktop-database ]; then
    echo "Updating .desktop files..."
    /bin/update-desktop-database -q /share/applications >/dev/null 2>&1
  fi

  if find /share/icons -maxdepth 2 2> /dev/null | grep -q icon-theme.cache ; then
    for theme_dir in /share/icons/* ; do
      echo "Updating icon-theme.cache in ${theme_dir}..."
      /bin/gtk-update-icon-cache -t -f ${theme_dir} >/dev/null 2>&1
   done
  fi

  if [ -x /bin/update-mime-database ]; then
    echo "Updating MIME database..."
    /bin/update-mime-database -n /share/mime >/dev/null 2>&1 &
  fi

  # Update immodules cache at /lib/gtk-2.0/2.10.0/immodules.cache
  if [ -x /bin/gtk-query-immodules-2.0 ]; then
    echo "Updating GTK2 Immodules..."
    /bin/gtk-query-immodules-2.0 --update-cache > /dev/null 2>&1 &
  fi

  # Update immodules cache at /lib/gtk-3.0/3.0.0/immodules.cache
  if [ -x /bin/gtk-query-immodules-3.0 ]; then
    echo "Updating GTK3 Immodules..."
    /bin/gtk-query-immodules-3.0 --update-cache > /dev/null 2>&1 &
  fi

  if [ -x /bin/gdk-pixbuf-query-loaders ]; then
    echo "Updating GDK Pixbuf loaders..."
    /bin/gdk-pixbuf-query-loaders --update-cache >/dev/null 2>&1 &
  fi

  if [ -x /bin/glib-compile-schemas ]; then
    echo "Compiling GSettings XML scheme files..."
    /bin/glib-compile-schemas /share/glib-2.0/schemas >/dev/null 2>&1 &
  fi
  
  rm -f "$waitfile"

  exit 0
fi
