#!/bin/bash ########################################################################### # # Copyright (c) 2021-2023 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file start.sh # # @author (last) Vy # @date (last) 20-May-2023 # @author (original) Behrouz NematiPour # @date (original) 11-Aug-2021 # ############################################################################ # @details # This file is used to setup newly flashed SoM to copy everythin on the device. source ./globals.sh function displayHelp() { if [ "$1" = "-h" -o "$1" = "--help" ]; then echo -e "-h \t This help" echo "usage start.sh [ [xxx] or [xxx.xxx.xxx.xxx] or [] ]" exit 0 fi } function exitConfirm() { read -p "Continue? [y,n]" -n 1 -r CONTINUE if [ "$CONTINUE" != "y" ]; then echo "" exit "$1" else echo "" fi } function defaultIP() { if [ -n "$1" ]; then if [ ${#1} -gt $IP_SEG_MAX_LEN ]; then DST_IP=$1 else DST_IP=$IP_EMT"$1" fi fi } function setupLogs() { local log_location=$LOG_LOCATION/$DST_IP mkdir -p $log_location LOG_OUT_FILE="$log_location/update-$(timestamp).log" LOG_ERR_FILE="$log_location/update-$(timestamp).err" touch $LOG_OUT_FILE touch $LOG_ERR_FILE } function sshKeyExists() { existsFile $SRC_FILE_SSHKEY_PUB return $? } function sshKeyGen() { sshKeyExists if [ ! $? -eq 0 ]; then echo "No ssh key found,Generating ssh key" ssh-keygen -N "" -f $SRC_FILE_SSHKEY 1>>$LOG_OUT_FILE 2>>$LOG_ERR_FILE return $? else echo "Found ssh key " $SRC_FILE_SSHKEY_PUB fi return 0 } function sshKeyCopy() { echo "Registering the ssh key on the device $DST_IP" echo "Please wait ..." ssh-copy-id $SSH_PARAM -f -i $SRC_FILE_SSHKEY_PUB $DST_USER@$DST_IP 1>>$LOG_OUT_FILE 2>>$LOG_ERR_FILE if [ ! $? -eq 0 ]; then echo "Connection to host $DST_IP failed." | tee -a $LOG_OUT_FILE exitConfirm $? return $? fi } function sshRun() { ssh $SSH_PARAM $DST_USER@$DST_IP $1 return $? } function copyFolderTo() { existsFolder "$1" if [ $? -eq 0 ]; then echo -n "Copy folder" >> $LOG_OUT_FILE scp -r $SSH_PARAM $1/* $DST_USER@$DST_IP:$2 if [ $? -eq 0 ]; then echo " Successfull - $1" >> $LOG_OUT_FILE else echo " Failed" >> $LOG_OUT_FILE exitConfirm $? return $? fi else echo "File '$1' doesn't exist" | tee -a $LOG_OUT_FILE exitConfirm $? fi return 0 } function copyFileTo() { existsFile "$1" if [ $? -eq 0 ]; then echo -n "Copy file" >> $LOG_OUT_FILE scp $SSH_PARAM $1 $DST_USER@$DST_IP:$2 if [ $? -eq 0 ]; then echo " Successfull - $1" >> $LOG_OUT_FILE else echo " Failed" >> $LOG_OUT_FILE exitConfirm $? return $? fi else echo "File '$1' doesn't exist" | tee -a $LOG_OUT_FILE exitConfirm $? fi return 0 } function killPrompt() { echo_star_comment echo_star_message "Current running UI Software on the device will be stopped." echo_star_message "All the settings, configurations and binaries will be overwritten" echo_star_comment exitConfirm $ERR_KILLPROMPT } function getDeviceIP() { while true; do validIP "$DST_IP" if [ $? -eq 0 ]; then break else read -p "Please enter the device Ip address: " -e -i "$IP_EMT" -r DST_IP validIP "$DST_IP" if [ $? -eq 0 ]; then break else echo "The entered IP address is not valid [$DST_IP]" exitConfirm $? fi fi done } function setupBootupScripts() { echo_dash_comment echo_dash_message "Installing bootup scripts" | tee -a $LOG_OUT_FILE echo_dash_comment copyFileTo "globals.sh" $DST_PATH_HOME copyFileTo "autostart" $DST_PATH_HOME copyFileTo "run.sh" $DST_PATH_HOME copyFileTo "setup.sh" $DST_PATH_HOME } #TODO to be removed when yocto recipes updated function setupBluetoothConfFile() { echo_dash_comment echo_dash_message "Installing bluetooth conf file" | tee -a $LOG_OUT_FILE echo_dash_comment copyFileTo $SRC_PATH_SCRIPTS/"bluetooth_main.conf" $DST_PATH_BT_CONF/main.conf } function setupSettingsScripts() { echo_dash_comment echo_dash_message "Installing settings scripts" | tee -a $LOG_OUT_FILE echo_dash_comment sshRun "rm -frd $DST_PATH_SCRIPTS;" sshRun "mkdir -p $DST_PATH_SCRIPTS;" copyFolderTo $SRC_PATH_SCRIPTS $DST_PATH_SCRIPTS sshRun "cd $1; chmod a+x *.sh;" } function setupConfigurations() { echo_dash_comment echo_dash_message "Installing configurations" | tee -a $LOG_OUT_FILE echo_dash_comment sshRun "rm -frd $DST_PATH_CONFIG;" sshRun "mkdir -p $DST_PATH_CONFIG;" copyFolderTo $SRC_PATH_CONFIG $DST_PATH_CONFIG } function setupCloudSync() { echo_dash_comment echo_dash_message "Installing CloudSync" | tee -a $LOG_OUT_FILE echo_dash_comment sshRun "killall python3" sshRun "rm -frd $DST_PATH_CLOUDSYNC;" sshRun "mkdir -p $DST_PATH_CLOUDSYNC;" copyFolderTo $SRC_PATH_CLOUDSYNC $DST_PATH_CLOUDSYNC } function setupFonts() { echo_dash_comment echo_dash_message "Installing fonts" | tee -a $LOG_OUT_FILE echo_dash_comment copyFolderTo $SRC_PATH_FONTS $DST_PATH_FONTS } function setupApplication() { echo_dash_comment echo_dash_message "Installing UI Software" | tee -a $LOG_OUT_FILE echo_dash_comment sshRun "killall $DENALI_BIN" copyFileTo $DENALI_BIN $DST_PATH_HOME } function connect() { echo_dash_comment echo_dash_message "please ssh into device $DST_IP and run ./setup.sh " echo_dash_comment read -p "Hit enter to continue" sshRun # the setup.sh has to run on the device while user has logged into the device. } function wipe_device() { sshRun "rm -frd *" sshRun "rm -frd .*" } function main() { displayHelp "$1" defaultIP "$1" getDeviceIP setupLogs sshKeyGen sshKeyCopy killPrompt wipe_device setupBootupScripts setupSettingsScripts setupConfigurations setupBluetoothConfFile setupCloudSync setupFonts setupApplication } # running the main function main "$1" connect exit 0