#!/bin/bash
#PupShutdown 3.0
#Copyright 2017 GPL3
#Roger Grider (radky)
#20130204 internationalized (vicmz)

export TEXTDOMAIN=pupshutdown
export OUTPUT_CHARSET=UTF-8

export VERSION="3.0"
export WORKDIR="/usr/local/PupShutdown"

#define gtkdialog
if [ "`which gtkdialog4`" ]; then
  GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
  GTKDIALOG=gtkdialog3
else
  GTKDIALOG=gtkdialog
fi
export GTKDIALOG

#find current font color
. $WORKDIR/func -current_fontcolor

#find current font name
. $WORKDIR/func -current_fontname

#find current graphic
. $WORKDIR/func -current_graphic

#if no pupshutdown configuration file, then build config file
[ ! -s $HOME/.PupShutdown/pupshutdown.rc ] && echo -e -n 'NEWLABEL="My Pup 1.0"\nNEWGRAPHIC="Shutdown.png"\nNEWFONTNAME="DejaVuSans"\nNEWFONTSIZE="12"\nNEWFONTWEIGHT="bold"\nNEWFONTSTYLE="normal"\n' > $HOME/.PupShutdown/pupshutdown.rc
. $HOME/.PupShutdown/pupshutdown.rc

#define $NEWLABEL_ITEMS for label-name combobox
NEWLABEL_ITEMS="<item>$NEWLABEL</item>"
for I in Hide_Label Pup Apup Dpup Spup Tpup Upup; do NEWLABEL_ITEMS=`echo "$NEWLABEL_ITEMS<item>$I</item>"`; done

#define $NEWGRAPHIC_ITEMS for logo-graphic combobox
NEWGRAPHIC_ITEMS=`cat /tmp/PSD-logolist.txt`

#define $NEWFONTNAME_ITEMS for font-name combobox
NEWFONTNAME_ITEMS=`cat /tmp/PSD-fontlist.txt | awk '!a[$0]++'`

#define $NEWFONTSIZE_ITEMS for font-size combobox
NEWFONTSIZE_ITEMS="<item>$NEWFONTSIZE</item>"
for I in 7 8 9 10 11 12 14 16 18 20 22; do NEWFONTSIZE_ITEMS=`echo "$NEWFONTSIZE_ITEMS<item>$I</item>"`; done

#define $NEWFONTWEIGHT_ITEMS for font-weight combobox
NEWFONTWEIGHT_ITEMS="<item>normal</item> <item>bold</item>"

#define $NEWFONTSTYLE_ITEMS for font-style combobox
NEWFONTSTYLE_ITEMS="<item>normal</item> <item>italic</item>"

#define NEWFONTCOLOR for font-color entry box
if [ -s /tmp/PSD-color.tmp ]; then
	NEWFONTCOLOR=`cat /tmp/PSD-color.tmp`
else
	NEWFONTCOLOR=`cat $HOME/.PupShutdown/pupshutdown-restore.rc | grep -a NEWFONTCOLOR= | cut -d "=" -f 2 | sed 's/\"//g'`
fi

#define graphic editor for logo creation
if [ "`which mtpaint`" ]; then
	LOGOEDITOR="mtpaint"
else
	LOGOEDITOR="defaultpaint"
fi

#define font-color selector
	[ ! -s /tmp/PSD-color.tmp ] && cat $HOME/.PupShutdown/pupshutdown-restore.rc | grep -a NEWFONTCOLOR= | cut -d "=" -f 2 | sed 's/\"//g' > /tmp/PSD-color.tmp
	COLORSEL="
	<entry tooltip-text=\" $(gettext 'Click the color selector button') \">
	<input>cat /tmp/PSD-color.tmp</input>
	<variable>NEWFONTCOLOR</variable>
	</entry>

	<colorbutton title=\"$(gettext 'PupShutdown Color Selector')\" tooltip-text=\" $(gettext 'Color selector') \">
	<input file>/tmp/PSD-color.tmp</input>
	<variable>NEWCOLORBUTTON</variable>
	<action>echo "'"$NEWCOLORBUTTON"'" > /tmp/PSD-color.tmp</action>
	<action>refresh:NEWFONTCOLOR</action>
	</colorbutton>"

#Help dlg...
func_psd_help (){
export PSD_HELP="
<window title=\"PSD - $(gettext 'Help')\" icon-name=\"shutdown64\" window-position=\"2\">
 <notebook labels=\"$(gettext 'About')|$(gettext 'License')\">
  <frame>
   <pixmap><input file>$WORKDIR/icons/shutdown64.png</input><height>48</height><width>48</width></pixmap>
   <text use-markup=\"true\"><label>\"<b><span size='"'x-large'"'>Pup</span><span size='"'x-large'"' color='"$NEWFONTCOLOR"'>Shutdown</span> $VERSION</b>\"</label></text>
   <text use-markup=\"true\"><label>\"<b>Shutdown Manager</b>\"</label></text>
   <text><label>\"Copyright (C) 2010-2017 Roger D. Grider\"</label></text>
   <text height-request=\"10\"><label>\"\"</label></text>
   <hbox homogeneous=\"true\">
    <button width-request=\"170\">
     <label>\"$(gettext 'Local Help')\"</label>
     <action>defaultbrowser $WORKDIR/Help.html &</action>
    </button>
    <button width-request=\"170\">
     <label>\"$(gettext 'Online Forum')\"</label>
     <action>defaultbrowser http://murga-linux.com/puppy/viewtopic.php?p=422134#422134 &</action>
    </button>
   </hbox>
  </frame>
  <frame>
   <text use-markup=\"true\"><label>\"<b>PupShutdown</b> $(gettext 'is free software which you can modify and/or redistribute under terms of the <b>GNU General Public License</b> as published by the Free Software Foundation; either GPLv3 or later version.')

$(gettext 'This program is distributed in the hope that it will be useful, but') <b><span color='"'red'"'>$(gettext 'WITHOUT ANY WARRANTY')</span></b>. $(gettext 'See the GNU General Public License homepage below for additional information.')\"</label></text>
   <hbox homogeneous=\"true\">
    <button>
     <label>\"   http://www.gnu.org/licenses/   \"</label>
     <action>defaulthtmlviewer http:\/\/www.gnu.org\/licenses\/ &</action>
    </button>
   </hbox>
  </frame>
 </notebook>
</window>"
$GTKDIALOG -p PSD_HELP
unset PSD_HELP
}
export -f func_psd_help

