#!/bin/bash

set -e
clear
if [ "root" != "${USER}" ]; then
  echo "ERROR: You must be logged in as the 'root' user."
  echo "Remote support session aborted."
  exit 1
fi

if [ -z "${1}" ]; then
  RANDOM=`/bin/date +%s`
  PORT=$((($RANDOM%20000)+10000))
else
  PORT=$1
fi

if [ ! -x "/usr/bin/ssh" ]; then
  echo "WARNING: ssh is not yet installed and is required for remote support to operate."
  echo -n "Install the required software package now (Y/N)? "
  read answer
  case "$answer" in
    Y|y)
    ;;

    *)
      echo "Remote support session aborted."
      exit 1
    ;;
  esac

  apt-get install ssh
fi

if [ ! -x "/usr/bin/corkscrew" ]; then
  echo "WARNING: corkscrew is not yet installed and is required for remote support to operate."
  echo -n "Install the required software package now (Y/N)? "
  read answer
  case "$answer" in
    Y|y)
    ;;

    *)
      echo "Remote support session aborted."
      exit 1
    ;;
  esac

  apt-get install corkscrew
fi

clear
echo "Your remote support number is ${PORT}, please take a note of this number and"
echo "quote it to the CensorNet support team when you contact them."
echo
echo "Press return to open the remote support session..."
read
echo "Connecting, please wait..."
ssh -o 'ProxyCommand /usr/bin/corkscrew 127.0.0.1 3128 %h %p' -o 'StrictHostKeyChecking=no' -p 443 -l support -R ${PORT}:127.0.0.1:22 support.censornet.com
