#!/bin/ash
#sfslinux@gmail.com 170126

MSGxxx () {
#notify-send -i info "$srv" "$msg" || gxmessage -center -timeout 5  -borderless -wrap -iconic "$msg"
if [ -n "`which notify-send`" ] ;then
#	notify-send  "$1" "$2" -i info
	ntf -w  "$1" "$2" 
    else
	gxmessage -center -timeout 5  -borderless -wrap  "$1: $2"
fi
}

sudo="`which sudo`"
srv=$1
#[ "$srv" = "" ] && srv=ERROR! && msg="Use: $0 service_name" && MSG && exit
[ "$srv" = "" ] && srv=ERROR! && MSG "Use: $0 service_name" && exit
ip="Ваш IP: `ifconfig |awk '/inet / && !/127.0.0.1/ {print $2}'`"

if [ "`which service`" = "" ] ;then
	srv1=$srv
	[ "$srv" = "ssh" ] && srv1=sshd
	
	if [ "`pidof $srv1`" = "" ] ;then
		st="$srv остановлен"
	    else
		st="$srv запущен"
	fi
else
	st="`$sudo service $srv status`"
fi

#if [ -n "`echo $st|grep " is running"`" ] ;then
if [ -n "`echo $st|grep "запущен"`" ] ;then
	act=Остановить
	ic=gtk-media-stop
else
	st="$srv остановлен"
	act=Запустить
	ic=gtk-media-play
fi

WARN(){ 
w="Через ssh посторонние могут зайти в Вашу систему. 
Чтобы этого не произошло, измените стандартные (woofwoof) пароли всех пользователей"  
ssh=" SSH"
}
#WARN
[ "$srv" = "dropbear" ] && sico=ssh || sico="mime-application:x-shellscript"
export DIALOG='
<window title="'$srv'" resizable="false" icon-name="'$sico'">
 <vbox>
  <hbox>
  <pixmap><height>48</height><width>48</width><input file icon="'$sico'"></input></pixmap>
  <text use-markup="true">
    <label>"<b>'$st'. '$ip'</b>

'$w'
    "</label>
  </text>
  </hbox>
  <hbox>
       <button>
        <input file icon="startup_config"></input>
        <label>Автозапуск</label>
        <action>xfce4-session-settings || wmstartups &</action>
       </button>

       <button>
        <input file icon="lock"></input>
        <label>Смена пароля</label>
        <action>chpass &</action>
       </button>

       <button>
        <input file icon="gtk-help"></input>
        <label>Справка</label>
        <action>defaultbrowser "https://ru.wikipedia.org/wiki/SSH" &</action>
       </button>
  </hbox>
  <hbox>
       <button>
        <input file stock="'$ic'"></input>
        <label>'$act'</label>
       </button>

       <button cancel></button>
  </hbox>
 </vbox>
</window>
'
ret="`gtkdialog --program=DIALOG  --center`"
eval "$ret"
#echo $ret; echo $EXIT;echo $f;exit

service="service "
[ "`which service`" = "" ] && service="sh /etc/init.d/"


case "$EXIT" in
"Запустить" ) 
if [ "$srv" = "ssh" ] ;then
#[ `id -u` -ne "0" ] && echo "You are not root" 1>&2 && exit 1
# ssh keygen
#/etc/init.d/xinetd stop

`which sudo` rm -f /etc/ssh/ssh_host_*

#lsb_release -cs | grep -vqE '(lucid|maverick)' && ssh-keygen -q -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa
`which sudo`     ssh-keygen -q -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa &&
`which sudo`     ssh-keygen -q -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa &&
     echo "New SSH keys have been generated"

#/etc/init.d/ssh stop
#update-rc.d -f ssh remove
#initctl stop ssh
#/etc/init.d/xinetd start

#if [ -f /etc/rc.d/PUPSTATE ] ;then
WARN
fi

[ "$srv" = "dropbear" ] && WARN && ntf -i "SSH dropbear" "$w" &

    msg="`$sudo $srv\ctl start`" 
    pidof $srv && yad --text="$srv$ssh запущен 
$ip
Нажмите для остановки"  --notification --image=$sico   --command='bash -c "wt \"sudo pkill -9 '$srv1'\" & kill $YAD_PID"' &


#$w"
#    [ "`which service`" = "" ] && msg="запущен"
    ;;
"Остановить" ) 
    msg="`$sudo $srv\ctl stop`" 
    kill `pgrep -f "yad --text=dropbear"`
#    [ "`which service`" = "" ] && msg="остановлен"
    ;;
*       ) exit;;
esac
#MSG "$srv" "$msg"
ntf -i "$srv" "$msg" &
#wt "$msg"