#!/usr/bin/ksh

#
# This file is a part of the NsCDE - Not so Common Desktop Environment
# Author: Hegel3DReloaded
# Licence: GPLv3
#

if [ "x$1" == "x" ]; then
   echo "NOARG"
   exit 1
else
   InfVarName="$1"
fi

if [ "x$2" == "x" ]; then
   echo "NOARG"
   exit 1
else
   CatName="$2"
fi

if [ ! -r "$NSCDE_DATADIR/fallback/app-catalog/$CatName" ]; then
   echo "NOCATALOG"
   exit 2
fi

# For browser, first try environment, then XDG
if [ "$CatName" == "www-browser" ]; then
   if [ "x$BROWSER" != "x" ]; then
      whence -q $BROWSER
      if (($? == 0)); then
         echo $BROWSER
         exit 0
      fi
   fi

   whence -q xdg-settings
   if (($? == 0)); then
      candidate_raw=$(xdg-settings get default-web-browser)
      candidate="${candidate_raw%.desktop}"
      if [ "x${candidate}" != "x" ]; then
         for c in "${candidate}" "${candidate}-stable" "${candidate}-bin" "${candidate}.bin"
         do
            whence -q "${c}"
            if (($? == 0)); then
               echo "${c}"
               exit 0
            fi
      done
      fi

      candidate_raw=$(xdg-settings get default-url-scheme-handler htts)
      candidate="${candidate_raw%.desktop}"
      if [ "x${candidate}" != "x" ]; then
         for c in "${candidate}" "${candidate}-stable" "${candidate}-bin" "${candidate}.bin"
         do
            whence -q "${c}"
            if (($? == 0)); then
               echo "${c}"
               exit 0
            fi
         done
      fi
   
      candidate_raw=$(xdg-settings get default-url-scheme-handler http)
      candidate="${candidate_raw%.desktop}"
      if [ "x${candidate}" != "x" ]; then
         for c in "${candidate}" "${candidate}-stable" "${candidate}-bin" "${candidate}.bin"
         do
            whence -q "${c}"
            if (($? == 0)); then
               echo "${c}"
               exit 0
            fi
         done
      fi
   fi
   
   whence -q xdg-mime
   if (($? == 0)); then
      candidate_raw=$(xdg-mime query default x-scheme-handler/https)
      candidate="${candidate_raw%.desktop}"
      if [ "x${candidate}" != "x" ]; then
         for c in "${candidate}" "${candidate}-stable" "${candidate}-bin" "${candidate}.bin"
         do
            whence -q "${c}"
            if (($? == 0)); then
               echo "${c}"
               exit 0
            fi
         done
      fi
   
      candidate_raw=$(xdg-mime query default x-scheme-handler/http)
      candidate="${candidate_raw%.desktop}"
      if [ "x${candidate}" != "x" ]; then
         for c in "${candidate}" "${candidate}-stable" "${candidate}-bin" "${candidate}.bin"
         do
            whence -q "${c}"
            if (($? == 0)); then
               echo "${c}"
               exit 0
            fi
         done
      fi
   fi
fi

if [ "$CatName" == "mailreader" ]; then
   whence -q xdg-mime
   if (($? == 0)); then
      candidate_raw=$(xdg-mime query default x-scheme-handler/mailto)
      candidate="${candidate_raw%.desktop}"
      if [ "x${candidate}" != "x" ]; then
         for c in "${candidate}" "${candidate}-stable" "${candidate}-bin" "${candidate}.bin"
         do
            whence -q "${c}"
            if (($? == 0)); then
               if [ "$c" != "mutt" ]; then
                  echo "${c}"
               else
                  echo "\$[infostore.terminal] -e ${c}"
               fi
               exit 0
            fi
         done
      fi
   fi
fi

found=0
cat "$NSCDE_DATADIR/fallback/app-catalog/$CatName" | while read appname
do
   echo "$appname" | egrep -q '^\$\[infostore\..*\]'
   if (($? == 0)); then
      found=1
      echo "$appname"
      break
   fi

   # appname: top -c 3:needterm
   appname0="${appname%% *}"         # top
   appname1="${appname#* }"          # -c 3:needterm
   appname2="${appname1/:needterm/}" # -c 3
   appname3="${appname##*:}"         # needterm

   if [ "${appname3}" == "needterm" ]; then
      appname4="${appname/:needterm/}"
      if $(whence -q ${appname4}); then
         echo "\$[infostore.terminal] -e $appname4"
         found=1
         break
      fi
   fi

   if $(whence -q ${appname0}); then
      if [ "${appname3}" == "needterm" ]; then
         appname4="${appname/:needterm/}"
         echo "\$[infostore.terminal] -e $appname4"
      else
         echo $appname
      fi
      found=1
      break
   fi
done

if (($found == 0)); then
   echo "nscde_fvwmclnt 'f_NoApp NOAPP $InfVarName'"
fi

