#!/bin/bash
#
# Server Wizard - A wizard for settings and features in Porteus Kiosk Server
# Author: T.Jokiel <http://porteus-kiosk.org>

## ====================================================================
## variables
## ====================================================================

## We need to export these variables so they are available to gtkdialog
ICONS=/usr/share/pixmaps
export PCID=`grep ^ID: /etc/version | cut -d" " -f2`-`grep ^MBID: /etc/version | cut -d" " -f2`

## Create temporary working directory
rm -rf /tmp/kwiz.* 2>/dev/null
export TMPDIR=/tmp/kwiz.$$
mkdir -p $TMPDIR

## ====================================================================
## Main dialog
## ====================================================================

################# Setup required files
export IMAGEONE="/usr/share/icons/oxygen/22x22/actions/gtk-no.png"
export IMAGETWO="/usr/share/icons/oxygen/22x22/actions/gtk-ok.png"
export IMGPASS=$TMPDIR/img.png
cp -L $IMAGEONE $TMPDIR/img.png

## Set some text files that should exist before the main
## gtkdialog is rendered
echo 0 > $TMPDIR/.knetPage
lsblk -o NAME,TYPE,MODEL,SIZE | egrep -v 'NAME|loop|part|rom' | tr -s ' ' | sed -e 's/ /_/g' -e 's/_/|/1' -e 's/_/|/1' -e 's/\(.*\)_/\1|/' > $TMPDIR/block.txt
echo 22 > $TMPDIR/sshport.txt

## Setup browser:
if [ -e /mnt/ISO/xzm/002-chrome.xzm ]; then
    mkdir -p /dev/shm && chmod 1777 /dev/shm
    ln -sf /usr/bin/chrome /usr/bin/firefox
    chflag="-"
    ln -sf /usr/share/pixmaps/chrome-48.png /usr/share/pixmaps/browser-48.png
else
    ln -sf /usr/share/pixmaps/firefox-48.png /usr/share/pixmaps/browser-48.png
fi

# Launch wizard:
WIZARD_MAIN='
<window title="Server Wizard" icon-name="kiosk" resizable="false" width-request="780" height-request="580" decorated="true">
<vbox>

## <------- FIRST PAGE CONFIGURATIONS
<notebook page="0" show-tabs="false" show-border="false" labels="configurations|report|autoupdates|device"  height-request="540">

################## PAGE 0 SERVER CHOICE
<vbox margin="10">
        <text use-markup="true" yalign="1"><label>"<span weight='"'bold'"' size='"'x-large'"'>Server version</span>"</label></text>
        <hseparator></hseparator>
        <text xalign="0" width-request="760" wrap="true"><label>Select Porteus Kiosk Server version you would like to install. Variant "'"Basic"'" is available for free, "'"Premium"'" requires paid subscription and offers more features.</label></text>
        <text><label>""</label></text>
        <hbox>
	    <text xalign="0" width-request="645" wrap="true"><label>Click on the "'"Visit webpage"'" button to find complete description and comparison of the products:</label></text>
	    <button>
		<label>Visit webpage</label>
		<action>killall firefox chrome 2>/dev/null; su '$chflag' guest -c "DISPLAY=:0 firefox http://porteus-kiosk.org/server.html &"; sleep 3</action>
	    </button>
	</hbox>
        <text><label>""</label></text>
        <text xalign="0" width-request="740" wrap="true"><label>Before you buy the subscription you are entitled to a 30 days free trial of the "'"Premium"'" version of the product. Please contact with sales@porteus-kiosk.org in order to activate the trial.</label></text>
        <text><label>""</label></text>
        <text xalign="0" width-request="740" wrap="true"><label>Server ID is needed only when you install "'"Premium"'" version of the product. Please provide it when contacting with sales@porteus-kiosk.org in order to get your server activated in our database:</label></text>
        <text><label>""</label></text>
        <hseparator></hseparator>
        <text xalign="1" width-request="640" use-markup="true" selectable="true"><label>"<span weight='"'bold'"'>Server identification number (Server ID): '$PCID'</span>"</label></text>
        <hseparator></hseparator>
        <text><label>""</label></text>
        <text><label>""</label></text>
        <hbox>
                <button image-position="2" tooltip-text="Porteus Kiosk Server version Basic">
                        <width>170</width>
                        <height>170</height>
                        <input file>'$ICONS'/server-basic-128.png</input>
                        <action>echo "Basic" > /tmp/server-choice</action>
                        <action>echo 2 > '$TMPDIR'/.knetPage</action>
                        <action function="refresh">nPage</action>
                        <action function="show">butBurn</action>
                </button>
                <text width-request="80"><label>" "</label></text>
                <button image-position="2" tooltip-text="Porteus Kiosk Server version Premium">
                        <width>170</width>
                        <height>170</height>
                        <input file>'$ICONS'/server-premium-128.png</input>
                        <action>echo "Premium" > /tmp/server-choice</action>
                        <action>echo 1 > '$TMPDIR'/.knetPage</action>
                        <action function="refresh">nPage</action>
                        <action function="show">butNextWizard</action>
                </button>
                <text width-request="140"><label>" "</label></text>
        </hbox>
