#!/usr/bin/ksh93

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

# Function: examine_entrypart
# Purpose: detect and count missing
# entries until next valid one
function examine_entrypart
{
   direction="$1"
   ouraddr="$2"
   addrbefore=$(($ouraddr - 1))
   addrafter=$(($ouraddr + 1))

   if [ "$direction" == "up" ]; then
      addrrange={$addrbefore..1}
   else
      addrrange={$addrafter..$endaddr}
   fi

   for addr in $addrrange
   do
      echo "$entrypart" | sed -n "${addr}p" | while IFS="," read subp esign title check iconfile cmdargs
      do
         cmd="${cmdargs%% *}"
         if [ "$check" != "" ]; then
            if [ "${check%%:*}" == "CHECK" ]; then
               whence -qp "${check#*:}"
               if (($? != 0)); then
                  echo 1
               else
                  break 2
               fi
            else
               break 2
            fi
         fi
         if [ "$check" == "" ]; then
            whence -qp $cmd
            if (($? != 0)); then
               echo 1
            else
               break 2
            fi
         fi
      done
   done
}

function actions_file_sanity
{
   pexist=$(egrep "^S${subpid},(NAME|WIDTH|ENABLED)," "$FVWM_USERDIR/Subpanels.actions" 2>/dev/null | wc -l)
   if (($pexist != 3)); then
      egrep "^S${subpid}," "$NSCDE_DATADIR/defaults/Subpanels.actions" >> "$FVWM_USERDIR/Subpanels.actions"
   fi
}

function subpanels_file_tstamp
{
   REGEN=0
   if [[ -r "$FVWM_USERDIR/Subpanels.actions" && -r "$FVWM_USERDIR/Subpanels.fvwm2.fvwmgen" ]]; then
      if [ "$FVWM_USERDIR/Subpanels.actions" -nt "$FVWM_USERDIR/Subpanels.fvwm2.fvwmgen" ]; then
         echo "File $FVWM_USERDIR/Subpanels.actions is newer than $FVWM_USERDIR/Subpanels.fvwm2.fvwmgen ... regenerating last one." >&2
         $NSCDE_TOOLSDIR/generate_subpanels -w > $FVWM_USERDIR/Subpanels.fvwm2.fvwmgen
         REGEN=1
      fi
   fi
   if [[ -r "$FVWM_USERDIR/Subpanels.actions" && -r "$FVWM_USERDIR/Subpanels.fvwm3.fvwmgen" ]]; then
      if [ "$FVWM_USERDIR/Subpanels.actions" -nt "$FVWM_USERDIR/Subpanels.fvwm3.fvwmgen" ]; then
         echo "File $FVWM_USERDIR/Subpanels.actions is newer than $FVWM_USERDIR/Subpanels.fvwm3.fvwmgen ... regenerating last one." >&2
         $NSCDE_TOOLSDIR/generate_subpanels -f > $FVWM_USERDIR/Subpanels.fvwm3.fvwmgen
         REGEN=1
      fi
   fi
   if (($REGEN == 1)); then
      $NSCDE_ROOT/bin/nscde_fvwmclnt "f_ReadCfg Subpanels"
      sleep 1
      $NSCDE_ROOT/bin/nscde_fvwmclnt "KillModule FvwmButtons NsCDE-Subpanel${subpid}"
      exit 0
   fi
}

