#!/bin/sh

# clean_userapps.sh
# 2014-04-02
# by Gernot Walzl

# After an update, mozilla products (e.g. firefox) promt to be the default
# application. Outdated desktop entries are not removed automatically.

set -e

cd $HOME/.local/share/applications || exit 1

USERAPPS_REMOVED=""
USERAPPS=$(ls userapp-*.desktop)
for USERAPP in $USERAPPS; do
  EXEC=$(sed -n 's/Exec=\([^ ]*\).*/\1/p' $USERAPP)
  if [ "$(echo $EXEC | cut -c1)" = "/" ]; then
    if [ ! -x "$EXEC" ]; then
      rm -fv $USERAPP
      USERAPPS_REMOVED="$USERAPPS_REMOVED $USERAPP"
    fi
  fi
done

for USERAPP in $USERAPPS_REMOVED; do
  sed -i 's/'$USERAPP';//g' mimeapps.list
done