#!/bin/bash

# opencocon VPN menu

SELECT=/tmp/.cocon.vpnmenu.select
SSH_HOSTN=/tmp/.cocon.sshhost
SSH_PORT=/tmp/.cocon.sshport
SSH_USERN=/tmp/.cocon.sshuser
SSH_FWHOSTN=/tmp/.cocon.fwhost
SSH_FWPORT=/tmp/.cocon.fwport
SE_HOSTN=/tmp/.cocon.sehost
SE_USERN=/tmp/.cocon.seuser
SE_HUBN=/tmp/.cocon.sehub
SE_METHOD=/tmp/.cocon.semethod
SE_GATEWAY=/tmp/.cocon.segw
SE_FLAG=/tmp/.cocon.softether
SE_DISCONN=/tmp/.cocon.sedisconn
SE_GATEWAY_SELECT=/tmp/.cocon.segw.select
SE_RESOLV_BACKUP=/tmp/.cocon.seresolv
PARTCNF=/tmp/.cocon.cnf.part


while :
do
  dialog --no-cancel --menu "VPN connect menu" 20 60 25 \
    SSHPF "Setup SSH port forwarding" \
    SE    "Connect (or disconnect) SoftEther VPN Server" \
    SEST  "SoftEther connection status" \
    RET   "Return to main menu" 2>$SELECT

  select=`cat $SELECT`

  case $select in

  SSHPF)
    dialog --inputbox "Enter SSH hostname to connect" 8 40 2>$SSH_HOSTN
    dialog --inputbox "Enter SSH port to connect (if default port(22), just enter)" 8 40 2>$SSH_PORT
    dialog --inputbox "Enter SSH username" 8 40 2>$SSH_USERN
    dialog --inputbox "Enter forward hostname" 8 40 2>$SSH_FWHOSTN
    dialog --inputbox "Enter forward port (RDP:3389, VNC:5900+Display number)" 8 40 2>$SSH_FWPORT

    ssh_hostn=`cat $SSH_HOSTN`
    ssh_port=`cat $SSH_PORT`
    ssh_user=`cat $SSH_USERN`
    ssh_fwhostn=`cat $SSH_FWHOSTN`
    ssh_fwport=`cat $SSH_FWPORT`

    if [ -n "$ssh_hostn" -a -n "$ssh_user" -a -n "$ssh_fwhostn" -a -n "$ssh_fwport" ];
    then
      if [ -z "$ssh_port" ];
      then
        ssh_port=22
      fi

      # TODO : move to sepalated script
      /usr/bin/ssh -N -f -L $ssh_fwport:$ssh_fwhostn:$ssh_fwport -p $ssh_port -l $ssh_user $ssh_hostn

      if [ -z "`pidof ssh`" ];
      then
        dialog --msgbox "Failed to connect." 15 60
      else
        dialog --msgbox "Okey, SSH port-forwarding is connected. please input hostname to 'localhost'." 15 60
      fi
    fi
    ;;


  SE)

    if [ -e $SE_FLAG ];
    then
      # Disconnect?
      dialog --yesno "Disconnect current SoftEther connection?" 8 40 2>$SE_DISCONN
      se_disconn_ret="$?"
      if [ "$se_disconn_ret" = "0" ];
      then
        cocon-softether-launch hangup
      fi
    else

      dialog --inputbox "Enter SoftEther VPN hostname:port to connect" 8 40 2>$SE_HOSTN
      dialog --inputbox "Enter Hub name to connect" 8 40 2>$SE_HUBN

      dialog --no-cancel --menu "Select auth method" 20 60 25 \
        PASS "Password Auth (standard)" \
        ANON "Anonymous Login"  2>$SE_METHOD

      dialog --inputbox "Enter SoftEther username" 8 40 2>$SE_USERN


      dialog --no-cancel --menu "Select default gateway to use" 20 60 25 \
        PHY "Physical gateway" \
        VPN "VPN gateway" 2>$SE_GATEWAY_SELECT

      if [ `cat $SE_GATEWAY_SELECT` = "VPN" ];
      then
        se_gateway_onvpn=1
      else
        se_gateway_onvpn=0
      fi
  
      se_host=`cat $SE_HOSTN`
      se_user=`cat $SE_USERN`
      se_hub=`cat $SE_HUBN`
      se_method=`cat $SE_METHOD`


      if [ -n $se_host -a -n $se_user -a -n $se_hub ];
      then
        echo "COCON_SE_HOST=$se_host" >> $PARTCNF
        echo "COCON_SE_USER=$se_user" >> $PARTCNF
        echo "COCON_SE_HUB=$se_hub" >> $PARTCNF
        echo "COCON_SE_AUTH_METHOD=$se_method" >> $PARTCNF
        echo "COCON_SE_WAIT_CONNECTED=4" >> $PARTCNF
        echo "COCON_SE_SET_GATEWAY_ONVPN=$se_gateway_onvpn" >> $PARTCNF

        cocon-softether-launch connect
        ret=$?
        if [ $ret -eq 0 ];
        then
          dialog --msgbox "Okey, SoftEther connect complete." 15 60
        fi

        rm $PARTCNF
        
      fi

    fi
    ;;


  SEST)
    about=` cocon-softether-launch status `
    dialog --msgbox "$about" 40 80
    ;;

  RET)
    exit 1
    ;;


  esac

done