function manage_entry
{
   if [ ! -d "$FVWM_USERDIR/tmp" ]; then
      mkdir -p "$FVWM_USERDIR/tmp"
   fi

   export TMPDIR="$FVWM_USERDIR/tmp"

   if [[ -z $NSCDE_DEBUG ]]; then
      tmpfile=$(mktemp)
      exec > "$tmpfile"
   fi

   IFS=" "

   restoffile=$(egrep -v "^S${subpid}," "$FVWM_USERDIR/Subpanels.actions")
   subpinfopart=$(egrep "^S${subpid},(NAME|WIDTH|ENABLED)," "$FVWM_USERDIR/Subpanels.actions")
   entrypart=$(egrep "^S${subpid},ENTRY," "$FVWM_USERDIR/Subpanels.actions")
   endaddr=$(echo "$entrypart" | wc -l)

   if [ "$ACTION" == "up" ]; then
      if (($entryid == 1)); then
         if [[ -n $DISPLAY ]]; then
            $NSCDE_ROOT/bin/nscde_fvwmclnt "f_Notifier \"Subpanel Properties\" \"Dismiss\" \"NsCDE/Info.xpm\" \
                        \"$[gt.Item] '$TITLE' $[gt.is already on the top of the Subpanel] $subpid.\""
         fi
         # Important to exit here, else Subpanels.actions will be clobbered
         exit 0
      else
         empties=$(examine_entrypart up $entryid | wc -l)

         entrybefore=$(($entryid - 2 - $empties))
         entryafter=$(($entryid - 1 - $empties))
         restentries=$(($entryid + 1))
         entryidminusone=$(($entryid - 1))

         if (($entrybefore > 0)); then
            firstpart=$(echo "$entrypart" | sed -n "1,${entrybefore}p")
         else
            firstpart=""
         fi
         secondpart=$(echo "$entrypart" | sed -n "${entryid}p")
         thirdpart=$(echo "$entrypart" | sed -n "${entryafter},${entryidminusone}p")
         restpart=$(echo "$entrypart" | sed -n "${restentries},\$p")

         echo "$restoffile"
         echo ""
         echo "$subpinfopart"

         if (($entrybefore > 0)); then
            echo "$firstpart"
         fi
         echo "$secondpart"
         echo "$thirdpart"

         if [ "x$restpart" != "x" ]; then
            echo "$restpart"
         fi
      fi
   fi

   if [ "$ACTION" == "down" ]; then
      if (($entryid == $endaddr)); then
         if [[ -n $DISPLAY ]]; then
            $NSCDE_ROOT/bin/nscde_fvwmclnt "f_Notifier \"Subpanel Properties\" \"Dismiss\" \"NsCDE/Info.xpm\" \
                        \"$[gt.Item] '$TITLE' $[gt.is already on the bottom of the Subpanel] $subpid.\""
         fi
         # Important to exit here, else Subpanels.actions will be clobbered
         exit 0
      else
         empties=$(examine_entrypart down $entryid | wc -l)

         entrybefore=$(($entryid - 1))
         entryafter=$(($entryid + 1 + $empties))
         entryidplusone=$(($entryid + 1))
         entryidplusempties=$(($entryid + $empties))
         restpart=$(($entryafter + 1))

         firstpart=$(echo "$entrypart" | sed -n "1,${entrybefore}p")
         secondpart=$(echo "$entrypart" | sed -n "${entryafter}p")
         thirdpart=$(echo "$entrypart" | sed -n "${entryid}p")

         restdiff=$(echo "$entrypart" | sed -n "${entryidplusone},${entryidplusempties}p")

         restpart=$(echo "$entrypart" | sed -n "${restpart},\$p")

         echo "$restoffile"
         echo ""
         echo "$subpinfopart"

         if (($entryid != 1)); then
            echo "$firstpart"
         fi

         if [ "x$secondpart" != "x" ]; then
            echo "$secondpart"
         fi

         if (($entryidplusone <= $entryidplusempties)); then
            echo "$restdiff"
         fi

         echo "$thirdpart"

         if [ "x$restpart" != "x" ]; then
            echo "$restpart"
         fi
      fi
   fi

   if [ "$ACTION" == "begin" ]; then
      if (($entryid == 1)); then
         if [[ -n $DISPLAY ]]; then
            $NSCDE_ROOT/bin/nscde_fvwmclnt "f_Notifier \"Subpanel Properties\" \"Dismiss\" \"NsCDE/Info.xpm\" \
                        \"$[gt.Item] '$TITLE' $[gt.is already on the top of the Subpanel] $subpid.\""
         fi
         # Important to exit here, else Subpanels.actions will be clobbered
         exit 0
      else
         entrybefore=$(($entryid - 1))
         entryafter=$(($entryid + 1))

         firstpart=$(echo "$entrypart" | sed -n "${entryid}p")
         secondpart=$(echo "$entrypart" | sed -n "1,${entrybefore}p")
         restpart=$(echo "$entrypart" | sed -n "${entryafter},\$p")

         echo "$restoffile"
         echo ""
         echo "$subpinfopart"
         echo "$firstpart"
         echo "$secondpart"
         echo "$restpart"
      fi
   fi

   if [ "$ACTION" == "end" ]; then
      if (($entryid == $endaddr)); then
         if [[ -n $DISPLAY ]]; then
            $NSCDE_ROOT/bin/nscde_fvwmclnt "f_Notifier \"Subpanel Properties\" \"Dismiss\" \"NsCDE/Info.xpm\" \
                        \"$[gt.Item] '$TITLE' $[gt.is already on the bottom of the Subpanel] $subpid.\""
         fi
         # Important to exit here, else Subpanels.actions will be clobbered
         exit 0
      else
         entrybefore=$(($entryid - 1))
         entryafter=$(($entryid + 1))

         firstpart=$(echo "$entrypart" | sed -n "${entryid}p")
         if (($entrybefore > 0)); then
            secondpart=$(echo "$entrypart" | sed -n "1,${entrybefore}p")
         else
            secondpart=""
         fi
         restpart=$(echo "$entrypart" | sed -n "${entryafter},\$p")

         echo "$restoffile"
         echo ""
         echo "$subpinfopart"
         if (($entrybefore > 0)); then
            echo "$secondpart"
         fi
         echo "$restpart"
         echo "$firstpart"
      fi
   fi

   if [ "$ACTION" == "delete" ]; then
      entrybefore=$(($entryid - 1))
      entryafter=$(($entryid + 1))

      firstpart=$(echo "$entrypart" | sed -n "1,${entrybefore}p")
      restpart=$(echo "$entrypart" | sed -n "${entryafter},\$p")

      echo "$restoffile"
      echo ""
      echo "$subpinfopart"

      if (($entryid > 1)); then
         echo "$firstpart"
      fi

      if (($entryid != $endaddr)); then
         echo "$restpart"
      fi
   fi

   if [ "$ACTION" == "rename" ]; then
      entrybefore=$(($entryid - 1))
      entryafter=$(($entryid + 1))

      newitemname=$(echo ${newitemname} | tr '\057\134\042\054' '.')
      newentrypart=$(echo "$entrypart" | $NSCDE_TOOLSDIR/ised -c "${entryid}s/\(${subpid}\),\(ENTRY\),\(.*\),\(.*\),\(.*\),\(.*\)/\1,\2,${newitemname},\4,\5,\6/g" -o -f -)

      echo "$restoffile"
      echo "$restoffile" | head -1 | egrep -q '^$'
      if (($? == 0)); then
         echo ""
      fi
      echo "$subpinfopart"
      echo "$newentrypart"
   fi

   if [[ -z $NSCDE_DEBUG ]]; then
      egrep -q "^S${subpid},NAME,.*" "$tmpfile"
      if (($? == 0)); then
         cp -f "$FVWM_USERDIR/Subpanels.actions" "$FVWM_USERDIR/tmp/Subpanels.actions.old"
         cat "$tmpfile" > "$FVWM_USERDIR/Subpanels.actions"
         $NSCDE_TOOLSDIR/generate_subpanels -w > $FVWM_USERDIR/Subpanels.fvwm2.fvwmgen
         $NSCDE_TOOLSDIR/generate_subpanels -f > $FVWM_USERDIR/Subpanels.fvwm3.fvwmgen
         $NSCDE_ROOT/bin/nscde_fvwmclnt "f_ReadCfg Subpanels"
         $NSCDE_ROOT/bin/nscde_fvwmclnt "KillModule FvwmButtons NsCDE-Subpanel${subpid}"
         rm "$tmpfile"
      else
         if [[ -n $DISPLAY ]]; then
            $NSCDE_ROOT/bin/nscde_fvwmclnt "f_Notifier \"Subpanel Properties\" \"Dismiss\" \"NsCDE/Error.xpm\" \
                        \"$[gt.Action for item] '$TITLE' $[gt.not performed on Subpanel] $subpid. $[gt.Errors found in] $tmpfile.\""
         else
            echo "Action for item $TITLE not performed on Subpanel $subpid. Errors in $tmpfile found." >&2
         fi
         exit 2
      fi
   fi

   if [ -f "$tmpfile" ]; then
      echo "Removing stale tmp file $tmpfile" >&2
      rm -f "$tmpfile"
   fi
}

