#!/bin/bash ########################################################################### # # Copyright (c) 2019-2020 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 copy.sh # # @author (last) Behrouz NematiPour # @date (last) 01-Nov-2021 # @author (original) Behrouz NematiPour # @date (original) 13-Mar-2020 # ############################################################################ # @details # This file is used to setup newly flashed SoM to copy everythin on the device. SRC_PATH_SCRIPTS="scripts" SRC_PATH_CONFIG="settings" SRC_PATH_FONTS="fonts" DST_IP="192.168.10.135" DST_USER=root DST_PATH_CONFIG="/home/$DST_USER/.config" DST_PATH_HOME="/home/$DST_USER" DST_PATH_SCRIPTS="/home/$DST_USER/scripts" DST_PATH_FONTS="/usr/share/fonts/truetype" SRC_PATH_DENALI="." DENALI_BIN=denali # device default Ip Address and denali application path if [ -n "$1" ]; then DST_IP="$1" fi function folderExists() { if [ ! -d "$1" ]; then echo 1 return fi echo 0 } function fileExists() { if [ ! -f "$1" ]; then echo 1 return fi echo 0 } function copyFolderTo() { echo $1 scp -r $1/* $DST_USER@$DST_IP:$2 if [ ! $? -eq 0 ];then echo "copy of folder $1 unsuccessful" return 1 fi return 0 } function copyFileTo() { if [ $(fileExists "$1") -eq 0 ]; then scp $1 $DST_USER@$DST_IP:$2 if [ ! $? -eq 0 ];then echo "copy of file $1 unsuccessful" return 1 fi else echo " ~~~ File $1 doesn't exist" fi return 0 } function exitconfirm() { read -p "Continue? [y,n]" -n 1 -r CONTINUE if [ "$CONTINUE" != "y" ]; then echo "" exit "$1" else echo "" fi } function removeIPFromHost() { ssh-keygen -f "/home/denali/.ssh/known_hosts" -R "$DST_IP" sleep 5 } # getting the ip address of the device function getIpAddress() { while true; do read -p "Please enter the device Ip address: " -e -i $DST_IP -r DST_IP echo "removing device Ip Address from known hosts" removeIPFromHost echo "Testing connection on IP $DST_IP" if [ ! -z "$DST_IP" ]; then ping "$DST_IP" -c1 -W1 if [ $? -eq 0 ]; then break else echo " ~~~ Cannot connect to the device." echo " ~~~ Either device not connected or the IP address is not correct." exitconfirm 1 fi fi done echo "Connection successful on device with IP $DST_IP" echo "" } # getting the denali application path function getDenaliPath() { while true; do read -p "Please enter the Denali application path: " -e -i $SRC_PATH_DENALI -r SRC_PATH_DENALI echo "Testing the Denali application path" if [ $(fileExists "$SRC_PATH_DENALI/$DENALI_BIN") -eq 0 ]; then break else echo " ~~~ Cannot find the Denali application." echo " ~~~ Either the path is not correct or the Denali application doesn't exist." exitconfirm 2 fi done echo "The Denali application found successfully in path '$SRC_PATH_DENALI'" echo "" } # getting the fonts path function getFontsPath() { while true; do read -p "Please enter the fonts path: " -e -i $SRC_PATH_FONTS -r SRC_PATH_FONTS echo "Testing fonts path" if [ "$(folderExists "$SRC_PATH_FONTS")" -eq 0 ]; then if [ "$(ls)" != "" ]; then break else echo " ~~~ The folder $SRC_PATH_FONTS is empty" exitconfirm 3 fi else echo " ~~~ Cannot find Fonts path." exitconfirm 4 fi done echo "Fonts found successfully in path '$SRC_PATH_FONTS'" echo "" } # SSH Connection function connect() { echo "please ssh into device $DST_IP and run ./setup.sh" read -p "Do you want to ssh into device? [y,n]" -n 1 -r CONFIRM if [ "$CONFIRM" == "y" ]; then echo "" ssh $DST_USER@$DST_IP else echo "" fi } # copy the settings scripts function setupSettingsScripts() { ssh $DST_USER@$DST_IP "mkdir -p $DST_PATH_SCRIPTS;" copyFolderTo $SRC_PATH_SCRIPTS $DST_PATH_SCRIPTS ssh $DST_USER@$DST_IP "cd $1; chmod a+x *.sh;" } # copy the instructions function setupInstructions() { ssh $DST_USER@$DST_IP "mkdir -p $DST_PATH_CONFIG;" ssh $DST_USER@$DST_IP "rm -frd $DST_PATH_CONFIG/*;" copyFolderTo $SRC_PATH_CONFIG $DST_PATH_CONFIG } # copy the fonts function setupFonts() { getFontsPath copyFolderTo $SRC_PATH_FONTS $DST_PATH_FONTS } function main() { getIpAddress copyFileTo "autostart" $DST_PATH_HOME copyFileTo "run.sh" $DST_PATH_HOME copyFileTo "setup.sh" $DST_PATH_HOME setupSettingsScripts setupInstructions setupFonts getDenaliPath copyFileTo "$SRC_PATH_DENALI/$DENALI_BIN" $DST_PATH_HOME } # running the main function main connect