#!/bin/bash

# Program: Premote version 0.9.3                                                      
# Creation date (YMD): 2012/09/07 (original version 0.9.0)                                        
# Revision date (YMD): 2012/09/27
# (C) Copyright 2012 William McEwan; License GPLv3  
#
# Changes:
# 0.9.3 Fixed helptext for internationalisation.
#211019 sfs

trap default INT TERM

Pversion_="0.9.3r"

export TEXTDOMAIN=premote
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8

[ $(which defaulttexteditor) ] && export P_TXT_EDITOR="defaulttexteditor" || export P_TXT_EDITOR="geany"
programtitle="Premote $Pversion_"
export PROGRAMNAME="premote"
export PROGINSTANCENAME=${PROGRAMNAME}$(date +%s)
CFG_NAME="premote_default.rc"
export PROGRAMHOME=~/.${PROGRAMNAME}
CONFIGFILE=$PROGRAMHOME/"${CFG_NAME}"

export CMD1_
export A U P

if [ ! -s /etc/${PROGRAMNAME}/"${CFG_NAME}" ] || [ ! -s /etc/${PROGRAMNAME}/cmd_lists/CMD1_combo ];then
 printf "Default config files missing, please reinstall premote\n"
 exit 1
else
 if [ ! -d $PROGRAMHOME ];then
  cp -a /etc/${PROGRAMNAME} ~/.${PROGRAMNAME}
 else
  [ ! -s "$CONFIGFILE" ] && cp -a /etc/${PROGRAMNAME}/"${CFG_NAME}" $PROGRAMHOME
  [ ! -s $PROGRAMHOME/cmd_lists/CMD1_combo ] && cp -a /etc/${PROGRAMNAME}/cmd_lists/CMD1_combo $PROGRAMHOME/cmd_lists
 fi
fi

[ ! -e /tmp/${PROGINSTANCENAME} ] && mkdir /tmp/${PROGINSTANCENAME}
echo $$ > /tmp/${PROGINSTANCENAME}/PIDstartbinsh
export TMPCONFIGFILE="/tmp/${PROGINSTANCENAME}/${CFG_NAME}"
TMPCMD1_combo=/tmp/${PROGINSTANCENAME}/CMD1_combo

if [ -s "$CONFIGFILE" ];then
 cp -f "$CONFIGFILE" "$TMPCONFIGFILE"
else
 printf "Config files missing, re-run premote to self-repair that\n"
 exit 1
fi

function helpsetup (){
  printf "$(gettext "Program: Premote
(C) Copyright 2012 William McEwan; GPLv3
----

Premote is a GUI front end to remote desktop and login clients such as rdesktop (rdp), vnc, and secure shell (ssh). It thus depends for functionality on the underlying commandline client being installed.

On pressing the \"play\" button, it operates just as if the displayed command was typed into a console window and the \"Enter key pressed\", but it brings the following advantages of a GUI environment:
")""$(gettext "

* A dropdown list of pre-saved commands, which can be edited (modified, deleted, or appended to) in your system's default text editor by pressing the Edit button on the righthand side of the dropdown list.
")""$(gettext '
* Entry boxes (variables) for remote Computer URL address (A), remote login User name (U), and remote login Password (P). The dropdown command can refer to these using standard bash shell syntax (i.e. $A, $U, $P).
')""$(gettext "

* Entry data can be input manually, or via drag and drop, or cut and paste.

* Premote also provides brief tool-tip text over most elements of its GUI, which becomes visible when the mouse is hovered over each element. 

More generally, Premote provides an editable, floating commandline (with a dropdown list of pre-stored commandlines) which can be any commandline at all as long as it is valid bash syntax. Pretty much any bash commandline including quote marks and \ can be entered.

")""$(gettext "

----

Commandline Usage

In a console enter command: premote --help
                            for brief commandline usage.
----

Tips:

To keep premote as the top window, right-click on its window bar, and select Layer -> Top
----

mcewanw, Sep 2012")" > /tmp/${PROGINSTANCENAME}/helpsetup.txt
}

function make_combolist (){
  cp -f $PROGRAMHOME/cmd_lists/CMD1_combo $TMPCMD1_combo
  sed -i -e 's/\\/\\\\/g' $TMPCMD1_combo
  CMD1_ITEMS=""
  while read "i";do
    CMD1_ITEMS="${CMD1_ITEMS}<item>${i//\</@@\\<}</item>"
  done < $TMPCMD1_combo
}

function default (){
  quit
  [ -f "$CONFIGFILE" ] &&  rm -f "$CONFIGFILE" 2>/dev/null
  [ -d /tmp/${PROGINSTANCENAME}/ ] && rm -rf /tmp/${PROGINSTANCENAME}/ 2>/dev/null
  exit
}

function config_gui (){
#  CMD1_=${CMD1_//\"/@@\\\"}
  config_dialog_
  gtkdialog --program=MAIN_DIALOG --center > /tmp/${PROGINSTANCENAME}/maindialog
  sed -i -e "/^CMD1_=/ s/'/'\\\''/g" -e 's/^CMD1_=\"/CMD1_='\''/g' -e '/^CMD1_=/ s/"$/'\''/g' /tmp/${PROGINSTANCENAME}/maindialog
  MAIN_DIALOG=$(</tmp/${PROGINSTANCENAME}/maindialog)
  if [ "$(echo "$MAIN_DIALOG" | grep 'EXIT="abort"')" = "" ];then
      echo "$MAIN_DIALOG" > "$TMPCONFIGFILE"
      . "$TMPCONFIGFILE"
  fi
}

