giuliomoro@425: #!/bin/sh
andrewm@58: #
giuliomoro@377: # This script copies the core Bela files to the BeagleBone Black
andrewm@58: # in preparation for building projects. It will remove any existing
giuliomoro@377: # Bela directory before copying the files over
giuliomoro@447: 
giuliomoro@461: # Find location of this script so we can locate the rest of the files
giuliomoro@461: SCRIPTDIR=$(dirname "$0")
giuliomoro@461: [ -z $SCRIPTDIR ] && SCRIPTDIR="./" || SCRIPTDIR=$SCRIPTDIR/ 
giuliomoro@461: . $SCRIPTDIR.bela_common || { echo "You must be in Bela/scripts to run these scripts" | exit 1; }  
giuliomoro@461: 
giuliomoro@461: [ -z "$IDE_FOLDER" ] && IDE_FOLDER=$SCRIPTDIR/../../bela-ide/
giuliomoro@413: 
giuliomoro@413: FILES_TO_COPY="core include Makefile libNE10.a libprussdrv.a examples Doxyfile"
giuliomoro@413: 
giuliomoro@413: # The following variables are manually copied from the Makefile.
giuliomoro@413: # It is a bit nasty, but here we need to handle the case that the board may be 
giuliomoro@413: # in any arbitrarily (bad) condition and try to handle it the best we can
giuliomoro@413: BELA_IDE_SCREEN_NAME=IDE-Bela
giuliomoro@413: SCREEN_NAME=Bela
giuliomoro@413: 
giuliomoro@428: usage ()
andrewm@58: {
andrewm@58:     THIS_SCRIPT=`basename "$0"`
giuliomoro@452:     echo "Usage: $THIS_SCRIPT [--clean] [--no-frills] [-dy ] [-i user@ip] [-b /path/to/remote/Bela]"
andrewm@58: 
andrewm@58:     echo "
giuliomoro@413:     This script updates the core Bela files on the BeagleBone, bringing it up
giuliomoro@415:     to date with the files in the folder on the host computer.
giuliomoro@413:     The script must be run once to initialize the board before running any of
giuliomoro@413:     the other scripts in this directory. It must also be run every time you
giuliomoro@413:     wish to update the core code.
giuliomoro@413:     Running this script will discard all changes to the core code on the remote
giuliomoro@413:     end.
giuliomoro@413: 
giuliomoro@413: Command line options:    
giuliomoro@413:     -i arg : allows to set the username and IP address of the remote end (default: $BBB_ADDRESS)
giuliomoro@413:     -b arg : allows to set the path to the Bela folder on the remote end. Use quotes if you use the \"~\" symbol (default: $BBB_BELA_HOME)
giuliomoro@413:     --clean : removes all the files in the $BBB_BELA_HOME folder, INCLUDING any source code you may have in the $BBB_BELA_HOME/projects/ subdirectory.
giuliomoro@452:     -d : skips re-generating Doxygen documentation on the board.
giuliomoro@484:     -n : do not try to update the IDE.
giuliomoro@452:     --no-frills : skips some optional commands.
giuliomoro@413:     -y : does not prompt the user before deleting the remote files.
giuliomoro@413:     "
andrewm@58: }
giuliomoro@412: 
giuliomoro@428: signal_handler ()
giuliomoro@428: {
giuliomoro@428:   echo
giuliomoro@428:   exit
giuliomoro@428: }
giuliomoro@428: 
giuliomoro@432: trap signal_handler 2 
giuliomoro@428: 
giuliomoro@428: error_handler () {
giuliomoro@412:  [ $1 -eq 0 ] && printf "done\n" || { [ -z "$2" ] && printf "\nAn error occurred. Is the board connected?\n" || printf "$2"; exit 1; }
giuliomoro@412: }
andrewm@58: 
giuliomoro@452: FULL=1
andrewm@58: OPTIND=1
giuliomoro@412: ALWAYS_YES=0
giuliomoro@413: RESET_BOARD=0
giuliomoro@452: DOXYGEN=1
giuliomoro@484: UPDATE_IDE=1
giuliomoro@413: while [ "$1" != "" ]; do
giuliomoro@413:   case $1 in
giuliomoro@413:     -b)           
giuliomoro@413:       shift
giuliomoro@413:       BBB_BELA_HOME=$1
giuliomoro@413:     ;;
giuliomoro@413:     -i)
giuliomoro@413:       shift
giuliomoro@413:       BBB_ADDRESS=$1
giuliomoro@413:     ;;
giuliomoro@413:     --clean)
giuliomoro@413:       RESET_BOARD=1
giuliomoro@413:     ;;
giuliomoro@452:     -d)
giuliomoro@452:       DOXYGEN=0
giuliomoro@452:     ;;
giuliomoro@484:     -n)
giuliomoro@484:       UPDATE_IDE=0
giuliomoro@484:     ;;
giuliomoro@452:     --no-frills)
giuliomoro@452:       FULL=0
giuliomoro@452:     ;;
giuliomoro@413:     -y)          
giuliomoro@413:        ALWAYS_YES=1
giuliomoro@413:     ;;
giuliomoro@413:     *)            
giuliomoro@413:       usage
giuliomoro@413:       exit 1
giuliomoro@413:     ;;
giuliomoro@412:   esac
giuliomoro@413:   shift
andrewm@58: done
andrewm@58: 
andrewm@58: 
andrewm@58: 
giuliomoro@454: # Check if destination folder exists
giuliomoro@454: # the StrictHostKeyChecking no should prevent the unkown host prompt
giuliomoro@454: ssh -o "StrictHostKeyChecking no" $BBB_ADDRESS stat $BBB_BELA_HOME &>/dev/null && DESTINATION_EMPTY=0 || DESTINATION_EMPTY=1
giuliomoro@452: # Set the date on the board
giuliomoro@452: [ $FULL -eq 1 ] && set_date 
giuliomoro@413: 
giuliomoro@413: if [ $DESTINATION_EMPTY -eq 0 ];
andrewm@58: then
giuliomoro@413:   echo "Updating the Bela core code in $BBB_ADDRESS:$BBB_BELA_HOME"
giuliomoro@413:   if [ $RESET_BOARD -eq 1 ];
giuliomoro@413:   then
giuliomoro@413:     printf "DANGER: you are about to perform a HARD RESET, which will DELETE all the  existing files from the folder $BBB_BELA_HOME on the BeagleBone Black.\nAre you sure you want to continue? (y/N) "
giuliomoro@413:   else
giuliomoro@413:     printf "All the changes to the CORE files in the remote folder $BBB_BELA_HOME will be LOST. The content of these files/folders will be lost:\n  $FILES_TO_COPY \nYour projects stored in $BBB_BELA_HOME/projects should be safe (and surely you have already backed them up somewhere safe, haven't you?).\nAre you sure you want to continue? (y/N) "
giuliomoro@413:   fi;
giuliomoro@413:   if [ $ALWAYS_YES -eq 1 ];
giuliomoro@413:   then
giuliomoro@413:     printf "y\n"
giuliomoro@413:   else 
giuliomoro@413:     read REPLY;
giuliomoro@439:     [ -z "$REPLY" ] || { [ "$REPLY" !=  y ]  && [ "$REPLY" != Y ]; } && { echo "Aborting..."; exit 1; }
giuliomoro@413:   fi
giuliomoro@413: else
giuliomoro@413:   echo "Installing Bela core code in $BBB_ADDRESS:$BBB_BELA_HOME"
giuliomoro@413: fi
giuliomoro@412: 
giuliomoro@452: echo "Updating Bela core files to remote folder $BBB_BELA_HOME"
giuliomoro@377: # Stop Bela if running and remove all files
giuliomoro@454: if [ $FULL -eq 1 ]
giuliomoro@454: then
giuliomoro@454:   printf "Stopping Bela..." 
giuliomoro@454:   ssh $BBB_ADDRESS "make -C $BBB_BELA_HOME idestop stop &>/dev/null; screen -X -S Bela quit &>/dev/null; screen -X -S IDE-Bela quit &>/dev/null; true";
giuliomoro@454:   error_handler $?
giuliomoro@454: fi
andrewm@58: 
giuliomoro@413: if [ $RESET_BOARD -eq 1 ];
giuliomoro@413: then
giuliomoro@413:   printf "Removing old files..."
giuliomoro@413:   ssh $BBB_ADDRESS "rm -rf $BBB_BELA_HOME";
giuliomoro@413:   error_handler $?
giuliomoro@413: fi
giuliomoro@413: 
giuliomoro@413: #Check if rsync is available
giuliomoro@413: [ -z `which rsync` ] && RSYNC_AVAILABLE=0 || RSYNC_AVAILABLE=1
giuliomoro@413: 
giuliomoro@420: # cd to the home of the project to make the following lines less verbose
giuliomoro@420: RUN_FOLDER=`pwd`
giuliomoro@413: cd $SCRIPTDIR/../
giuliomoro@420: 
giuliomoro@413: printf "Updating files..."
andrewm@58: # Copy relevant files to BeagleBone Black
giuliomoro@413: if [ $RSYNC_AVAILABLE -eq 1 ];
giuliomoro@413: then
giuliomoro@413:   [ -z `which sed` ] && FILTER=cat || FILTER="sed s/\\n// | sed s/^.*:/Updated\ files:\/g | tr \"\n\" \" \""
giuliomoro@461:   rsync -ac --no-t --delete-after --stats $FILES_TO_COPY $BBB_ADDRESS:$BBB_BELA_HOME 2>/dev/null |grep -i "Number" | grep -i "files" | grep -i "transferred" | eval $FILTER &&\
giuliomoro@477:   printf "..." && rsync -ac --no-t libpd.so $BBB_ADDRESS:/usr/lib
giuliomoro@413: else
giuliomoro@413:   # if rsync is not available, then clean the folders before copying the files
giuliomoro@413:   ssh $BBB_ADDRESS "rm -rf $FILES_TO_COPY; mkdir -p $BBB_BELA_HOME" &&\
giuliomoro@413:   scp -r -q $FILES_TO_COPY $BBB_ADDRESS:$BBB_BELA_HOME &&\
giuliomoro@413:   scp -q libpd.so $BBB_ADDRESS:/usr/lib
giuliomoro@413: fi
giuliomoro@412: error_handler $?
giuliomoro@420: # cd back to the original folder.
giuliomoro@420: cd "$RUN_FOLDER"
giuliomoro@412: 
giuliomoro@402: # Create remaining directories needed for building
giuliomoro@412: printf "Creating directory structure on BeagleBone..."
giuliomoro@415: ssh $BBB_ADDRESS "mkdir -p $BBB_BELA_HOME/build/core $BBB_BELA_HOME/projects"
giuliomoro@412: error_handler $?
giuliomoro@412: 
giuliomoro@452: if [ $DOXYGEN -eq 1 ] 
giuliomoro@452: then 
giuliomoro@452:   printf "Generating on-board documentation..."
giuliomoro@452:   ssh $BBB_ADDRESS "cd $BBB_BELA_HOME; doxygen &> /dev/null"
giuliomoro@452:   error_handler $? "\nError while generating Doxygen documentation\n"
giuliomoro@452: fi
andrewm@58: 
l@451: printf "Cleaning build environment..."
giuliomoro@477: ssh $BBB_ADDRESS "make -C $BBB_BELA_HOME --no-print-directory coreclean &>/dev/null"
l@451: error_handler $? "\nError cleaning core\n"
l@451: 
giuliomoro@402: #-------------
giuliomoro@402: #Installing IDE
giuliomoro@484: 
giuliomoro@484: if [ $UPDATE_IDE -eq 1 ]
giuliomoro@402: then
giuliomoro@484: 	[ $ALWAYS_YES -eq 0 ] && ALWAYS_YES_FLAG= || ALWAYS_YES_FLAG="-y"
giuliomoro@484: 	[ $FULL -eq 1 ] && NO_FRILLS_FLAG= || NO_FRILLS_FLAG="--no-frills"
giuliomoro@484: 	ls $IDE_FOLDER/scripts/update_IDE >/dev/null 2>/dev/null
giuliomoro@484: 	if [ $? -eq 0 ]
giuliomoro@484: 	then
giuliomoro@484: 	  export BBB_ADDRESS BBB_BELA_HOME
giuliomoro@484: 	  cd $IDE_FOLDER/scripts && ./update_IDE $ALWAYS_YES_FLAG $NO_FRILLS_FLAG
giuliomoro@484: 	  # The IDE is restarted by the update_IDE script
giuliomoro@484: 	else
giuliomoro@484: 	  # run the IDE 
giuliomoro@484: 	   printf "\nThe Bela core files were updated on the board, but a valid IDE folder was not found in $IDE_FOLDER/, so the IDE was not updated.
giuliomoro@484: 	You can get a copy of the most up-to-date IDE files from https://github.com/LBDonovan/bela-ide\n"
giuliomoro@484: 	  [ $FULL -eq 1 ] && printf "If there was an older version of the IDE on the board, it is being restarted.\n\n" &&\
giuliomoro@484: 	    ssh $BBB_ADDRESS "make -C $BBB_BELA_HOME --no-print-directory idestart" 
giuliomoro@484: 	fi
giuliomoro@402: else
giuliomoro@484: 	echo "Not updating the IDE"
giuliomoro@419: fi
giuliomoro@477: [ $FULL -eq 1 ] && ssh $BBB_ADDRESS make -C $BBB_BELA_HOME --no-print-directory idestartup nostartup && printf "The board will now run the IDE at startup, but startup of the Bela program has been disabled.\nTo enable it, use the set_startup.sh script.\nTo control the ide use the ide.sh script\n"
giuliomoro@421: 
giuliomoro@452: [ $ALWAYS_YES -eq 1 ] && printf "\nSetup complete.\n\n" || {
giuliomoro@477:   printf '\nSetup complete, press any key to continue . . .\n\n'
giuliomoro@437:   read 
giuliomoro@437: }