function add_to_main_panel
{
   if [ -e "$FVWM_USERDIR/FrontPanel.actions" ]; then
      egrep -q "^Btn${subpid},.*,.*,.*,.*" "$FVWM_USERDIR/FrontPanel.actions"
      if (($? == 0)); then
         egrep -q "^Btn${subpid},__APPLET__,M1,.*,.*" "$FVWM_USERDIR/FrontPanel.actions"
         if (($? == 0)); then
            RestartFP=1
         else
            RestartFP=0
         fi
      else
         egrep -q "^Btn${subpid},__APPLET__,M1,.*,.*" "$NSCDE_DATADIR/defaults/FrontPanel.actions"
         if (($? == 0)); then
            RestartFP=1
         else
            RestartFP=0
         fi
      fi
   else
      egrep -q "^Btn${subpid},__APPLET__,M1,.*,.*" "$NSCDE_DATADIR/defaults/FrontPanel.actions"
      if (($? == 0)); then
         RestartFP=1
      else
         RestartFP=0
      fi
   fi

   title=$(echo ${TITLE} | tr '\057\134\042\054' '.')

   fp_m1="Btn${subpid},$icon,M1,NOCHK,$cmd"
   fp_m2="Btn${subpid},,M2,FVWM,Nop"
   fp_m3="Btn${subpid},,M3,FVWM,f_FrontPanelPropsMenu ${subpid} $title"

   if [ -e "$FVWM_USERDIR/FrontPanel.actions" ]; then
      $NSCDE_TOOLSDIR/ised -c "/^Btn${subpid},\(.*\)\?,M\(1\|2\|3\)/d" -f $FVWM_USERDIR/FrontPanel.actions
   fi

   tail -1 "$FVWM_USERDIR/FrontPanel.actions" | egrep -q "^$"
   if (($? != 0)); then
      echo "" >> "$FVWM_USERDIR/FrontPanel.actions"
   fi
   echo "$fp_m1" | head -1 >> "$FVWM_USERDIR/FrontPanel.actions"
   echo "$fp_m2" | head -1 >> "$FVWM_USERDIR/FrontPanel.actions"
   echo "$fp_m3" | head -1 >> "$FVWM_USERDIR/FrontPanel.actions"
   $NSCDE_ROOT/bin/nscde_fvwmclnt "SendToModule FrontPanel ChangeButton Btn${subpid} Icon \"$icon\""

   # If changing from applet to icon, restart Front Panel for settings to be visible
   if (($RestartFP == 1)); then
      $NSCDE_ROOT/bin/nscde_fvwmclnt "f_RestartFrontPanel"
   fi
}