</vbox>

################## PAGE 1 WIZARD
<vbox scrollable="true" margin="8" hscrollbar-policy="2">
## <------- PAGE TITLE
        <text use-markup="true" yalign="1"><label>"<span weight='"'bold'"' size='"'x-large'"'>Server Wizard</span>"</label></text>
        <hseparator></hseparator>
        <text xalign="0" use-markup="true"><label>"<span weight='"'bold'"'>Scroll down for more options.</span>"</label></text>
        <text><label>""</label></text>

######### ICMP PROTOCOL
        <frame ICMP protocol>
                <hbox>
                        <pixmap><input file>'$ICONS'/icmp-48.png</input><variable>pixIcmp</variable></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Allow incoming ICMP. This is useful when you want to check if the Server is up and running from another computer using the ping utility">
                                        <label>Enable</label>
                                        <variable>rbutIcmpEn</variable>
                                        <default>false</default>
                                        <action>if true echo allow_icmp_protocol=yes > $TMPDIR/icmp.tmp</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Disable the ability to ping the Server from other computers" file-monitor="true" auto-refresh="true">
                                        <label>Disable</label>
                                        <variable>rbutIcmpDis</variable>
                                        <default>true</default>
                                        <input file>'$TMPDIR'/icmpDisable.mon</input>
                                        <action>if true rm $TMPDIR/icmp.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
## <------- This text is blank padding to put space after radiobuttons
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <variable>txtIcmp</variable>
                                <label>"Answer incoming ICMP queries. This is useful when you want to check if the Server is up and running from another computer using the ping utility."</label>
                        </text>
                </hbox>
        </frame>

######### TIME ZONE
        <frame Time zone>
                <hbox>
                        <pixmap><input file>'$ICONS'/timezone-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Enable custom timezone">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <action>if true gtk_list "Choose a timezone" "Choose a timezone from the list below:" 760 timezone /usr/share/wizard/timezones.txt '$TMPDIR'/timezone.tmp ntpserver</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Use Factory as default timezone">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <action>if true rm '$TMPDIR'/timezone.tmp '$TMPDIR'/ntp-server.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>Choose a timezone from a list of world timezones. Hardware clock will be set through the NTP protocol during every system boot if PC is connected to the network. Make sure that your company firewall is not blocking this protocol.</label>
                        </text>
                </hbox>
        </frame>

