#!/usr/bin/env bash # the preceding line instructs this script to run using the bash interpreter # main function definition perform_install() { # echo: output a message to the screen echo 'Starting activation process...' # install python3 install_python3 # install python3-pip (python3 package installer) install_python3_pip # install websocket-client python3 library install_websocket_client # install smbus2 python3 library install_smbus2 # download the limitos.py file download_limitos_py # add the limitos.py process to systemctl install_limitos_py # remove previous installations remove_limitos_js # output the registration information echo "Register this device within 5 minutes at https://limitos.com/register" echo "Registration code: $registration_auth_token" } # function to install python3 function install_python3() { # check if python3 is installed # command -v: outputs location of specified program, wc -l: count line numbers python3_installed=$(command -v python3 | wc -l) # if python3 is not installed (command location line count is zero) if [ $python3_installed -eq 0 ] # then execute the commands below then # output activation message echo 'python3 not found, installing...' # install python3, and log output sudo apt-get install -y python3 &> 'limitos_install.log' # end the if statement fi } # function to install python3-pip function install_python3_pip() { # check if python3-pip is installed python3_pip_installed=$(command -v pip3 | wc -l) # if python3-pip is not installed if [ $python3_pip_installed -eq 0 ] # then execute the commands below then # output activation message echo 'python3-pip not found, installing...' # install python3-pip, and log output sudo apt-get install python3-pip -y &> 'limitos_install.log' # end the if statement fi } # function to install python3 websocket libary (websocket-client) # checking if a library is installed takes almost as long as installation, so we proceed with installation only function install_websocket_client() { # output activation message echo 'installing websocket-client...' # install websocket-client, and log output sudo pip3 install websocket-client &> 'limitos_install.log' } # function to install python3 bus communication library (smbus2) function install_smbus2() { # output activation message echo 'installing smbus2...' # install smbus2, and log output sudo pip3 install smbus2 &> 'limitos_install.log' } # function to download limitos.py function download_limitos_py() { # create a new device and save the auth_token as device_json # curl: request a remote url, -s: silent, -S: show errors, -L: follow redirects, -X POST: use a HTTP POST request device_json=$(curl -sSL -X POST https://limitos.com/api/v1/devices) # get the device id from the JSON # grep: search, -P: use Perl regular expressions, -o: only output the match, '"id":(\d*)': look for "id":(digits) # awk: pattern manipulation, -F':': separate by ':', print $2: print the second match device_id=$(echo $device_json | grep -Po '"id":(\d*)' | awk -F':' '{print $2}') # get the auth token from the JSON # '"auth_token":.*?[^\\]"': look for "auth_token":"xxxxxx", tr -d '"': delete quotes device_auth_token=$(echo $device_json | grep -Po '"auth_token":.*?[^\\]"' | awk -F':' '{print $2}' | tr -d '"') # get a new registration code (using the device auth_token) registration_json=$(curl -sSL --data "auth_token=$device_auth_token" https://limitos.com/api/v1/devices/$device_id/registrations) # get the auth token from the JSON registration_auth_token=$(echo $registration_json | grep -Po '"auth_token":.*?[^\\]"' | awk -F':' '{print $2}' | tr -d '"') # retrieve the python script and save it as limitos.py curl -sS --data "auth_token=$device_auth_token" https://limitos.com/api/v1/devices/$device_id/python_script > limitos_unverified.py # if the file is valid if python3 -c "import ast; ast.parse(open('limitos_unverified.py').read())" # then execute the commands below then # move the file mv limitos_unverified.py limitos.py # else the script is invalid else # exit exit 0 # end the if statement fi } # function to remove previous node.js installations of the limitos.py process function remove_limitos_js() { # check if pm2 is installed pm2_installed=$(command -v pm2 | wc -l) # if pm2 is installed if [ $pm2_installed -eq 1 ] # then execute the commands below then # remove node.js script sudo pm2 -s stop limitos sudo pm2 -s delete limitos sudo pm2 -s save rm limitos*.js 2> /dev/null # end the if statement fi } # function to install the limitos.py process function install_limitos_py() { # output message echo 'starting limitos.py process...' # get the python3 directory python3_directory=$(which python3) # get the current directory current_directory=$(pwd) # retrieve the systemd script curl -sS "https://limitos.com/api/v1/devices/systemd_script?python3_directory=$python3_directory¤t_directory=$current_directory" > limitos.service # create directories mkdir ~/.config/systemd 2> /dev/null mkdir ~/.config/systemd/user 2> /dev/null # move the service file to ~/.config/systemd/user/ mv limitos.service ~/.config/systemd/user/ # enable lingering of pi user so that systemd --user service is started on boot loginctl enable-linger pi # reload systemd systemctl --user daemon-reload # start (or restart) the systemd service systemctl --user restart limitos # start limitos at boot systemctl --user enable limitos 2> /dev/null } # run the main function perform_install