#!/bin/bash
# Pmwget created by gposil with thanks to Lobster for Pwget
# April 2009 GPL v3 License
# http://gposil.netne.net
#2009 gposil: version 0.2
#121018 bk: bug fixes, version 0.2.1.
#130517 robwoj44: gettexted.
#2023 radky: update GUI for GTK2/GTK3

. gettext.sh
export TEXTDOMAIN=pmwget
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8

[ "`which gtk2dialog 2>/dev/null`" ] && GTKDIALOG=gtk2dialog || GTKDIALOG=gtkdialog
export GTKDIALOG

[ ! -f /usr/share/icons/hicolor/48x48/apps/install.svg ] && \
ln -sf /usr/share/pixmaps/puppy/install.svg /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

export HELP_DIALOG='
<window title="'$(gettext 'PMirrorget - Help')'" resizable="false" window-position="3" icon-name="gtk-help">
<vbox margin="10">
	<text>
		<label>'$(gettext 'PMirrorget allows you to download an entire web page and its text linked pages to a folder on your PC. Copy and paste the URL you wish to download. Use the folder selector to choose the destination. It is designed primarily for grabbing manuals and wiki pages without sifting through them, so you can view them later.')'</label>
		<variable>HELP_DLG_MSG</variable>
	</text>
	<text height-request="5"><label>" "</label></text>
	<button>
		<label>'$(gettext 'Close')'</label>
		<action type="closewindow">HELP_DLG_MSG</action>
	</button>
	<variable>HELP_DIALOG</variable>
</vbox>
</window>
'

export Pmwget='
<window title="'$(gettext 'PMirrorget - Site Grabber')'" default-width="658" resizable="true" window-position="1" icon-name="install">
<vbox margin="4">
 <vbox homogeneous="true" space-expand="true" space-fill="true">
 <vbox space-expand="false" space-fill="false">
 <hbox homogeneous="true">
       <text wrap="false"><label>'$(gettext 'Copy and Paste or type the URL of the required site.')'</label></text>
 </hbox>
 <hbox homogeneous="true">
       <text wrap="false"><label>'$(gettext 'Choose the destination folder and then Grab It Now!')'</label></text>
 </hbox>
 </vbox>
 </vbox>

 <hseparator space-expand="false" space-fill="false"></hseparator>
 <text height-request="5"><label>" "</label></text>

 <hbox>
       <text xalign="0" width-request="75" space-expand="false" space-fill="false"><label>'$(gettext 'URL:')'</label></text>
       <entry width-request="500" space-expand="true" space-fill="true" accept="directory" secondary-icon-stock="gtk-clear">
              <variable>SOURCE</variable>
              <input>cat /tmp/pm_source_dir 2>/dev/null</input>
              <action>echo $SOURCE > /tmp/pm_source_dir</action>
              <action signal="secondary-icon-release">clear:SOURCE</action>
       </entry>
       <button width-request="75" space-expand="false" space-fill="false">
              <input file stock="gtk-help"></input>
              <action type="launch">HELP_DIALOG</action>
       </button>
 </hbox>
 <hbox>
       <text xalign="0" width-request="75" space-expand="false" space-fill="false"><label>'$(gettext 'Folder:')'</label></text>
       <entry width-request="500" space-expand="true" space-fill="true" accept="directory" fs-title="'$(gettext 'Folder Selector')'" secondary-icon-stock="gtk-clear">
              <variable>DEST</variable>
              <input>cat /tmp/pm_mirror_dir 2>/dev/null</input>
              <action>echo $DEST > /tmp/pm_mirror_dir</action>
              <action signal="secondary-icon-release">clear:DEST</action>
       </entry>
       <button width-request="75" space-expand="false" space-fill="false">
              <input file stock="gtk-open"></input>
              <action type="fileselect">DEST</action>
              <action>refresh:DEST</action>
       </button>
 </hbox>

 <text height-request="5"><label>" "</label></text>
 <hseparator space-expand="false" space-fill="false"></hseparator>

 <vbox>
 <hbox space-expand="true" space-fill="true">
       <button width-request="250">
              <input file stock="gtk-cancel"></input>
              <label>'$(gettext 'Cancel')'</label>
              <action type="exit">quit_now</action>
       </button>
       <button width-request="250">
              <input file stock="gtk-apply"></input>
              <label>'$(gettext 'Grab It Now!')'</label>
              <action type="exit">OK</action>
       </button>
 </hbox>
 </vbox>

</vbox>
</window>'

I=$IFS; IFS=""
for STATEMENTS in  $($GTKDIALOG --program=Pmwget); do
   eval $STATEMENTS
done
IFS=$I
if [ $EXIT = OK ]; then
  [ -x /usr/local/bin/roxfiler ] && ROX=roxfiler || ROX=rox
  rxvt -name PMirrorget -bg "#F3F2DF" -e wget -m -c -k -r -np -P "$DEST" $SOURCE
  $ROX -d "$DEST"
fi

###END###
