#!/bin/ash
#Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v3.02 Nov 2007 BK: updated. handle PET pkgs only.
#v3.99 apr2008 BK: run /usr/local/petget/pkg_chooser.sh instead of petget.
#v404 add choice of Universal Installer.
#w015 march 2009 BK: update for Woof.
#110402 added button to choose and install SFS file.
#110813 fixed package-management url.
#110814 01micko: convert script from gtkdialog2 to gtkdialog3+.
#110910 BK: get a syntax error with gtkdialog3 (patriot-e version), bump to gtkdialog4.
#120201 rodin.s: internationalized.
# 26apr2013 shinobar: frugalinstaller, horisontal layout
# 10dec2013 shinobar: dotpup1, gtkdialog

export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=dotpup
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}

#how do i called
MYOPT=$1
[ "$MYOPT" ] || MYOPT=$(basename $0)
MYOPT=$(echo $MYOPT | tr -dc '0-9')

. /etc/DISTRO_SPECS

# has frugalinstaller? quickpet? # 29nov11 shinobar: sfs_load
which frugalinstaller &>/dev/null && HASFRUGAL="yes" || HASFRUGAL=""
which quickpet &>/dev/null && HASQUICKPET="yes" || HASQUICKPET=""
which sfs_load &>/dev/null && HASSFSLOAD="yes" || HASSFSLOAD=""
which f2fs-usb-installer &>/dev/null && HASF2FS="yes" || HASF2FS=""

install_button() {
  echo '
  <hbox>
    <vbox homogeneous="true">
      <vbox>
        <text use-markup="true" xalign="1"><label>"<b>'${IBUTTON_TEXT1}'</b>"</label></text>
        <text xalign="1"><label>'${IBUTTON_TEXT2}'</label></text>
      </vbox>
    </vbox>
    <button>
      '`/usr/lib/gtkdialog/xml_button-icon puppy_install.svg big`'
      <action>'"${IBUTTON_ACTION}"'</action>
      <action>EXIT:13</action>
    </button>
  </hbox>
  '
}

if [ "$HASFRUGAL" != "" ]; then
	IBUTTON_TEXT1=$(gettext "Frugal Installer")
	IBUTTON_TEXT2=$(gettext "RECOMMENDED install style of Puppy")
	IBUTTON_ACTION='frugalinstaller &'
	FRUGALBUTTON=$(install_button)
fi

IBUTTON_TEXT1=$(gettext "Universal installer")
IBUTTON_TEXT2=$(gettext "Required for 'full' install")
IBUTTON_ACTION='puppyinstaller &'
FULLINSTALLBUTTON=$(install_button)

IBUTTON_TEXT1=$(gettext "BootFlash USB installer")
IBUTTON_TEXT2=$(gettext "Install to flash drive - destructive method")
IBUTTON_ACTION='stickpup &'
BOOTFLASHBUTTON=$(install_button)

if [ "$HASF2FS" != "" ]; then
	IBUTTON_TEXT1=$(gettext "F2fs USB Installer")
	IBUTTON_TEXT2=$(gettext "Install Puppy on USB flash devices using f2fs file system")
	IBUTTON_ACTION='f2fs-usb-installer &'
	F2FSBUTTON=$(install_button)
fi