######### KEYBOARD LAYOUT
        <frame Primary keyboard layout>
                <hbox>
                        <pixmap><input file>'$ICONS'/keyboard-48.png</input><variable>pixKbdPri</variable></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Enable custom keyboard layout" file-monitor="true" auto-refresh="true">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <input file>'$TMPDIR'/pkbdl.mon</input>
                                        <variable>rbutKbdPriEn</variable>
                                        <action>if true gtk_list "Primary keyboard layout" "Choose a keyboard layout from the list:" 760 primary_keyboard_layout /usr/share/wizard/keyboards.txt '$TMPDIR'/keyboard1.txt keyboard1</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Use en-US as default keyboard layout" file-monitor="true" auto-refresh="true">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <variable>rbutKbdPriDis</variable>
                                        <input file>'$TMPDIR'/KbdPri.mon</input>
                                        <action>if true rm '$TMPDIR'/keyboard1.tmp 2>/dev/null</action>
                                        <action>if true sed -i /^primary_keyboard_layout/d /tmp/config 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>Choose a primary keyboard from the list of known keyboard layouts and variants. You can also choose a secondary keyboard layout below. The default is English (US).</label>
                                <variable>txtKbdPri</variable>
                        </text>
                </hbox>
        </frame>
	<frame Secondary keyboard layout>
                <hbox>
                        <pixmap><input file>'$ICONS'/keyboard-48.png</input><variable>pixKbdSec</variable></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Enable secondary keyboard layout">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <variable>rbutKbdSecEn</variable>
                                        <action>if true gtk_list "Secondary keyboard layout" "Choose a keyboard layout from the list:" 760 secondary_keyboard_layout /usr/share/wizard/keyboards.txt '$TMPDIR'/keyboard1.txt keyboard2</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Use only one keyboard layout" file-monitor="true" auto-refresh="true">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <variable>rbutKbdSecDis</variable>
                                        <input file>'$TMPDIR'/KbdSec.mon</input>
                                        <action>if true rm '$TMPDIR'/keyboard2.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>Choose a secondary keyboard layout. Use Ctrl+Space key combination to switch between the layouts when system is installed.</label>
                                <variable>txtKbdSec</variable>
                        </text>
                </hbox>
        </frame>

######### SCREEN
	<frame Screen resolution>
                <hbox>
                        <pixmap><input file>'$ICONS'/screenres-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Set a lower screen resolution">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <action>if true xrandr|grep [0-9].*x[0-9]|grep -v connect|tr -s "'" "'"|cut -d"'" "'" -f2 > '$TMPDIR'/res.txt</action>
                                        <action> if true gtk_list "Choose resolution" "Choose from one of the supported screen resolutions or enter your own below (provided value must be supported by the monitor)." 760 screen_resolution '$TMPDIR'/res.txt '$TMPDIR'/res.tmp res</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Use the maximum available screen resolution">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <action>if true rm '$TMPDIR'/res.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>By default the system will use maximum supported screen resolution but you may also set a custom lower resolution.</label>
                        </text>
                </hbox>
      </frame>

######### SOUND CARD
        <frame Default system sound card>
                <hbox>
                        <pixmap><input file>'$ICONS'/sound-card-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Set default sound card">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <action>if true aplay -l | grep ^card > '$TMPDIR'/sound-cards.txt</action>
                                        <action>if true gtk_list "Default system sound card" "Choose default sound device from the list below:" 760 default_sound_card '$TMPDIR'/sound-cards.txt '$TMPDIR'/selected-card.txt sound-card</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Do not change default sound card">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <action>if true rm '$TMPDIR'/sound-card.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>Set default sound card in case when you have no sound in the system.</label>
                        </text>
                </hbox>
        </frame>

######### SOUND VOLUME LEVEL
        <frame Sound volume>
                <hbox>
                        <pixmap><input file>'$ICONS'/sound-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Set custom sound volume level">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <action>if true gtk_get_range "Set sound volume level" "Use the slider to select a percentage value of the sound level to use. When set to 0 the sound will be muted." 760 volume_level '$TMPDIR'/sound.tmp 100 %</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Set sound volume level to 90%">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <action>if true rm '$TMPDIR'/sound.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>By default the system will set the sound volume to 90% but you may also choose a custom level.</label>
                        </text>
                </hbox>
        </frame>

######### SESSION PASSWORD
        <frame Session password>
                <hbox>
                        <pixmap><input file>'$ICONS'/lock-48.png</input><width>48</width></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Protect the session with a password">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <action>if true get_input "Session password" "Enter the password which will be used to unlock the session for the user." 760 '$TMPDIR'/sessionp.tmp session_password '$TMPDIR'/sessionp.txt</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Keep the session unlocked">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <action>if true rm $TMPDIR/sessionp.tmp</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>"Protect the session with a password. Only authorized users can access the Administration Panel and use the Server."</label>
                        </text>
                </hbox>
        </frame>

