#!/usr/bin/ksh93

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

# Called from f_FrontPanelAction to populate Front Panel Action Icons

Button="$1"

SysActionFile="$NSCDE_DATADIR/defaults/FrontPanel.actions"
if [ -r "$FVWM_USERDIR/FrontPanel.actions" ]; then
   UserActionFile="$FVWM_USERDIR/FrontPanel.actions"
else
   UserActionFile="/dev/null"
fi

if [ "$Button" == "pre" ]; then
   for btn in {1..20}
   do
      echo "InfoStoreRemove FP.Btn${btn}.type"
      echo "InfoStoreRemove FP.Btn${btn}.content"
   done

   egrep "^Btn.*,__APPLET__,M1,.*,.*" "$UserActionFile" | while IFS="," read button icon mousebutton check cmd
   do
      if [ "$mousebutton" == "M1" ] && [ "${icon}" == "__APPLET__" ]; then
         echo "InfoStoreAdd FP.${button}.type applet"
         echo "InfoStoreAdd FP.${button}.content ${check//\|/,}"
      fi
   done

   # Hardcoded special case to choose pcmanfm or pcmanfm-qt as Application Manager
   # because this two are only that fits the role. If none of this is installed,
   # editor icon and default editor will be placed on the button.
   Btn8FromUser=$(egrep "^Btn.*,__ICON8__,M1,__SPECIAL8__,__APPCMD8__$" "$UserActionFile")
   if (($? == 0)); then
      ParseFile="$UserActionFile"
   else
      ParseFile="$SysActionFile"
   fi
   egrep "^Btn.*,__ICON8__,M1,__SPECIAL8__,__APPCMD8__$" "$ParseFile" | while IFS="," read button icon mousebutton check cmd
   do
      if [ "$icon" == "__ICON8__" ]; then
         if [ "x$NSCDE_HAS_APPMGR" == "x1" ]; then
            icon="NsCDE/Fpapps.l.pm"
         else
            icon="NsCDE/Fppenpd.l.pm"
         fi
      fi
      echo "InfoStoreAdd FP.${button}.type icon"
      echo "InfoStoreAdd FP.${button}.content \"Icon ${icon}\""
   done

   egrep -i "^Btn.*,*.(x?pm|png),M1,.*,.*" "$UserActionFile" | while IFS="," read button icon mousebutton check cmd
   do
      if [ "$mousebutton" == "M1" ] && [ "${icon}" != "__APPLET__" ]; then
         echo "InfoStoreAdd FP.${button}.type icon"
         echo "InfoStoreAdd FP.${button}.content \"Icon ${icon}\""
      fi
   done
   # This is needed for Btn8 - special case
   egrep -i "^Btn.*,__APPLET__,M1,.*,.*" "$UserActionFile" | while IFS="," read button icon mousebutton check cmd
   do
      if [ "$mousebutton" == "M1" ] && [ "${icon}" == "__APPLET__" ]; then
         echo "InfoStoreAdd FP.${button}.type applet"
         echo "InfoStoreAdd FP.${button}.content ${check//\|/,}"
      fi
   done
else
   ActionLine=$(egrep "^${Button},.*,.*,.*,.*" $UserActionFile $SysActionFile | head -1)
   if [ "x${ActionLine}" != "x" ]; then
      echo $ActionLine | while IFS="," read button icon mousebutton check cmd
      do
         if [ "$mousebutton" == "M1" ] && [ "$icon" != "__APPLET__" ]; then
            if [ "$icon" == "" ]; then
               icon="NsCDE/None.xpm"
            fi
            # Hardcoded special case to choose pcmanfm or pcmanfm-qt as Application Manager
            # because this two are only that fits the role. If none of this is installed,
            # editor icon and default editor will be placed on the button.
            if [ "$icon" == "__ICON8__" ]; then
               if [ "x$NSCDE_HAS_APPMGR" == "x1" ]; then
                  icon="NsCDE/Fpapps.l.pm"
               else
                  icon="NsCDE/Fppenpd.l.pm"
               fi
            fi
            if [ "$When" != "pre" ]; then
               echo "SendToModule FrontPanel ChangeButton $Button Icon $icon"
            fi
         fi
         if [ "$mousebutton" == "M1" ] && [ "${icon}" == "__APPLET__" ]; then
            egrep -q "^${Button},__APPLET__,.*,.*,.*" $SysActionFile
            if (($? != 0)); then
               echo "InfoStoreAdd FP.${Button}.type applet"
               echo "InfoStoreAdd FP.${Button}.content ${check//\|/,}"
            fi
         fi
      done
   else
      echo "Nop"
   fi
fi