function copy_to_main_panel
{
   echo "$BUTTONTYPE" | cut -d"." -f 3 | egrep -q '(icon|applet)'
   if (($? != 0)); then
      BUTTONTYPE="default"
   fi

   if [ -r "$FVWM_USERDIR/FrontPanel.actions" ]; then
      egrep -q "^Btn${subpid},__APPLET__,M1,.*,.*" "$FVWM_USERDIR/FrontPanel.actions"
      if (($? == 0)); then
         RestartFP=1
      else
         RestartFP=0
      fi

      if [ "$BUTTONTYPE" == "default" ]; then
         if [ -r "$NSCDE_DATADIR/defaults/FrontPanel.actions" ]; then
            egrep -q "^Btn${subpid},__APPLET__,M1,.*,.*" "$NSCDE_DATADIR/defaults/FrontPanel.actions"
            if (($? == 0)); then
               RestartFP=1
            else
               RestartFP=0
            fi
         else
            RestartFP=0
         fi
      else
         if [ "$BUTTONTYPE" == "applet" ]; then
            RestartFP=1
         else
            RestartFP=0
         fi
      fi
   fi

   entry_to_copy=$(egrep -h "^S${subpid},ENTRY,*" "$FVWM_USERDIR/Subpanels.actions" | sed -n "${entryid}p")
   if [ "x$entry_to_copy" == "x" ]; then
      entry_to_copy=$(egrep -h "^S${subpid},ENTRY,*" "$NSCDE_DATADIR/defaults/Subpanels.actions" | sed -n "${entryid}p")
   fi
   if [ "x$entry_to_copy" == "x" ]; then
      echo "ERROR: subpanel_menuitem_props (copy_to_main_panel): internal error. No entry in system Subpanels.actons file."
      exit 10
   fi
   echo "$entry_to_copy" | tail -1 | while IFS="," read smark emark title type icon cmd
   do
      for content in "$title" "$icon" "$cmd"
      do
         if [ "x$content" == "x" ]; then
            echo "ERROR: subpanel_menuitem_props (copy_to_main_panel): empty title, icon or command definition."
            exit 10
         fi
      done

      # Main variables for find_appropriate_icon function
      try_as_fd_misc=0
      icon_orig="$icon"
      icon_ext="${icon##*.}"

      # Main funting routine for appropriate  icon
      find_appropriate_icon "$icon"

      # Try local 32x32-* icon without fvwm generated prefix in freedesktop paths.
      # In the 48x48 subdirectories.
      if (($try_as_fd_misc == 1)); then
         find_appropriate_icon "${icon_orig#*-}"
      fi

      # Try local 32x32-* icon without fvwm generated prefix in freedesktop paths.
      # But this time in 32x32 size subdirectories.
      if (($try_as_fd_misc == 2)); then
         find_appropriate_icon "${icon_orig}"
      fi

      # Try find_appropriate_icon with different extensions.
      if (($ENOICON == 1)); then
         echo "Trying heuristics with icon extension formats while searching for an icon ..."
         export ENOICON=0
         case "$icon_ext" in
         'png')
            icon_new_ext=$(echo "$icon" | sed 's/\.png$/.xpm/g')
            find_appropriate_icon "$icon_new_ext"
            if (($ENOICON == 1)); then
               icon_new_ext=$(echo "$icon" | sed 's/\.png$/.svg/g')
               find_appropriate_icon "$icon_new_ext"
            fi
         ;;
         'xpm'|'pm')
            icon_new_ext=$(echo "$icon" | sed 's/\.xpm$/.png/g')
            find_appropriate_icon "$icon_new_ext"
            if (($ENOICON == 1)); then
               icon_new_ext=$(echo "$icon" | sed 's/\.xpm$/.svg/g')
               find_appropriate_icon "$icon_new_ext"
            fi
         ;;
         'svg')
            icon_new_ext=$(echo "$icon" | sed 's/\.svg$/.png/g')
            find_appropriate_icon "$icon_new_ext"
            if (($ENOICON == 1)); then
               icon_new_ext=$(echo "$icon" | sed 's/\.svg$/.xpm/g')
               find_appropriate_icon "$icon_new_ext"
            fi
         ;;
         esac
      fi

      case "x$type" in
      'x')
         fp_action_type="${cmd%% *}"
      ;;
      'xFVWM-F'|'xFVWM-M')
         fp_action_type="FVWM"
      ;;
      'xNOCHK')
         fp_action_type="true"
      ;;
      xCHECK:*)
         fp_action_type="${type##*:}"
      ;;
      esac

      # Strip absolute path from FVWM_USERDIR, NSCDE_ROOT, HOME, /usr/share/icons, /usr/local/share/icons ...
      sFVWM_USERDIR=$(echo "$FVWM_USERDIR" | $NSCDE_TOOLSDIR/ised -c 's/\(\[\|\]\|\^\|\$\|{\|}\|(\|)\||\|\.\|\/\|\+\|\*\)/\\\1/g' -o -f -)
      sHOME=$(echo "$HOME" | $NSCDE_TOOLSDIR/ised -c 's/\(\[\|\]\|\^\|\$\|{\|}\|(\|)\||\|\.\|\/\|\+\|\*\)/\\\1/g' -o -f -)
      sNSCDE_ROOT=$(echo "$NSCDE_ROOT" | $NSCDE_TOOLSDIR/ised -c 's/\(\[\|\]\|\^\|\$\|{\|}\|(\|)\||\|\.\|\/\|\+\|\*\)/\\\1/g' -o -f -)
      case $icon in
         $FVWM_USERDIR/icons/*)
            icon=$(echo "$icon" | $NSCDE_TOOLSDIR/ised -c "s/^$sFVWM_USERDIR\/icons\///g" -o -f -)
         ;;
         $NSCDE_ROOT/share/icons/*)
            icon=$(echo "$icon" | $NSCDE_TOOLSDIR/ised -c "s/^$sNSCDE_ROOT\/share\/icons\///g" -o -f -)
         ;;
         $HOME/.icons/*)
            icon=$(echo "$icon" | $NSCDE_TOOLSDIR/ised -c "s/^$sHOME\/\.\/icons\///g" -o -f -)
         ;;
         $HOME/.local/share/icons/*)
            icon=$(echo "$icon" | $NSCDE_TOOLSDIR/ised -c "s/^$sHOME\/\.local\/share\/icons\///g" -o -f -)
         ;;
         /NONEXISTENT/*)
            icon=$(echo "$icon" | $NSCDE_TOOLSDIR/ised -c "s/\/NONEXISTENT\///g" -o -f -)
         ;;
         /usr/share/icons/*)
            icon=$(echo "$icon" | $NSCDE_TOOLSDIR/ised -c "s/^\/usr\/share\/icons\///g" -o -f -)
         ;;
         /usr/local/share/icons/*)
            icon=$(echo "$icon" | $NSCDE_TOOLSDIR/ised -c "s/^\/usr\/local\/share\/icons\///g" -o -f -)
         ;;
         $NSCDE_DATADIR/fallback/icons/*)
            icon=$(echo "$icon" | $NSCDE_TOOLSDIR/ised -c "s/^$sNSCDE_DATADIR\/fallback\/icons\///g" -o -f -)
         ;;
      esac

      fp_m1="Btn${subpid},$icon,M1,$fp_action_type,$cmd"
      fp_m3="Btn${subpid},,M3,FVWM,f_FrontPanelPropsMenu ${subpid} $title"
      if [ -r "$FVWM_USERDIR/FrontPanel.actions" ]; then
         $NSCDE_TOOLSDIR/ised -c "/^Btn${subpid},\(.*\)\?,M\(1\|3\)/d" -f $FVWM_USERDIR/FrontPanel.actions
      fi

      echo "$fp_m1" | head -1 >> "$FVWM_USERDIR/FrontPanel.actions"
      echo "$fp_m3" | head -1 >> "$FVWM_USERDIR/FrontPanel.actions"
      $NSCDE_ROOT/bin/nscde_fvwmclnt "SendToModule FrontPanel ChangeButton Btn${subpid} Icon \"$icon\""

      # If changing from applet to icon, restart Front Panel for settings to be visible
      if (($RestartFP == 1)); then
         $NSCDE_ROOT/bin/nscde_fvwmclnt "f_RestartFrontPanel"
      fi
   done
}

function find_appropriate_icon
{
   # Defaults
   ENOICON=0

   case "$1" in
   *.m.pm|*.s.pm|*.t.pm)
      icon_search_paths="${FVWM_USERDIR}/icons/NsCDE:${FVWM_USERDIR}/icons/CDE:${FVWM_USERDIR}/icons:${NSCDE_DATADIR}/icons/NsCDE"
      pathtype="CDE"
   ;;
   *.m.xpm|*.s.xpm|*.t.xpm)
      icon_search_paths="${FVWM_USERDIR}/icons/NsCDE:${FVWM_USERDIR}/icons:${NSCDE_DATADIR}/icons/NsCDE"
      pathtype="NsCDE"
   ;;
   32x32-*.xpm|*/32x32-*.xpm|32x32-*.png|*/32x32-*.png|24x24-*.xpm|*/24x24-*.xpm|24x24-*.png|*/24x24-*.png)
      icon_search_paths="${FVWM_USERDIR}/icons"
      pathtype="FVWM"
   ;;
   *.png|*.svg|*.xpm)
      icon_search_paths="${FVWM_USERDIR}/icons:${HOME}/.icons:${HOME}/.local/share/icons:/usr/local/share/icons:/usr/share/icons:/usr/share/pixmaps:/usr/local/share/pixmaps:${NSCDE_DATADIR}/fallback/icons"
      pathtype="FD-MISC"
   ;;
   *)
      # If we are here, nothing has been found. Indicate this.
      export ENOICON=1
   ;;
   esac

   path_iconame=${1}
   iconame=${path_iconame##*/}
   found=1
   IFS=":"; for icon_search_path in $icon_search_paths
   do
      if [ "$pathtype" == "CDE" ]; then
         try_icon=$(echo "${iconame}" | $NSCDE_TOOLSDIR/ised -c 's/\.\(m\|t\|s\)\.pm$/\.l\.pm/g' -o -f -)
         try_medium_icon=$(echo "${iconame}" | $NSCDE_TOOLSDIR/ised -c 's/\.\(t\|s\)\.pm$/\.m\.pm/g' -o -f -)
      elif [ "$pathtype" == "NsCDE" ]; then
         try_icon=$(echo "${iconame}" | $NSCDE_TOOLSDIR/ised -c 's/\.\(m\|t\|s\)\.xpm$/\.l\.xpm/g' -o -f -)
         try_medium_icon=$(echo "${iconame}" | $NSCDE_TOOLSDIR/ised -c 's/\.\(t\|s\)\.xpm$/\.m\.xpm/g' -o -f -)
      elif [ "$pathtype" == "FVWM" ]; then
         try_icon=$(echo "${iconame}" | $NSCDE_TOOLSDIR/ised -c 's/^\(32x32\|24x24\)-/48x48-/g' -o -f -)
         # Try to generate 48x48 icon
         if [ ! -r "$FVWM_USERDIR/icons/${try_icon}" ]; then
            fvwm-menu-desktop --mini-icon-dir "$FVWM_USERDIR/icons" --enable-mini-icons --theme NsCDE --size 48 > /dev/null 2>&1
         fi
         # Fallback
         if [ ! -r "$FVWM_USERDIR/icons/${try_icon}" ]; then
            if (($try_as_fd_misc > 1)); then
               try_medium_icon=$(echo "${iconame}" | $NSCDE_TOOLSDIR/ised -c 's/^24x24-/48x48-/g' -o -f -)
            else
               try_as_fd_misc=$(($try_as_fd_misc + 1))
            fi
         fi
      elif [ "$pathtype" == "FD-MISC" ]; then
         try_icon="${iconame}"
         echo "$path_iconame" | egrep -q '.*\/(32x32|24x24|22x22)\/.*\/.*'
         if (($? == 0)); then
            full_try_icon="${path_iconame}"
            big_full_try_icon=$(echo "${path_iconame}" | $NSCDE_TOOLSDIR/ised -c 's/\(.*\)\/\(32x32\|24x24\|22x22\)\/\(.*\)/48x48/g' -o -f -)
         fi
      fi

      if [ -d "${icon_search_path}/NsCDE/48x48" ]; then
         find ${icon_search_path}/NsCDE/48x48 -type d -print | while read sub_dir
         do
            if [ -r ${sub_dir}/$try_icon ]; then
               icon=$(echo "${sub_dir}/$try_icon" | head -1)
               return
            else
               found=0
            fi
         done
      fi

      for othertheme in hicolor locolor mate gnome oxygen breeze Adwaita
      do
         if [ -d "${icon_search_path}/${othertheme}/48x48" ]; then
            find ${icon_search_path}/${othertheme}/48x48 -type d -print | while read sub_dir
            do
               if [ -r ${sub_dir}/$try_icon ]; then
                  icon=$(echo "${sub_dir}/$try_icon" | head -1)
                  return
               else
                  found=0
               fi
            done
         fi
      done

      if [ "x$big_full_try_icon" != "x" ]; then
         if [ -r "${icon_search_path}/$big_full_try_icon" ]; then
            icon=$(echo "${icon_search_path}/$big_full_try_icon" | head -1)
            return
         else
            found=0
         fi
      fi

      if [ "x$full_try_icon" != "x" ]; then
         if [ -r "${icon_search_path}/$full_try_icon" ]; then
            icon=$(echo "${icon_search_path}/$full_try_icon" | head -1)
            return
         else
            found=0
         fi
      else
         if [ -r "${icon_search_path}/$try_icon" ]; then
            icon=$(echo "${icon_search_path}/$try_icon" | head -1)
            return
         else
            if [ -r "${icon_search_path}/$try_medium_icon" -a -f "${icon_search_path}/$try_medium_icon" ]; then
               icon=$(echo "${icon_search_path}/$try_medium_icon" | head -1)
               return
            else
               found=0
            fi
         fi
      fi

      if [ -d "${icon_search_path}/NsCDE/32x32" ]; then
         find ${icon_search_path}/NsCDE/32x32 -type d -print | while read sub_dir
         do
            if [ -r ${sub_dir}/$try_icon ]; then
               icon=$(echo "${sub_dir}/$try_icon" | head -1)
               return
            else
               found=0
            fi
         done
      fi

      for othertheme in hicolor locolor mate gnome oxygen breeze Adwaita
      do
         if [ -d "${icon_search_path}/${othertheme}/32x32" ]; then
            find ${icon_search_path}/${othertheme}/32x32 -type d -print | while read sub_dir
            do
               if [ -r ${sub_dir}/$try_icon ]; then
                  icon=$(echo "${sub_dir}/$try_icon" | head -1)
                  return
               else
                  found=0
               fi
            done
         fi
      done
   done

   if (($found == 0)) || [ ! -r "$icon" -a ! -f "$icon" ]; then
      icon="${NSCDE_DATADIR}/icons/NsCDE/Noicon.xpm"
      return
      # If we are here, nothing has been found. Indicate this.
      export ENOICON=1
   fi
}