SFSBUTTON=''
if [ "$HASSFSLOAD" != "" ]; then
  SFSBUTTON='
  <hbox>
    <text><label>'$(gettext 'Load and unload SFS packages using the SFS-load')'</label></text>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon package_sfs.svg big`"'
      <action>sfs_load &</action>
      <action>EXIT:13</action>
    </button>
  </hbox>'
fi

#make dialog

PUPFRAME=""
[ "$MYOPT" = "2" ] || PUPFRAME='
<frame '$(gettext 'Install Puppy to some other media')'>
  '"`/usr/lib/gtkdialog/xml_info fixed puppy_install.svg 60 "$(gettext 'Puppy can be installed to just about anything, including internal hard drive USB drive, and optical disc. If for example you are now running Puppy from live-CD and fancy booting Puppy off a USB Flash drive, this is the right place.')"`"'
  <vbox margin="5" space-expand="true" space-fill="true">
    <vbox space-expand="false" space-fill="false">
      '$FRUGALBUTTON'
      '$FULLINSTALLBUTTON'
      '$BOOTFLASHBUTTON'
      '$F2FSBUTTON'
    </vbox>
    <vbox space-expand="true" space-fill="true">
      <hbox space-expand="true" space-fill="true">
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </hbox>
      <vbox space-expand="false" space-fill="false">
        <hbox space-expand="true" space-fill="true">
          <button space-expand="false" space-fill="false">
            '"`/usr/lib/gtkdialog/xml_button-icon help`"'
            <label>'$(gettext 'Installation introduction webpage')'</label>
            <action>defaultbrowser file:///usr/share/doc/installation.htm & </action>
          </button>
          <text space-expand="true" space-fill="true"><label>""</label></text>
        </hbox>
      </vbox>
    </vbox>
  </vbox>
</frame>'
 
APPFRAME=""
[ "$MYOPT" = "1" ] || APPFRAME='
<frame '$(gettext 'Install applications in the current Puppy')'>
  '"`/usr/lib/gtkdialog/xml_info fixed package_add.svg 60 "$(eval_gettext "The Puppy Package Manager is a tool for installing and uninstalling packages (applications). There are online repositories of ${DISTRO_BINARY_COMPAT} and Puppy packages -- the latter are known as PET packages and are identified by '.pet' on the end of their filename. Note that apart from the official ${DISTRO_BINARY_COMPAT} and PET repositories, Puppy enthusiasts have created many PETs at other locations, and all you need to do is download one and click on it to install it.")" "" "$(gettext 'SFS files are application combo-packs, very easy to install and uninstall.')"`"'
  <vbox margin="5" space-expand="true" space-fill="true">
    <vbox space-expand="false" space-fill="false">
    <hbox>
      <text><label>'$(gettext 'Puppy Package Manager')'</label></text>
      <button tooltip-text="PET Packages">
        '"`/usr/lib/gtkdialog/xml_button-icon package_add.svg big`"'
        <action>/usr/local/petget/pkg_chooser.sh &</action>
        <action>EXIT:14</action>
      </button>
    </hbox>
    <hbox>
      <text><label>'$(gettext 'Choose an SFS file from the official repo')'</label></text>
      <button tooltip-text="SFS Modular Packages">
        '"`/usr/lib/gtkdialog/xml_button-icon package_sfs.svg big`"'
        <action>/usr/sbin/sfsget &</action>
        <action>EXIT:14</action>
      </button>
    </hbox>
    '$SFSBUTTON'
    </vbox>
    <vbox space-expand="true" space-fill="true">
      <hbox space-expand="true" space-fill="true">
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </hbox>
      <vbox space-expand="false" space-fill="false">
        <hbox space-expand="true" space-fill="true">
          <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon help`"'
          <label>'$(gettext 'Package Manager introduction webpage')'</label>
          <action>defaultbrowser http://puppylinux.com/development/package-management.htm & </action>
          </button>
          <text space-expand="true" space-fill="true"><label>""</label></text>
        </hbox>
      </vbox>
    </vbox>
  </vbox>
</frame>'


if [ "$PUPFRAME" ] && [ "$APPFRAME" ]; then
	export Puppy_Install='
	<window title="'$(gettext 'Install')'" icon-name="gtk-preferences">
	<vbox>
	  <notebook labels="'$(gettext 'Install Puppy')'|'$(gettext 'Install applications')'" space-expand="true" space-fill="true">
	    <vbox>
	      '$PUPFRAME'
	    </vbox>
	    <vbox>
	      '$APPFRAME'
	    </vbox>
	  </notebook>
	  <hbox space-expand="false" space-fill="false">
	    <button>
	      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
	      <label>'$(gettext 'Quit')'</label>
	    </button>
	  </hbox>
	</vbox>
	</window>'
else
	export Puppy_Install='
	<window title="'$(gettext 'Install')'" icon-name="gtk-preferences">
	<vbox>
	  '$PUPFRAME'
	  '$APPFRAME'
	  <hbox space-expand="false" space-fill="false">
	    <button>
	      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
	      <label>'$(gettext 'Quit')'</label>
	    </button>
	  </hbox>
	</vbox>
	</window>'
fi

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p Puppy_Install || echo "$Puppy_Install" >&2

###END###