######### DPMS
        <frame DPMS>
                <hbox>
                        <pixmap><input file>'$ICONS'/dpms-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Enable DPMS">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <action>if true echo "enable_dpms=yes" > '$TMPDIR'/dpms.tmp</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Disable DPMS">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <action>if true rm '$TMPDIR'/dpms.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>Enable DPMS (Display Power Management Signaling) which turns the monitor off after 10 minutes of keyboard/mouse inactivity.</label>
                        </text>
                </hbox>
        </frame>

######### UEFI
	<frame UEFI support>
                <hbox>
                        <pixmap><input file>'$ICONS'/uefi-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Add UEFI component to the ISO" file-monitor="true" auto-refresh="true">
                                        <label>Enable</label>
                                        <default>true</default>
                                        <input file>'$TMPDIR'/uefiEn.mon</input>
                                        <action>if true echo uefi.zip > '$TMPDIR'/uefi.add</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Do not add the UEFI component to the ISO" file-monitor="true" auto-refresh="true">
                                        <label>Disable</label>
                                        <default>false</default>
                                        <input file>'$TMPDIR'/uefiDis.mon</input>
                                        <action>if true rm '$TMPDIR'/uefi.add 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>UEFI component allows booting Porteus Kiosk on PCs supplied with UEFI firmware and others supplied with traditional BIOS. Disable this component if you experience booting problems.</label>
                        </text>
                </hbox>
        </frame>

######### ADDITIONAL FONTS
	<frame Additional fonts>
                <hbox>
                        <pixmap><input file>'$ICONS'/fonts-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Include additional fonts">
                                        <label>Enable</label>
                                        <default>false</default>
                                        <action>if true echo 06-fonts.xzm > '$TMPDIR'/fonts.add</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Exclude additional fonts">
                                        <label>Disable</label>
                                        <default>true</default>
                                        <action>if true rm '$TMPDIR'/fonts.add 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>Full dejavu font package and also various other fonts for displaying characters from asian languages: Chinese, Hindi, Korean, Japanese, Thai, Vietnamese and various others.</label>
                        </text>
                </hbox>
        </frame>

######### SSH COMPONENT
	<frame SSH service>
                <hbox>
                        <pixmap><input file>'$ICONS'/ssh-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Include ssh service" file-monitor="true" auto-refresh="true">
                                        <label>Enable</label>
                                        <variable>rbutSSHEn</variable>
                                        <default>false</default>
                                        <input file>'$TMPDIR'/enablessh.mon</input>
                                        <action>if true echo 08-ssh.xzm > '$TMPDIR'/ssh.add</action>
                                        <action>if true gtk_password "Root Password" "Enter a root password that will be used to connect to the Server through ssh." "root"</action>
                                        <action>if true get_input "SSH Port" "Enter a port number for the SSH Server (optional)" 760 $TMPDIR/sshport.tmp ssh_port $TMPDIR/sshport.txt</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Exclude ssh service" file-monitor="true" auto-refresh="true">
                                        <label>Disable</label>
                                        <variable>rbutSSHDis</variable>
                                        <default>true</default>
                                        <input file>'$TMPDIR'/disablessh.mon</input>
                                        <action>if true rm '$TMPDIR'/ssh.* 2>/dev/null</action>
                                        <action>if true rm '$TMPDIR'/sshport.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>SSH (Secure shell) service allows to connect/communicate to Porteus Kiosk Server via the SSH protocol. Selected port will be opened in the system firewall configuration.</label>
                        </text>
                </hbox>
	</frame>