#Preferences dlg...
export PSD_PREF="
<window title=\"$(gettext 'Preferences')\" icon-name=\"gtk-preferences\" resizable=\"false\" window-position=\"1\">
  <vbox>
    <text use-markup=\"true\"><label>\"<b><span size='"large"'>Pup</span><span size='"large"' color='"$NEWFONTCOLOR"'>Shutdown</span> "$VERSION"</b>\"</label></text>
  <frame $(gettext 'Label')>
  <vbox>
   <hbox>
    <text><label>$(gettext 'Name:')</label></text>
    <comboboxentry wrap-width=\"1\">
     <variable>NEWLABEL</variable>
     <default>$NEWLABEL</default>
     $NEWLABEL_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Graphic:')</label></text>
    <comboboxentry wrap-width=\"1\">
     <variable>NEWGRAPHIC</variable>
     <default>$NEWGRAPHIC</default>
     $NEWGRAPHIC_ITEMS
    </comboboxentry>
   </hbox>
  </vbox>
  </frame>

 <frame $(gettext 'Font:')>
   <vbox>
   <hbox>
   <vbox>
    <text><label>$(gettext 'Name')</label></text>
    <comboboxtext wrap-width=\"1\" width-request=\"120\">
     <variable>NEWFONTNAME</variable>
     <default>$NEWFONTNAME</default>
     $NEWFONTNAME_ITEMS
    </comboboxtext>
   </vbox>

   <vbox>
    <text><label>$(gettext 'Size')</label></text>
    <comboboxtext wrap-width=\"1\" width-request=\"120\">
     <variable>NEWFONTSIZE</variable>
     <default>$NEWFONTSIZE</default>
     $NEWFONTSIZE_ITEMS
    </comboboxtext>
   </vbox>
   </hbox>

   <hbox>
   <vbox>
    <text><label>$(gettext 'Weight')</label></text>
    <comboboxtext wrap-width=\"1\" width-request=\"120\">
     <variable>NEWFONTWEIGHT</variable>
     <default>$NEWFONTWEIGHT</default>
     $NEWFONTWEIGHT_ITEMS
    </comboboxtext>
   </vbox>

   <vbox>
    <text><label>$(gettext 'Style')</label></text>
    <comboboxtext wrap-width=\"1\" width-request=\"120\">
     <variable>NEWFONTSTYLE</variable>
     <default>$NEWFONTSTYLE</default>
     $NEWFONTSTYLE_ITEMS
    </comboboxtext>
   </vbox>
   </hbox>
   </vbox>

    <text><label>\"$(gettext 'Color')\"</label></text>
   <hbox>
    $COLORSEL
   </hbox>
 </frame>

   <hbox space-expand=\"false\" space-fill=\"true\" homogeneous=\"true\">
     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'PupShutdown Help') \">
      <input file stock=\"gtk-help\"></input>
      <action>func_psd_help &</action>
     </button>

    <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'View logo graphics') \">
     <input file>$WORKDIR/icons/viewgraphic20.png</input>
      <action>defaultimageviewer $WORKDIR/logos/Shutdown.png &</action>
    </button>

    <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Create a new logo') \">
     <input file>$WORKDIR/icons/editgraphic20.png</input>
      <action>$LOGOEDITOR &</action>
      <action>sleep 1; /tmp/PSD-createlogo &</action>
      <action type=\"exit\">quit_now</action>
    </button>

    <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Configuration options') \">
     <input file>$WORKDIR/icons/preferences20.png</input>
      <action>cp /usr/local/PupShutdown/PSD-apps.rc /tmp/PSD-apps.rc.tmp &</action>
      <action>/usr/local/PupShutdown/PSD-apps &</action>
    </button>

    <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Save changes and exit') \">
     <input file stock=\"gtk-save\"></input>
      <action>$WORKDIR/func -preferences_new &</action>
      <action type=\"exit\">quit_now</action>
    </button>

     <button width-request=\"32\" height-request=\"32\" tooltip-text=\" $(gettext 'Exit without saving') \">
      <input file>$WORKDIR/icons/exit20.png</input>
      <action>$WORKDIR/func -restore &</action>
      <action type=\"exit\">quit_now</action>
     </button>
   </hbox>
  </vbox>
</window>"

$GTKDIALOG -p PSD_PREF > $HOME/.PupShutdown/pupshutdown.rc

unset PSD_PREF

exit 0
