#!/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. FLG_QUIET=0 SSH_QUIET="" EMT_IP="192.168." ERR_CONNECTION=1 ERR_DENALI_BIN=2 ERR_FONTS_EMTY=3 ERR_FONTS_PATH=4 ERR_KILLPROMPT=5 SRC_PATH_SCRIPTS="scripts" SRC_PATH_CONFIG="settings" SRC_PATH_FONTS="fonts" DST_IP=$EMT_IP 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 if [ "$1" = "-h" -o "$1" = "--help" ]; then echo -e "-h \t This help" echo -e "-q \t Quiet mode" echo "usage start.sh [] [-q]" exit 0 fi if [ "$1" = "-q" -o "$2" = "-q" ]; then FLG_QUIET=1 SSH_QUIET="-oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -oLogLevel=ERROR -oConnectTimeout=5 -oHostKeyAlgorithms=+ssh-rsa" echo -e "Running in quiet mode." fi if [ -n "$1" -a "$1" != "-q" ]; then DST_IP="$1" fi function runssh() { ssh $SSH_QUIET $DST_USER@$DST_IP "$1" } 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 $SSH_QUIET $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 $SSH_QUIET $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 killPrompt() { if [ ! $FLG_QUIET -eq 1 ]; then echo "All the settings, configurations and binaries will be overwritten" exitconfirm $ERR_KILLPROMPT 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 if [ "$DST_IP" = "$EMT_IP" ]; then read -p "Please enter the device Ip address: " -e -i "$DST_IP" -r DST_IP else if [ ! $FLG_QUIET -eq 1 ]; then read -p "Please enter the device Ip address: " -e -i "$DST_IP" -r DST_IP fi fi if [ ! -z "$DST_IP" ]; then echo "removing device Ip Address from known hosts" removeIPFromHost echo "Testing connection on IP $DST_IP" runssh "exit 0" 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 $ERR_CONNECTION fi fi done echo "Connection successful on device with IP $DST_IP" echo "" } # getting the denali application path function getDenaliPath() { while true; do if [ ! $FLG_QUIET -eq 1 ]; then read -p "Please enter the Denali application path: " -e -i $SRC_PATH_DENALI -r SRC_PATH_DENALI fi 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 $ERR_DENALI_BIN fi done echo "The Denali application found successfully in path '$SRC_PATH_DENALI'" echo "" } # getting the fonts path function getFontsPath() { while true; do if [ ! $FLG_QUIET -eq 1 ]; then read -p "Please enter the fonts path: " -e -i $SRC_PATH_FONTS -r SRC_PATH_FONTS fi 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 $ERR_FONTS_EMTY fi else echo " ~~~ Cannot find Fonts path." exitconfirm $ERR_FONTS_PATH fi done echo "Fonts found successfully in path '$SRC_PATH_FONTS'" echo "" } # SSH Connection function connect() { echo "******************************************************" echo "please ssh into device $DST_IP and run ./setup.sh" echo "******************************************************" read -p "Hit enter to continue" runssh # the setup.sh has to run on the device while user has logged into the device. } # copy the settings scripts function setupSettingsScripts() { runssh "mkdir -p $DST_PATH_SCRIPTS ;" runssh "rm -frd $DST_PATH_SCRIPTS/*;" copyFolderTo $SRC_PATH_SCRIPTS $DST_PATH_SCRIPTS runssh "cd $1; chmod a+x *.sh;" } # copy the instructions function setupInstructions() { runssh "mkdir -p $DST_PATH_CONFIG ;" runssh "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 killPrompt copyFileTo "autostart" $DST_PATH_HOME copyFileTo "run.sh" $DST_PATH_HOME copyFileTo "setup.sh" $DST_PATH_HOME setupSettingsScripts setupInstructions setupFonts getDenaliPath runssh "killall $DENALI_BIN" copyFileTo "$SRC_PATH_DENALI/$DENALI_BIN" $DST_PATH_HOME } # running the main function main connect