######### VNC SERVER
	<frame VNC service>
                <hbox>
                        <pixmap><input file>'$ICONS'/vnc-48.png</input></pixmap>
                        <vbox>
                                <radiobutton tooltip-text="Include vnc service" file-monitor="true" auto-refresh="true">
                                        <label>Enable</label>
                                        <variable>rbutVNCEn</variable>
                                        <default>false</default>
                                        <input file>'$TMPDIR'/enablevnc.mon</input>
                                        <action>if true echo 09-x11vnc.xzm > '$TMPDIR'/vnc.add</action>
                                        <action>if true gtk_password "VNC Server Password" "Enter a vnc password (must be 8 characters or less) that will be used to connect to the Server through vnc." vnc</action>
                                        <action>if true echo "vnc_interactive=yes" > '$TMPDIR'/vnc-control.tmp</action>
                                </radiobutton>
                                <radiobutton tooltip-text="Exclude vnc service" file-monitor="true" auto-refresh="true">
                                        <label>Disable</label>
                                        <variable>rbutVNCDis</variable>
                                        <default>true</default>
                                        <input file>'$TMPDIR'/disablevnc.mon</input>
                                        <action>if true rm '$TMPDIR'/vnc.* 2>/dev/null</action>
                                        <action>if true rm '$TMPDIR'/vncport.tmp 2>/dev/null</action>
                                        <action>if true rm '$TMPDIR'/vnc-control.tmp 2>/dev/null</action>
                                        <action>if true rm '$TMPDIR'/vnc-key.tmp 2>/dev/null</action>
                                </radiobutton>
                        </vbox>
                        <text width-request="10"><label>""</label></text>
                        <text yalign="0" width-request="560">
                                <label>VNC service allows to connect to the graphical interface of Porteus Kiosk Server from another computer via the VNC protocol. Useful for monitoring or controlling the Server remotely. Port 5900 will be opened in the system firewall configuration.</label>
                        </text>
                </hbox>
	</frame>

</vbox>

################## PAGE 2 BURN
<vbox margin="10">
        <text use-markup="true" yalign="1"><label>"<span weight='"'bold'"' size='"'x-large'"'>Server installation</span>"</label></text>
        <hseparator></hseparator>
        <text xalign="0" width-request="740" wrap="true"><label>Please choose the target device to which you will be installing Porteus Kiosk Server.</label></text>
        <text><label>""</label></text>
        <text xalign="0" width-request="740" wrap="true" use-markup="true"><label>"<span weight='"'bold'"' color='"'red'"'>WARNING:</span> if you click the '"'Install Server'"' button then Server will be burned on selected device destroying all existing data. Be careful when selecting the device!"</label></text>
        <text><label>""</label></text>
        <hseparator></hseparator>
        <table>
                <variable>tblTarget</variable>
                <width>700</width>
                <label>"NAME               |TYPE           |MODEL                                              |SIZE"</label>
                <height>250</height>
                <input file>'$TMPDIR'/block.txt</input>
                <action>echo $tblTarget > '$TMPDIR'/target.txt</action>
        </table>
</vbox>


######### END OF PAGES
<variable>nPage</variable>
<input file>'$TMPDIR'/.knetPage</input>
</notebook>

######### BUTTONS
<hbox>
        <button visible="false" tooltip-text="Proceed">
                <label>Next</label>
                <variable>butNextWizard</variable>
                <input file stock="gtk-yes"></input>
                <action>process_settings</action>
                <action>echo 2 > '$TMPDIR'/.knetPage</action>
                <action function="refresh">nPage</action>
                <action condition="command_is_true( [ `grep 2 '$TMPDIR'/.knetPage` ] && echo true )">show:butBurn</action>
                <action condition="command_is_true( [ `grep 2 '$TMPDIR'/.knetPage` ] && echo true )">hide:butNextWizard</action>
        </button>
	<button visible="false" tooltip-text="Burn Server ISO on selected device">
		<label>Install Server</label>
		<variable>butBurn</variable>
		<input file>/usr/share/icons/oxygen/16x16/devices/drive-harddisk.png</input>
		<action condition="command_is_true( [ ! -f '$TMPDIR'/target.txt ] && echo true )">gtk_warning "Missing parameter" "You must choose a target device to burn the ISO to." 600</action>
		<action condition="command_is_true( [ ! -f '$TMPDIR'/target.txt ] && echo true )" function="break">break</action>
		<action>echo "burn_dev=$tblTarget" >> /tmp/config</action>
		<action function="exit">finished</action>
	</button>
</hbox>

</vbox>
</window>'

echo "$WIZARD_MAIN" | sed '/^##/d' | gtkdialog -i /usr/share/wizard/wizard-functions -s -c > $TMPDIR/output
