#!/bin/ksh

if [ -z $HOME ]; then
   echo "Error: HOME is not set cannot continue."
   exit 3
fi

if [ -z $NSCDE_ROOT ]; then
   echo "Error: NSCDE_ROOT is not set cannot continue."
   exit 4
fi

if [ ! -e "$NSCDE_ROOT" ]; then
   echo "Error: NSCDE_ROOT does not exist. Cannot continue."
   exit 5
fi

if [ -z $FVWM_USERDIR ]; then
   echo "Error: FVWM_USERDIR is not set cannot continue."
   exit 6
fi

function add_subpanel
{
   if [ "x$SubpanelNo" == "x" ]; then
      echo "ERROR: Missing subpanel number."
      exit 9
   fi

   if (($SubpLocalDefCheck == 3)); then
      $NSCDE_ROOT/bin/ised -c "s/^S${SubpanelNo},ENABLED,0/S${SubpanelNo},ENABLED,1/g" -f "$FVWM_USERDIR/Subpanels.actions"
   fi

   if (($SubpLocalDefCheck == 0)); then
      if [ -r "$NSCDE_ROOT/config/Subpanels.actions" ]; then
         echo "" >> "$FVWM_USERDIR/Subpanels.actions"
         egrep "^S${SubpanelNo},(NAME|WIDTH|ENABLED|ENTRY),.*" "$NSCDE_ROOT/config/Subpanels.actions" >> "$FVWM_USERDIR/Subpanels.actions"
         $NSCDE_ROOT/bin/ised -c "s/^S${SubpanelNo},ENABLED,0/S${SubpanelNo},ENABLED,1/g" -f "$FVWM_USERDIR/Subpanels.actions"
      else
         echo "ERROR: Cannot open $NSCDE_ROOT/config/Subpanels.actions for reading."
         exit 8
      fi
   fi
}

function delete_subpanel
{
   if [ "x$SubpanelNo" == "x" ]; then
      echo "ERROR: Missing subpanel number."
      exit 9
   fi

   if (($SubpLocalDefCheck == 3)); then
      $NSCDE_ROOT/bin/ised -c "s/^S${SubpanelNo},ENABLED,1/S${SubpanelNo},ENABLED,0/g" -f "$FVWM_USERDIR/Subpanels.actions"
   fi

   if (($SubpLocalDefCheck == 0)); then
      if [ -r "$NSCDE_ROOT/config/Subpanels.actions" ]; then
         echo "" >> "$FVWM_USERDIR/Subpanels.actions"
         egrep "^S${SubpanelNo},(NAME|WIDTH|ENABLED|ENTRY),.*" "$NSCDE_ROOT/config/Subpanels.actions" >> "$FVWM_USERDIR/Subpanels.actions"
         $NSCDE_ROOT/bin/ised -c "s/^S${SubpanelNo},ENABLED,1/S${SubpanelNo},ENABLED,0/g" -f "$FVWM_USERDIR/Subpanels.actions"
      else
         echo "ERROR: Cannot open $NSCDE_ROOT/config/Subpanels.actions for reading."
         exit 8
      fi
   fi

}

function reset_this_button
{
   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${PanelNo},__APPLET__,M1,.*,.*" "$FVWM_USERDIR/FrontPanel.actions"
      if (($? == 0)); then
         RestartFP=1
      else
         RestartFP=0
      fi

      if [ "$BUTTONTYPE" == "icon" ]; then
         if [ -r "$NSCDE_ROOT/config/FrontPanel.actions" ]; then
            egrep -q "^Btn${PanelNo},__APPLET__,M1,.*,.*" "$NSCDE_ROOT/config/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

      egrep -q "^Btn${PanelNo},.*" "$FVWM_USERDIR/FrontPanel.actions"
      FPA_RETVAL=$?
      if ((FPA_RETVAL == 0)); then
         $NSCDE_ROOT/bin/ised -c "/^Btn${PanelNo},.*/d" -f "$FVWM_USERDIR/FrontPanel.actions"
         FvwmCommand "f_Notifier \"Front Panel Info\" \"Dismiss\" \"NsCDE/Info.xpm\" \
                     \"Button nr. $PanelNo has been restored to it's default configured function.\""
      else
         FvwmCommand "f_Notifier \"Front Panel Info\" \"Dismiss\" \"NsCDE/Info.xpm\" \
                     \"Button $PanelNo is already set at it's default configured function.\""
      fi

      if (($RestartFP == 0)); then
         FvwmCommand "PipeRead \"\$[NSCDE_ROOT]/libexec/fpseticon Btn${PanelNo}\""
      else
         FvwmCommand "InfoStoreRemove FP.Btn${PanelNo}.type"
         FvwmCommand "f_RestartFrontPanel"
      fi
   fi
}

while getopts p:adr: Option
do
   case $Option in
      p)
         SubpanelNo=$OPTARG
         PanelNo=$SubpanelNo
         SubpLocalDefCheck=$(egrep "^S${SubpanelNo},(NAME|WIDTH|ENABLED),.*" "$FVWM_USERDIR/Subpanels.actions" 2>/dev/null | wc -l)

         if (($SubpLocalDefCheck != 0)) && (($SubpLocalDefCheck != 3)); then
            echo "ERROR: invalid configuration for subpanel ${SubpanelNo} in $FVWM_USERDIR/Subpanels.actions."
            exit 7
         fi
      ;;
      a)
         add_subpanel
      ;;
      d)
         delete_subpanel
      ;;
      r)
         BUTTONTYPE="$OPTARG"
         reset_this_button
      ;;
   esac
done