function do_cmdline (){
   eval "${CMD1_//@@\\/}" &       
}
export -f do_cmdline helpsetup

function quit (){
 [ -d /tmp/${PROGINSTANCENAME}/ ] && rm -rf /tmp/${PROGINSTANCENAME}/ 2>/dev/null
}

config_dialog_ (){

export HELP_SETUP="
  <window title=\"$programtitle $(gettext 'Help')\"icon-name=\"gtk-help\">
   <vbox>
    <edit wrap-mode=\"2\"><width>600</width><height>500</height>
     <variable>HELPTEXTSETUP</variable>
     <input file>\"/tmp/${PROGINSTANCENAME}/helpsetup.txt\"</input>
    </edit>
    <hbox>
     <button ok>
      <variable>HELP_SETUP</variable> 
      <action type=\"closewindow\">HELP_SETUP</action>
     </button>
    </hbox>
   </vbox>
  </window>"
  
export MAIN_DIALOG="
<window width-request=\"736\" title=\"$programtitle $(gettext 'Press Help button or hover mouse over elements for brief usage help') \"icon-name=\"gtk-network\">
<vbox>

 <hbox space-expand=\"true\">
  <pixmap><height>48</height><width>48</width><input file icon=\"disconnect\"></input></pixmap>
  <text wrap=\"false\" use-markup=\"true\"><label>\"<b>Менеджер подключения к удаленным рабочим столам и ресурсам</b>
Обнаружены: <i>$(basename `which rdesktop`) $(basename `which xfreerdp`) $(basename `which dbclient`) $(basename `which vncviewer`) $(basename `which sshfs`) $(basename `which curlftpfs`) $(basename `which mount.cifs`) $(basename `which wdfs`)</i>\"</label></text>
  <pixmap><height>48</height><width>48</width><input file icon=\"connect\"></input></pixmap>
 </hbox>
 <hbox>

     <frame $(gettext 'Command. This dropdown command list can reference any of the variables below')>       
      <hbox>
       <comboboxentry width-request=\"658\" tooltip-text=\"$(gettext 'Select from the dropdown list, enter, or copy and paste here, the commandline that you wish to execute')\">
        <variable>CMD1_</variable>
        "${CMD1_ITEMS}"
       </comboboxentry>
       <button tooltip-text=\"$(gettext 'Edit combobox commandlines to modify, re-order, delete, or add to them')\">
        <input file stock=\"gtk-edit\"></input>
        <action>$P_TXT_EDITOR ~/.premote/cmd_lists/CMD1_combo &</action>
       </button>       
       <button tooltip-text=\"$(gettext 'Run Commandline')\">
        <input file stock=\"gtk-media-play\"></input>
        <action>do_cmdline &</action>
       </button>
      </hbox>
     </frame>

               

 </hbox>

<hbox homogeneous=\"true\">
 <frame $(gettext 'Remote Computer')>
  <hbox homogeneous=\"true\">
     <frame $(gettext 'Computer URL ') (A)>
      <entry tooltip-text=\"$(gettext 'Drag and drop, paste, or enter the remote computer URL into here')\" editable=\"true\" accept=\"filename\">
        <variable>A</variable>
        <input>echo \"\$A\"</input>
      </entry>
     </frame>
     <frame $(gettext 'Enter User login name (if required) ') (U)>
      <entry tooltip-text=\"$(gettext 'Copy and paste or enter the remote login username here')\" editable=\"true\" accept=\"filename\">
        <variable>U</variable>
        <input>echo \"\$U\"</input>
      </entry>
     </frame>
     <frame $(gettext 'Enter Password (if required)') (P)>
      <entry tooltip-text=\"$(gettext 'Copy and paste or enter the remote login password here')\" editable=\"true\" accept=\"filename\">
        <visible>password</visible>
        <variable>P</variable>
        <input>echo \"\$P\"</input>
      </entry>
     </frame>
  </hbox>
 </frame>
</hbox>

  <hbox homogeneous=\"false\">
      <button tooltip-text=\"$(gettext 'Help')\">
       <label>$(gettext 'Help')</label>
       <input file stock=\"gtk-help\"></input>
       <variable>HELPSETBUT</variable>
	   <action>launch:HELP_SETUP</action>
      </button>
      <button height-request=\"30\" tooltip-text=\"$(gettext 'Quit')\">
       <label>$(gettext 'Quit')</label>
       <input file stock=\"gtk-quit\"></input>
       <action type=\"exit\">EXIT-QUIT</action>
      </button>
  </hbox>

 </vbox>
</window>"
}

make_combolist

_argv1="$1"
set -u
# process commandline arguments
case "$_argv1" in
  "")
	helpsetup
	;;
  "quit")
	trap - INT TERM EXIT
	quit
	exit;;
  '--help'|'-?')
	printf "$(gettext 'premote version %s
From commandline run as:
premote [options]
Commandline options:
quit : clean up all and quit program.
--help : this brief usage help.
-? : this brief usage help.
Note that premote automatically keeps
track of the running process IDs.
If no options are entered the premote GUI will start.') 
" "$Pversion_"
	trap - INT TERM EXIT
	quit
	exit;;
  *)
	helpsetup
	;;
esac
. "$TMPCONFIGFILE"
config_gui
sed -i -e '/^P=[^ ]*/ d' "$TMPCONFIGFILE"
if [ -f "$TMPCONFIGFILE" ];then
  cp -f "$TMPCONFIGFILE" "$CONFIGFILE" 2>/dev/null
else
  [ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE" 2>/dev/null
fi 
quit
trap - INT TERM EXIT
exit 0
