#!/bin/sh
#  connectd_register
#  for repetitive mfg line configuration of services
#
#  Run 'sudo connectd_register' to install remote.it
#  services for tcp listeners.
#
#=======================================================================
# For production:
# remot3.it, Inc. recommends running this script (with real username/password filled in below) from /tmp folder.
# e.g. use wget to copy this script to /tmp from a local web server on your LAN, then run it.
# on next boot, /tmp folder is erased, so there is no trace of account credentials left over.
#
# As supplied for demo, script will prompt you for your username and password.
#
#=======================================================================
# To customize this script for your own purposes:
# Edit the makeConnection lines towards the bottom of this file to specify
# which remote.it services you wish to install.
#
# Installs these remote.it services:
#
#  rmt3 (bulk service, required for "Device Name"
#  ssh on port 22 
#  http on port 80 
#  tcp on port 3389 (commented out)
#
# remot3.it, Inc. Copyright 2019. All rights reserved.
#

##### Settings #####
AUTHOR="Gary Worsham"
MODIFIED="October 15, 2019"
DEBUG="1"
USERNAME=""
PASSWORD=""
AUTHHASH="REPLACE_AUTHHASH"
APIKEY="remote.it.developertoolsHW9iHnd"

#==================================================================================
# include all of the registration functions

. /usr/bin/connectd_library

######### Main Program #########
main()
{
    displayVersion
    platformDetection
    echo "Platform = " $PLATFORM 
#-----------------------------------------------------------------------
#
    if [ "$APIKEY" = "" ]; then
        echo "APIKEY is blank.  Please contact support@remote.it for an API key."
	      exit 1
    fi
#-----------------------------------------------------------------------

    if [ "$1" != "" ]; then
        USERNAME=$1
    fi
    if [ "$2" != "" ]; then
        PASSWORD=$2
    fi
    userLogin
    testLogin

# =============================================    
# $SERVICEBASENAME is the base name of all installed services installed
# with connectd_register
#
# By default, we are setting SERVICEBASENAME to $mac
# This forms the first part of the displayed service name.
# OEM can supply some other expression for SERVICEBASENAME here
# but it must be unique per device.
#
# See /usr/bin/connectd_options for the definition of the "mac" variable.
# 
    SERVICEBASENAME="$(echo $mac | sed $HWIDSEDSTRING)"
    echo "SERVICEBASENAME = $SERVICEBASENAME"
    echo
# =============================================    
#
# ----- Edit the lines below to specify which services you want to have installed
#
# line syntax is:
# makeConnection <protocol> <port> <serviceName>
# <protocol> should be one of: ssh, web, webp, vnc, tcp, rmt3
# rmt3 is used to supply the "Device Name"
#
# <port> is the port number of the service you wish to connect to
# except for rmt3, in which case set <port> to 65535
#
# <protocol> and <port> should be unique for each entry, i.e. you cannot have multiple
# entries which are "ssh 22" but you could have "ssh 22", "ssh 23", "tcp 22".
#
# <name> is a quote-enclosed expression which should be unique for every device
# 
# =============================================    
    makeConnection rmt3 65535 "$SERVICEBASENAME-rmt3"
    makeConnection ssh 22 "$SERVICEBASENAME-ssh-22"
    makeConnection web 80 "$SERVICEBASENAME-web-80"
    makeConnection vnc 5900 "$SERVICEBASENAME-vnc-5900"
# =============================================  

    echo
}

######### End Main Program #########
# you can optionally call this script your with username and password as cmd line parameters
# e.g. sudo ./connectd_register myusername mypassword
# in the event your password has special characters in it such as ! or $, you must
# precede those characters with a backslash \ when passing credentials on the command line.

main $1 $2
