From fe39a1d4b5824489fe202ac479818fda1eb3927d Mon Sep 17 00:00:00 2001 From: cleoqc Date: Fri, 6 Jan 2017 15:22:18 +0000 Subject: [PATCH 1/8] first set of scripts --- functions_library.sh | 183 +++++++++++++++++++++++++++++++++++++++++++ update_wiringpi.sh | 45 +++++++++++ 2 files changed, 228 insertions(+) create mode 100755 functions_library.sh create mode 100755 update_wiringpi.sh diff --git a/functions_library.sh b/functions_library.sh new file mode 100755 index 0000000..96ba0c0 --- /dev/null +++ b/functions_library.sh @@ -0,0 +1,183 @@ +############################################################## +############################################################## +# +# A SERIES OF HELPER FUNCTIONS TO HELP OUT IN +# HANDLING SCRIPTS THAT ARE GROWING IN COMPLEXITY +# +############################################################## +############################################################## + +quiet_mode() { + # verify quiet mode + # returns 0 if quiet mode is enabled + # returns 1 otherwise + if [ -f /home/pi/quiet_mode ] + then + return 0 + else + return 1 + fi +} + +set_quiet_mode(){ + touch /home/pi/quiet_mode +} + +unset_quiet_mode(){ + delete_file /home/pi/quiet_mode +} + + +feedback() { + # first parameter is text to be displayed + # this sets the text color to a yellow color for visibility + # the last tput resets colors to default + # one could also set background color with setb instead of setaf + #http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html + echo -e "$(tput setaf 3)$1$(tput sgr0)" +} + +######################################################################### +# +# FILE EDITION +# +######################################################################### +delete_line_from_file() { + # first parameter is the string to be matched + # the lines that contain that string will get deleted + # second parameter is the filename + if [ -f $2 ] + then + sudo sed -i "/$1/d" $2 + feedback "deleted $1 from $2" + fi +} + +insert_before_line_in_file() { + # first argument is the line that needs to be inserted DO NOT USE PATHS WITH / in them + # second argument is a partial match of the line we need to find to insert before + # third arument is filename + + feedback "Inserting $1 before $2 in $3" + if [ -f $3 ] + then + feedback "sudo sed -i '/$2/i $1' $3" + sudo sed -i "/$2/i $1" $3 + fi +} +add_line_to_end_of_file() { + # first parameter is what to add + # second parameter is filename + if [ -f $2 ] + then + echo $1 >> $2 + fi +} + +find_in_file() { + # first argument is what to look for + # second argument is the filename + feedback "looking for $1 in $2" + if grep -q "$1" $2 + then + return 0 + else + return 1 + fi +} + +######################################################################### +# +# FILE HANDLING - detection, deletion +# +######################################################################### +file_exists() { + # Only one argument: the file to look for + # returns 0 on SUCCESS + # returns 1 on FAIL + if [ -f $1 ] + then + return 0 + else + return 1 + fi +} + +file_exists_in_folder(){ + # can only be run using bash, not sh + # first argument: file to look for + # second argument: folder path + pushd $2 + status = file_exists + popd + return status +} + +file_does_not_exists(){ + # Only one argument: the file to look for + # returns 0 on SUCCESS + # returns 1 on FAIL + feedback "looking for $1" + if [ ! -f $1 ] + then + feedback "not found $1" + return 0 + else + feedback "found $1" + return 1 + fi +} + +delete_file (){ + # One parameter only: the file to delete + if file_exists $1 + then + sudo rm $1 + fi +} + +wget_file() { + # One parameter: the URL of the file to wget + # this will look if ther's already a file of the same name + # if there's one, it will delete it before wgetting the new one + # this is to avoid creating multiple files with .1, .2, .3 extensions + echo $1 + # extract the filename from the provided path + target_file=${1##*/} + echo $target_file + delete_file $target_file + wget $1 --no-check-certificate + + +} + +######################################################################### +# +# FOLDER HANDLING - detection, deletion +# +######################################################################### +create_folder(){ + if ! folder_exists + then + mkdir $1 + fi +} + +folder_exists(){ + # Only one argument: the folder to look for + # returns 0 on SUCCESS + # returns 1 on FAIL + if [ -d $1 ] + then + return 0 + else + return 1 + fi +} + +delete_folder(){ + if folder_exists $1 + then + sudo rm -r $1 + fi +} \ No newline at end of file diff --git a/update_wiringpi.sh b/update_wiringpi.sh new file mode 100755 index 0000000..8d1a26f --- /dev/null +++ b/update_wiringpi.sh @@ -0,0 +1,45 @@ +#!/bin/bash +source /home/pi/di_update/Raspbian_For_Robots/upd_script/functions_library.sh + +# Check if WiringPi Installed and has the latest version. If it does, skip the step. +# Gets the version of wiringPi installed +version=`gpio -v` + +# Parses the version to get the number +set -- $version + +# Gets the third word parsed out of the first line of gpio -v returned. +# Should be 2.36 +WIRINGVERSIONDEC=$3 + +# Store to temp file +echo $WIRINGVERSIONDEC >> tmpversion + +# Remove decimals +VERSION=$(sed 's/\.//g' tmpversion) + +# Remove the temp file +delete_file tmpversion + +feedback "wiringPi VERSION is $VERSION" +if [ $VERSION -eq '236' ]; then + + feedback "FOUND WiringPi Version 2.36 No installation needed." +else + feedback "Did NOT find WiringPi Version 2.36" + # Check if the Dexter directory exists. + DIRECTORY='/home/pi/Dexter' + create_folder "$DIRECTORY" + create_folder "$DIRECTORY"/lib + + # Change directories to Dexter + cd $DIRECTORY/lib + delete_folder wiringPi + # Install wiringPi + git clone https://github.com/DexterInd/wiringPi/ # Clone directories to Dexter. + cd wiringPi + sudo chmod +x ./build + sudo ./build + feedback "wiringPi Installed" +fi +# End check if WiringPi installed From a18d44ded71fbed7bb6e0e2c8aeb0d3dde519730 Mon Sep 17 00:00:00 2001 From: cleoqc Date: Fri, 6 Jan 2017 15:36:03 +0000 Subject: [PATCH 2/8] use this install script with curl to get the files installed --- install_script_tools.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 install_script_tools.sh diff --git a/install_script_tools.sh b/install_script_tools.sh new file mode 100755 index 0000000..3bb4a57 --- /dev/null +++ b/install_script_tools.sh @@ -0,0 +1,39 @@ +#! /bin/bash +##################################################################### +##################################################################### +# +# curl +# +##################################################################### +##################################################################### + +PIHOME=/home/pi +DEXTER=Dexter +LIB=lib + +pushd $PIHOME + +# check if ~/Dexter exists, if not create it +if [ -d $DEXTER ] ; then + mkdir $DEXTER +fi +# go into $DEXTER +cd $DEXTER + +# check if lib exists, if not create it +if [ -d $LIB ] ; then + mkdir $LIB +fi + +# check if /home/pi/Dexter/lib/Dexter exists +# if yes refresh the folder +# if not, clone the folder +if [ -d $DEXTER] ; then + cd $DEXTER + sudo git pull +else + # clone the folder + sudo git clone https://github.com/DexterInd/script_tools.git +fi + +popd \ No newline at end of file From b1fb468eda44b8cf889e4494cc1fe3079dc1c7ab Mon Sep 17 00:00:00 2001 From: cleoqc Date: Fri, 6 Jan 2017 15:38:56 +0000 Subject: [PATCH 3/8] fix a couple of small mistakes --- install_script_tools.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/install_script_tools.sh b/install_script_tools.sh index 3bb4a57..68f1730 100755 --- a/install_script_tools.sh +++ b/install_script_tools.sh @@ -14,21 +14,21 @@ LIB=lib pushd $PIHOME # check if ~/Dexter exists, if not create it -if [ -d $DEXTER ] ; then +if [ ! -d $DEXTER ] ; then mkdir $DEXTER fi # go into $DEXTER cd $DEXTER # check if lib exists, if not create it -if [ -d $LIB ] ; then +if [ ! -d $LIB ] ; then mkdir $LIB fi # check if /home/pi/Dexter/lib/Dexter exists # if yes refresh the folder # if not, clone the folder -if [ -d $DEXTER] ; then +if [ ! -d $DEXTER ] ; then cd $DEXTER sudo git pull else From d3b1bdf68a7b106c1a9d15f1e21007e706285c72 Mon Sep 17 00:00:00 2001 From: cleoqc Date: Fri, 6 Jan 2017 15:40:44 +0000 Subject: [PATCH 4/8] fix the pull command --- install_script_tools.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install_script_tools.sh b/install_script_tools.sh index 68f1730..a510cb9 100755 --- a/install_script_tools.sh +++ b/install_script_tools.sh @@ -20,7 +20,7 @@ fi # go into $DEXTER cd $DEXTER -# check if lib exists, if not create it +# check if /home/pi/Dexter/lib exists, if not create it if [ ! -d $LIB ] ; then mkdir $LIB fi @@ -28,7 +28,7 @@ fi # check if /home/pi/Dexter/lib/Dexter exists # if yes refresh the folder # if not, clone the folder -if [ ! -d $DEXTER ] ; then +if [ -d $DEXTER ] ; then cd $DEXTER sudo git pull else From 392650647304f87fff3f6149675119d4d15c4ecf Mon Sep 17 00:00:00 2001 From: cleoqc Date: Fri, 6 Jan 2017 15:46:34 +0000 Subject: [PATCH 5/8] add debugging statements --- install_script_tools.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/install_script_tools.sh b/install_script_tools.sh index a510cb9..2feb616 100755 --- a/install_script_tools.sh +++ b/install_script_tools.sh @@ -15,6 +15,7 @@ pushd $PIHOME # check if ~/Dexter exists, if not create it if [ ! -d $DEXTER ] ; then + echo "creating $PIHOME/$DEXTER" mkdir $DEXTER fi # go into $DEXTER @@ -22,6 +23,7 @@ cd $DEXTER # check if /home/pi/Dexter/lib exists, if not create it if [ ! -d $LIB ] ; then + echo "creating $PIHOME/$DEXTER/$LIB" mkdir $LIB fi @@ -29,10 +31,12 @@ fi # if yes refresh the folder # if not, clone the folder if [ -d $DEXTER ] ; then + echo "Pulling" cd $DEXTER sudo git pull else # clone the folder + echo "Cloning" sudo git clone https://github.com/DexterInd/script_tools.git fi From 202b32683791abd0a5c81f6697efd934facb2b4f Mon Sep 17 00:00:00 2001 From: cleoqc Date: Fri, 6 Jan 2017 15:51:42 +0000 Subject: [PATCH 6/8] fix cloning again --- install_script_tools.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/install_script_tools.sh b/install_script_tools.sh index 2feb616..f8042ee 100755 --- a/install_script_tools.sh +++ b/install_script_tools.sh @@ -10,6 +10,7 @@ PIHOME=/home/pi DEXTER=Dexter LIB=lib +SCRIPTS=script_tools pushd $PIHOME @@ -27,12 +28,12 @@ if [ ! -d $LIB ] ; then mkdir $LIB fi -# check if /home/pi/Dexter/lib/Dexter exists +# check if /home/pi/Dexter/lib/script_tools exists # if yes refresh the folder # if not, clone the folder -if [ -d $DEXTER ] ; then +if [ -d $SCRIPT ] ; then echo "Pulling" - cd $DEXTER + cd $SCRIPT sudo git pull else # clone the folder From 0f499ef94a6c3b54f0b89fa3a3a3c1e512ab8ad8 Mon Sep 17 00:00:00 2001 From: cleoqc Date: Fri, 6 Jan 2017 15:58:01 +0000 Subject: [PATCH 7/8] forgot an import cd --- install_script_tools.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/install_script_tools.sh b/install_script_tools.sh index f8042ee..461d2b4 100755 --- a/install_script_tools.sh +++ b/install_script_tools.sh @@ -27,6 +27,7 @@ if [ ! -d $LIB ] ; then echo "creating $PIHOME/$DEXTER/$LIB" mkdir $LIB fi +cd $LIB # check if /home/pi/Dexter/lib/script_tools exists # if yes refresh the folder From 27ebf2786a41d16873428c31b3693cfe73b27e0f Mon Sep 17 00:00:00 2001 From: cleoqc Date: Fri, 6 Jan 2017 16:10:32 +0000 Subject: [PATCH 8/8] is this one the right one? --- install_script_tools.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/install_script_tools.sh b/install_script_tools.sh index 461d2b4..730f5dc 100755 --- a/install_script_tools.sh +++ b/install_script_tools.sh @@ -10,10 +10,10 @@ PIHOME=/home/pi DEXTER=Dexter LIB=lib -SCRIPTS=script_tools +SCRIPT=script_tools pushd $PIHOME - +result=${PWD##*/} # check if ~/Dexter exists, if not create it if [ ! -d $DEXTER ] ; then echo "creating $PIHOME/$DEXTER" @@ -21,6 +21,7 @@ if [ ! -d $DEXTER ] ; then fi # go into $DEXTER cd $DEXTER +echo $PWD # check if /home/pi/Dexter/lib exists, if not create it if [ ! -d $LIB ] ; then @@ -28,6 +29,15 @@ if [ ! -d $LIB ] ; then mkdir $LIB fi cd $LIB +echo $PWD + +# check if /home/pi/Dexter/lib/Dexter exists, if not create it +if [ ! -d $DEXTER ] ; then + echo "creating $PIHOME/$DEXTER/$LIB/$Dexter" + mkdir $DEXTER +fi +cd $DEXTER +echo $PWD # check if /home/pi/Dexter/lib/script_tools exists # if yes refresh the folder @@ -35,6 +45,8 @@ cd $LIB if [ -d $SCRIPT ] ; then echo "Pulling" cd $SCRIPT + echo $PWD + echo "now in $PIHOME/$DEXTER/$LIB/$SCRIPT" sudo git pull else # clone the folder