function delete_confirm
{
   if [[ -n $DISPLAY ]]; then
      $NSCDE_ROOT/bin/nscde_fvwmclnt "f_RunQuickScriptDialog ActionForm \
                   \"$[gt.Delete item] \\\"$TITLE\\\" $[gt.from the subpanel] ${subpid}?\" \
                   $[gt.Yes] $[gt.No] \"$[gt.Confirm Entry Deletion]\" \
                   \"f_DeleteFromSubpanel $subpid $entryid\" Nop"
   fi
}

function exec_entry
{
   $NSCDE_ROOT/bin/nscde_fvwmclnt "SendToModule NsCDE-Subpanel${subpid} PressButton Btn-${subpid}-${entryid}"
}

while getopts p:s:n:a:t:i:x:edc: Option
do
   case $Option in
      p)
         subpid="$OPTARG"
      ;;
      s)
         entryid="$OPTARG"
      ;;
      n)
         newitemname="$OPTARG"
      ;;
      a)
         ACTION="$OPTARG"
         subpanels_file_tstamp
         actions_file_sanity
         manage_entry
      ;;
      t)
         TITLE="$OPTARG"
      ;;
      x)
         cmd="$OPTARG"
      ;;
      i)
         icon="$OPTARG"
         add_to_main_panel
      ;;
      e)
         exec_entry
      ;;
      d)
         delete_confirm
      ;;
      c)
         BUTTONTYPE="$OPTARG"
         copy_to_main_panel
      ;;
   esac
done

