Untitled diff

Created Diff never expires
257 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
959 lines
237 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
938 lines
#!/bin/bash
#!/bin/bash
# __ __ __
# __ __ __
# / | / |/ |
# / | / |/ |
# ______ ______ __ __ _██ |_ ██/ ██ |
# ______ ______ __ __ _██ |_ ██/ ██ |
# / \ / \ ______ / | / |/ ██ | / |██ |
# / \ / \ ______ / | / |/ ██ | / |██ |
# /██████ |/██████ |/ |██ | ██ |██████/ ██ |██ |
# /██████ |/██████ |/ |██ | ██ |██████/ ██ |██ |
# ██ | ██ |██ | ██ |██████/ ██ | ██ | ██ | __ ██ |██ |
# ██ | ██ |██ | ██ |██████/ ██ | ██ | ██ | __ ██ |██ |
# ██ |__██ |██ \__██ | ██ \__██ | ██ |/ |██ |██ |
# ██ |__██ |██ \__██ | ██ \__██ | ██ |/ |██ |██ |
# ██ ██/ ██ ██/ ██ ██/ ██ ██/ ██ |██ |
# ██ ██/ ██ ██/ ██ ██/ ██ ██/ ██ |██ |
# ███████/ ██████/ ██████/ ████/ ██/ ██/
# ███████/ ██████/ ██████/ ████/ ██/ ██/
# ██ |
# ██ |
# ██ |
# ██ |
# ██/ https://po-util.com
# ██/ https://po-util.com
#
#


# po-util - The Ultimate Local Particle Experience for Linux and macOS
# po-util - The Ultimate Local Particle Experience for Linux and macOS
# Copyright (C) 2017 Nathan Robinson
# Copyright (C) 2017 Nathan Robinson
#
#
# This program is free software: you can redistribute it and/or modify
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# Helper functions
# Helper functions
pause()
pause()
{
{
read -rp "$*"
read -rp "$*"
}
}


print_logo()
print_logo()
{
{
if [ "$1" ];
if [ "$1" ];
then
then
TEXTLINE="$1"
TEXTLINE="$1"
else
else
TEXTLINE="https://po-util.com"
TEXTLINE="https://po-util.com"
fi
fi


LOGO=" __ __ __
LOGO=" __ __ __
/ | / |/ |
/ | / |/ |
______ ______ __ __ _██ |_ ██/ ██ |
______ ______ __ __ _██ |_ ██/ ██ |
/ \ / \ ______ / | / |/ ██ | / |██ |
/ \ / \ ______ / | / |/ ██ | / |██ |
/██████ |/██████ |/ |██ | ██ |██████/ ██ |██ |
/██████ |/██████ |/ |██ | ██ |██████/ ██ |██ |
██ | ██ |██ | ██ |██████/ ██ | ██ | ██ | __ ██ |██ |
██ | ██ |██ | ██ |██████/ ██ | ██ | ██ | __ ██ |██ |
██ |__██ |██ \__██ | ██ \__██ | ██ |/ |██ |██ |
██ |__██ |██ \__██ | ██ \__██ | ██ |/ |██ |██ |
██ ██/ ██ ██/ ██ ██/ ██ ██/ ██ |██ |
██ ██/ ██ ██/ ██ ██/ ██ ██/ ██ |██ |
███████/ ██████/ ██████/ ████/ ██/ ██/
███████/ ██████/ ██████/ ████/ ██/ ██/
██ |
██ |
██ |
██ |
██/ $TEXTLINE
██/ $TEXTLINE
"
"
if [ -t 1 ];
if [ -t 1 ];
then
then
echo "$(tput setaf 6)$(tput bold)$LOGO$(tput sgr0)"
echo "$(tput setaf 6)$(tput bold)$LOGO$(tput sgr0)"
else
else
echo "$LOGO"
echo "$LOGO"
fi
fi
}
}




blue_echo()
blue_echo()
{
{
if [ -t 1 ];
if [ -t 1 ];
then
then
echo "$(tput setaf 6)$(tput bold)$1$(tput sgr0)"
echo "$(tput setaf 6)$(tput bold)$1$(tput sgr0)"
else
else
echo "$1"
echo "$1"
fi
fi
}
}


green_echo()
green_echo()
{
{
if [ -t 1 ];
if [ -t 1 ];
then
then
echo "$(tput setaf 2)$(tput bold)$1$(tput sgr0)"
echo "$(tput setaf 2)$(tput bold)$1$(tput sgr0)"
else
else
echo "$1"
echo "$1"
fi
fi
}
}


red_echo()
red_echo()
{
{
if [ -t 1 ];
if [ -t 1 ];
then
then
echo "$(tput setaf 1)$(tput bold)$1$(tput sgr0)"
echo "$(tput setaf 1)$(tput bold)$1$(tput sgr0)"
else
else
echo "$1"
echo "$1"
fi
fi
}
}


find_objects() #Consolidated function
find_objects() #Consolidated function
{
{
if [ "$1" != "" ];
if [ "$1" != "" ];
then
then
case "$1" in
case "$1" in
*/)
*/)
#"has slash"
#"has slash"
DEVICESFILE="${1%?}"
DEVICESFILE="${1%?}"
FIRMWAREDIR="${1%?}"
FIRMWAREDIR="${1%?}"
FIRMWAREBIN="${1%?}"
FIRMWAREBIN="${1%?}"
DIRECTORY="${1%?}"
DIRECTORY="${1%?}"
;;
;;
*)
*)
echo "doesn't have a slash" > /dev/null
echo "doesn't have a slash" > /dev/null
DEVICESFILE="$1"
DEVICESFILE="$1"
FIRMWAREDIR="$1"
FIRMWAREDIR="$1"
FIRMWAREBIN="$1"
FIRMWAREBIN="$1"
DIRECTORY="$1"
DIRECTORY="$1"
;;
;;
esac
esac
if [ -f "$CWD/$DEVICESFILE/devices.txt" ] || [ -d "$CWD/$FIRMWAREDIR/firmware" ] || [ -f "$CWD/$FIRMWAREBIN/bin/firmware.bin" ];
if [ -f "$CWD/$DEVICESFILE/devices.txt" ] || [ -d "$CWD/$FIRMWAREDIR/firmware" ] || [ -f "$CWD/$FIRMWAREBIN/bin/firmware.bin" ];
then
then
DEVICESFILE="$CWD/$DEVICESFILE/devices.txt"
DEVICESFILE="$CWD/$DEVICESFILE/devices.txt"
FIRMWAREDIR="$CWD/$FIRMWAREDIR/firmware"
FIRMWAREDIR="$CWD/$FIRMWAREDIR/firmware"
FIRMWAREBIN="$CWD/$FIRMWAREBIN/bin/firmware.bin"
FIRMWAREBIN="$CWD/$FIRMWAREBIN/bin/firmware.bin"
else
else
if [ -d "$DIRECTORY" ] && [ -d "$DIRECTORY/firmware" ];
if [ -d "$DIRECTORY" ] && [ -d "$DIRECTORY/firmware" ];
then
then
DEVICESFILE="$DIRECTORY/devices.txt"
DEVICESFILE="$DIRECTORY/devices.txt"
FIRMWAREDIR="$DIRECTORY/firmware"
FIRMWAREDIR="$DIRECTORY/firmware"
FIRMWAREBIN="$DIRECTORY/bin/firmware.bin"
FIRMWAREBIN="$DIRECTORY/bin/firmware.bin"
else
else
if [ -d "$CWD/$DIRECTORY" ] && [ -d "$CWD/firmware" ];
if [ -d "$CWD/$DIRECTORY" ] && [ -d "$CWD/firmware" ];
then
then
DEVICESFILE="$CWD/$DIRECTORY/../devices.txt"
DEVICESFILE="$CWD/$DIRECTORY/../devices.txt"
FIRMWAREDIR="$CWD/$DIRECTORY"
FIRMWAREDIR="$CWD/$DIRECTORY"
FIRMWAREBIN="$CWD/$DIRECTORY/../bin/firmware.bin"
FIRMWAREBIN="$CWD/$DIRECTORY/../bin/firmware.bin"
else
else
if [ "$DIRECTORY" == "." ] && [ -f "$CWD/main.cpp" ];
if [ "$DIRECTORY" == "." ] && [ -f "$CWD/main.cpp" ];
then
then
cd "$CWD/.."
cd "$CWD/.."
DEVICESFILE="$(pwd)/devices.txt"
DEVICESFILE="$(pwd)/devices.txt"
FIRMWAREDIR="$CWD"
FIRMWAREDIR="$CWD"
FIRMWAREBIN="$(pwd)/bin/firmware.bin"
FIRMWAREBIN="$(pwd)/bin/firmware.bin"
else
else
echo
echo
red_echo "Firmware not found!"
red_echo "Firmware not found!"
blue_echo "Please run \"po init DEVICE FOLDER\" to setup a project,
blue_echo "Please run \"po init DEVICE FOLDER\" to setup a project,
or choose a valid directory."
or choose a valid directory."
echo
echo
exit
exit
fi
fi
fi
fi
fi
fi
fi
fi
else
else
DEVICESFILE="$CWD/devices.txt"
DEVICESFILE="$CWD/devices.txt"
FIRMWAREDIR="$CWD/firmware"
FIRMWAREDIR="$CWD/firmware"
FIRMWAREBIN="$CWD/bin/firmware.bin"
FIRMWAREBIN="$CWD/bin/firmware.bin"
fi
fi


if [ -d "$FIRMWAREDIR" ];
if [ -d "$FIRMWAREDIR" ];
then
then
FIRMWAREDIR="$FIRMWAREDIR"
FIRMWAREDIR="$FIRMWAREDIR"
else
else
if [ "$DIRWARNING" == "true" ];
if [ "$DIRWARNING" == "true" ];
then
then
echo
echo
red_echo "Firmware directory not found!"
red_echo "Firmware directory not found!"
blue_echo "Please run \"po init DEVICE FOLDER\" to setup a project,
blue_echo "Please run \"po init DEVICE FOLDER\" to setup a project,
or choose a valid directory."
or choose a valid directory."
echo
echo
exit
exit
fi
fi
FINDDIRFAIL="true"
FINDDIRFAIL="true"
fi
fi


if [ -f "$DEVICESFILE" ];
if [ -f "$DEVICESFILE" ];
then
then
DEVICES="$(cat $DEVICESFILE)"
DEVICES="$(cat $DEVICESFILE)"
else
else
if [ "$DEVICEWARNING" == "true" ];
if [ "$DEVICEWARNING" == "true" ];
then
then
echo
echo
red_echo "devices.txt not found!"
red_echo "devices.txt not found!"
blue_echo "You need to create a \"devices.txt\" file in your project directory with the names
blue_echo "You need to create a \"devices.txt\" file in your project directory with the names
of your devices on each line."
of your devices on each line."
green_echo "Example:"
green_echo "Example:"
echo " product1
echo " product1
product2
product2
product3
product3
"
"
fi
fi
FINDDEVICESFAIL="true"
FINDDEVICESFAIL="true"
fi
fi


if [ -f "$FIRMWAREBIN" ];
if [ -f "$FIRMWAREBIN" ];
then
then
FIRMWAREBIN="$FIRMWAREBIN"
FIRMWAREBIN="$FIRMWAREBIN"
else
else
if [ "$BINWARNING" == "true" ];
if [ "$BINWARNING" == "true" ];
then
then
echo
echo
red_echo "Firmware Binary not found!"
red_echo "Firmware Binary not found!"
blue_echo "Perhaps you need to build your firmware?"
blue_echo "Perhaps you need to build your firmware?"
echo
echo
fi
fi
FINDBINFAIL="true"
FINDBINFAIL="true"
fi
fi
}
}


build_message()
build_message()
{
{
echo
echo
cd "$FIRMWAREDIR"/.. || exit
cd "$FIRMWAREDIR"/.. || exit
BINARYDIR="$(pwd)/bin"
BINARYDIR="$(pwd)/bin"
green_echo "Binary saved to $BINARYDIR/firmware.bin"
green_echo "Binary saved to $BINARYDIR/firmware.bin"
echo
echo
exit
exit
}
}


dfu_open()
dfu_open()
{
{
DFU_LIST="$(dfu-util -l)"
DFU_LIST="$(dfu-util -l)"


if echo "$DFU_LIST" | grep "2b04:d006" > /dev/null || echo "$DFU_LIST" | grep "2b04:d008" > /dev/null || echo "$DFU_LIST" | grep "2b04:d00a" > /dev/null || echo "$DFU_LIST" | grep "1d50:607f" > /dev/null || echo "$DFU_LIST" | grep "1d50:607f" > /dev/null ;
if echo "$DFU_LIST" | grep "2b04:d006" > /dev/null || echo "$DFU_LIST" | grep "2b04:d008" > /dev/null || echo "$DFU_LIST" | grep "2b04:d00a" > /dev/null || echo "$DFU_LIST" | grep "1d50:607f" > /dev/null || echo "$DFU_LIST" | grep "1d50:607f" > /dev/null ;
then
then
blue_echo "
blue_echo "
Already found a device in DFU mode!
Already found a device in DFU mode!
"
"
rm "$DFU_LIST"
rm "$DFU_LIST"
return
return
fi
fi


if [ "$1" == "-d" ] || [ "$1" == "--device" ];
if [ "$1" == "-d" ] || [ "$1" == "--device" ];
then
then


if stty -F "$2" > /dev/null 2>&1; # Check if serial port is avaliable
if stty -F "$2" > /dev/null 2>&1; # Check if serial port is avaliable
then
then
blue_echo "
blue_echo "
Placing device $2 into DFU mode...
Placing device $2 into DFU mode...
"
"
custom-baud "$2" "$DFUBAUDRATE" > /dev/null 2>&1
custom-baud "$2" "$DFUBAUDRATE" > /dev/null 2>&1
return
return


else
else
red_echo "
red_echo "
Could not find a device on $2
Could not find a device on $2
"
"
fi
fi


return
return
fi
fi


if [ "$DEVICE_TYPE" == "duo" ];
if [ "$DEVICE_TYPE" == "duo" ];
then
then
if [ "$MODEM_DUO" != "" ];
if [ "$MODEM_DUO" != "" ];
then
then
MODEM="$MODEM_DUO"
MODEM="$MODEM_DUO"
else
else
echo
echo
red_echo "Device not found!"
red_echo "Device not found!"
echo
echo
blue_echo "Your device must be connected by USB."
blue_echo "Your device must be connected by USB."
echo
echo
exit
exit
fi
fi
else
else
if [ "$MODEM" != "" ];
if [ "$MODEM" != "" ];
then
then
MODEM="$MODEM"
MODEM="$MODEM"
else
else
echo
echo
red_echo "Device not found!"
red_echo "Device not found!"
echo
echo
blue_echo "Your device must be connected by USB."
blue_echo "Your device must be connected by USB."
echo
echo
exit
exit
fi
fi
fi
fi


if [ "$MODEM" ];
if [ "$MODEM" ];
then
then
custom-baud "$MODEM" "$DFUBAUDRATE" > /dev/null 2>&1
custom-baud "$MODEM" "$DFUBAUDRATE" > /dev/null 2>&1
fi
fi
}
}


switch_branch()
{
if [ "$1" != "" ];
then
if [ "$(git rev-parse --abbrev-ref HEAD)" != "$1" ];
then
git checkout "$1" > /dev/null
fi
else
if [ "$(git rev-parse --abbrev-ref HEAD)" != "$BRANCH" ];
then
git checkout "$BRANCH" > /dev/null
fi
fi
}

common_commands() #List common commands
common_commands() #List common commands
{
{
echo
echo
blue_echo "Common commands include:
blue_echo "Common commands include:
build, flash, clean, ota, dfu, serial, init, config, setup, library"
build, flash, clean, ota, dfu, serial, init, config, setup, library"
echo
echo
}
}


build_firmware()
build_firmware()
{
{
print_logo "Building firmware for $DEVICE_TYPE..."
print_logo "Building firmware for $DEVICE_TYPE..."


if [ "$DEVICE_TYPE" == "duo" ];
if [ "$DEVICE_TYPE" == "duo" ];
then
then
make all -s -C "$FIRMWARE_DUO/firmware/main" APPDIR="$FIRMWAREDIR" TARGET_DIR="$FIRMWAREDIR/../bin" PLATFORM="$DEVICE_TYPE"
make all -s -C "$FIRMWARE_DUO/firmware/main" APPDIR="$FIRMWAREDIR" TARGET_DIR="$FIRMWAREDIR/../bin" PLATFORM="$DEVICE_TYPE"
else
else


if [ "$DEVICE_TYPE" == "pi" ];
if [ "$DEVICE_TYPE" == "pi" ];
then
then
if hash docker 2>/dev/null;
if hash docker 2>/dev/null;
then
then
if docker run --rm -i -v $FIRMWARE_PI/firmware:/firmware -v $FIRMWAREDIR:/input -v $FIRMWAREDIR/../bin:/output particle/buildpack-raspberrypi 2> echo;
if docker run --rm -i -v $FIRMWARE_PI/firmware:/firmware -v $FIRMWAREDIR:/input -v $FIRMWAREDIR/../bin:/output particle/buildpack-raspberrypi 2> echo;
then
then
echo
echo
blue_echo "Successfully built firmware for Raspberry Pi"
blue_echo "Successfully built firmware for Raspberry Pi"
else
else
echo
echo
red_echo "Build failed."
red_echo "Build failed."
echo
echo
exit 1
exit 1
fi
fi
else
else
red_echo "Docker not found. Please install docker to build firmware for Raspberry Pi"
red_echo "Docker not found. Please install docker to build firmware for Raspberry Pi"
echo
echo
exit
exit
fi
fi
else
else
make all -s -C "$FIRMWARE_PARTICLE/firmware/main" APPDIR="$FIRMWAREDIR" TARGET_DIR="$FIRMWAREDIR/../bin" PLATFORM="$DEVICE_TYPE"
make all -s -C "$FIRMWARE_PARTICLE/main" APPDIR="$FIRMWAREDIR" TARGET_DIR="$FIRMWAREDIR/../bin" PLATFORM="$DEVICE_TYPE"
fi
fi
fi
fi
}
}


ota() # device firmware
ota() # device firmware
{
{
find_objects "$2"
find_objects "$2"
DIRWARNING="true"
DIRWARNING="true"
BINWARNING="true"
BINWARNING="true"
if [ "$FINDDIRFAIL" == "true" ] || [ "$FINDBINFAIL" == "true" ];
if [ "$FINDDIRFAIL" == "true" ] || [ "$FINDBINFAIL" == "true" ];
then
then
exit
exit
fi
fi


if [ "$1" == "" ];
if [ "$1" == "" ];
then
then
echo
echo
red_echo "Please specify which device to flash ota."
red_echo "Please specify which device to flash ota."
echo
echo
exit
exit
fi
fi


if [ "$1" == "--multi" ] || [ "$1" == "-m" ] || [ "$1" == "-ota" ];
if [ "$1" == "--multi" ] || [ "$1" == "-m" ] || [ "$1" == "-ota" ];
then
then
DEVICEWARNING="true"
DEVICEWARNING="true"
if [ "$FINDDEVICESFAIL" == "true" ];
if [ "$FINDDEVICESFAIL" == "true" ];
then
then
cd "$CWD"
cd "$CWD"
echo "" > devices.txt
echo "" > devices.txt
red_echo "Please list your devices in devices.txt"
red_echo "Please list your devices in devices.txt"
sleep 3
sleep 3
exit
exit
fi
fi
for DEVICE in $DEVICES ; do
for DEVICE in $DEVICES ; do
echo
echo
blue_echo "Flashing to device $DEVICE..."
blue_echo "Flashing to device $DEVICE..."
particle flash "$DEVICE" "$FIRMWAREBIN" || ( red_echo "Your device must be online in order to flash firmware OTA." )
particle flash "$DEVICE" "$FIRMWAREBIN" || ( red_echo "Your device must be online in order to flash firmware OTA." )
done
done
echo
echo
exit
exit
fi
fi
echo
echo
blue_echo "Flashing to device $1..."
blue_echo "Flashing to device $1..."
particle flash "$1" "$FIRMWAREBIN" || ( red_echo "Try using \"particle flash\" if you are having issues." )
particle flash "$1" "$FIRMWAREBIN" || ( red_echo "Try using \"particle flash\" if you are having issues." )
echo
echo
exit
exit
}
}


config()
{
if [[ -d "$HOME/.po-util" ]]; then
echo "Exists!" > /dev/null
else
mkdir -p "$HOME/.po-util"
fi

SETTINGS=~/.po-util/config
BASE_DIR=~/.po-util/src
FIRMWARE_PARTICLE=$BASE_DIR/particle
FIRMWARE_DUO=$BASE_DIR/redbearduo
FIRMWARE_PI=$BASE_DIR/pi
BRANCH="release/stable"
BRANCH_DUO="duo"
BRANCH_PI="feature/raspberry-pi"
GCC_ARM_PATH=$BINDIR/gcc-arm-embedded/$GCC_ARM_VER/bin/
MODEM_DUO=$MODEM_DUO

echo BASE_DIR="$BASE_DIR" >> $SETTINGS
echo FIRMWARE_PARTICLE="$FIRMWARE_PARTICLE" >> $SETTINGS
echo FIRMWARE_DUO="$FIRMWARE_DUO" >> $SETTINGS
echo FIRMWARE_PI="$FIRMWARE_PI" >> $SETTINGS
echo "export PARTICLE_DEVELOP=1" >> $SETTINGS
echo BINDIR="$BINDIR" >> $SETTINGS
echo FIRMWARE_DUO="$FIRMWARE_DUO" >> $SETTINGS

# Particle
echo
blue_echo "Which branch of the Particle firmware would you like to use?
You can find the branches at https://github.com/spark/firmware/branches
If you are unsure, please enter \"release/stable\""
read -rp "Branch: " branch_variable
BRANCH="$branch_variable"
echo BRANCH="$BRANCH" >> $SETTINGS
echo

# RedBear DUO
blue_echo "Which branch of the RedBear DUO firmware would you like to use?
You can find the branches at https://github.com/redbear/Duo/branches
If you are unsure, please enter \"duo\""
read -rp "Branch: " branch_variable
BRANCH_DUO="$branch_variable"
echo BRANCH_DUO="$BRANCH_DUO" >> $SETTINGS

echo
blue_echo "Shoud po-util automatically add and remove headers when using
libraries?"
read -rp "(yes/no): " response
if [ "$response" == "yes" ] || [ "$response" == "y" ] || [ "$response" == "Y" ];
then
AUTO_HEADER="true"
else
AUTO_HEADER="false"
fi
echo AUTO_HEADER="$AUTO_HEADER" >> $SETTINGS
echo
}

getAddedLibs()
{
find_objects "$1"
if [ "$FINDDIRFAIL" == "true" ]; then
exit
fi

cd "$FIRMWAREDIR"
for i in $(ls -d */ 2>/dev/null); do echo ${i%%/}; done
Text moved to lines 631-698
}

getLibURL()
{
TOKEN="$(grep 'token' ~/.particle/particle.config.json | grep -oE '([0-Z])\w+' | grep -v 'token')"
DATA=$(curl -sLH "Authorization: Bearer $TOKEN" "https://api.particle.io/v1/libraries/$1" | json_pp)
LIBURL=$(echo "$DATA" | grep "url" | grep -oE '"((?:\\.|[^"\\])*)"' | grep "http" | tr -d '"')
}

getLib() # "$i" "$LIB_NAME"
{

if [ "$2" == "" ];
then
LIB_QUERY="$1"
else
LIB_QUERY="$2"
fi

if (ls -1 "$LIBRARY" | grep -Fx "$LIB_QUERY") &> /dev/null ;
then
echo
blue_echo "Library $LIB_QUERY is already installed..."
else

if grep -q "://" <<<"$1";
then
GIT_ARGS=($1)

if [ "${GIT_ARGS[1]}" == "" ];
then
git clone "${GIT_ARGS[0]}" || ( echo ; red_echo "Could not download Library. Please supply a valid URL to a git repository." )
else
git clone "${GIT_ARGS[0]}" "${GIT_ARGS[1]}" || ( echo ; red_echo "Could not download Library. Please supply a valid URL to a git repository." )
fi

else
echo
getLibURL "$LIB_QUERY"

if ( echo "$LIBURL" | grep "github" ) > /dev/null ;
then
green_echo "$LIB_QUERY is availiable on GitHub!"
read -rp "Would you prefer to download it this way? (yes/no): " answer

if [ "$answer" == "yes" ] || [ "$answer" == "y" ] || [ "$answer" == "Y" ];
then
echo
cd "$LIBRARY"
git clone "$LIBURL" "$LIB_QUERY"
echo
blue_echo "Downloaded $LIB_QUERY from GitHub."
return 0
fi

echo
fi

blue_echo "Attempting to download $LIB_QUERY using Particle Libraries 2.0..."
echo
cd "$LIBRARY/.."
particle library copy "$LIB_QUERY" || ( echo && particle library search "$LIB_QUERY" && echo && return 1 )
echo
fi
fi

}

addLib()
{

if (ls -1 "$LIBRARY" | grep "$LIB_NAME") &> /dev/null ; # Try to automatically get library if not found
then
echo "FOUND LIBRARY!" &> /dev/null
else
getLib "$LIB_NAME" || ( echo && exit)
fi

if [ -f "$FIRMWAREDIR/$LIB_NAME.cpp" ] || [ -f "$FIRMWAREDIR/$LIB_NAME.h" ] || [ -d "$FIRMWAREDIR/$LIB_NAME" ];
then
echo
red_echo "Library $LIB_NAME is already added to this project..."
else
echo
green_echo "Adding library $LIB_NAME to this project..."

# Include library as a folder full of symlinks -- This is the new feature

mkdir -p "$FIRMWAREDIR/$LIB_NAME"

if [ -d "$LIBRARY/$LIB_NAME/firmware" ];
then
ln -s $LIBRARY/$LIB_NAME/firmware/* "$FIRMWAREDIR/$LIB_NAME"
else
if [ -d "$LIBRARY/$LIB_NAME/src" ];
then
ln -s $LIBRARY/$LIB_NAME/src/* "$FIRMWAREDIR/$LIB_NAME"
else
ln -s $LIBRARY/$LIB_NAME/* "$FIRMWAREDIR/$LIB_NAME"
fi
fi
fi
}

Text moved to lines 549-593
addHeaders()
{
[ "$1" != "" ] && HEADER="$1" || HEADER="$LIB_NAME"
if [ "$AUTO_HEADER" == "true" ];
then
if (grep "#include \"$HEADER/$HEADER.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ;
then
echo "Already imported" &> /dev/null
else
echo "#include \"$HEADER/$HEADER.h\"" > "$FIRMWAREDIR/main.cpp.temp"
cat "$FIRMWAREDIR/main.cpp" >> "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi
fi
}

rmHeaders()
{
if [ "$AUTO_HEADER" == "true" ];
then
if (grep "#include \"$1/$1.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ;
then
grep -v "#include \"$1/$1.h\"" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi

if (grep "#include \"$1.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ; # Backwards support
then
grep -v "#include \"$1.h\"" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi

if (grep "#include <$1.h>" "$FIRMWAREDIR/main.cpp") &> /dev/null ; # Other support
then
grep -v "#include <$1.h>" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi

fi
}

initProject()
initProject()
{
{
if [[ "$FOLDER" == "/"* ]]; # Check for absolute or relative
if [[ "$FOLDER" == "/"* ]]; # Check for absolute or relative
then
then
FIRMWAREDIR="$FOLDER/firmware"
FIRMWAREDIR="$FOLDER/firmware"
else
else
FIRMWAREDIR="$CWD/$FOLDER/firmware"
FIRMWAREDIR="$CWD/$FOLDER/firmware"
fi
fi


if [ -d "$FIRMWAREDIR" ];
if [ -d "$FIRMWAREDIR" ];
then
then
echo
echo
green_echo "Directory is already Initialized!"
green_echo "Directory is already Initialized!"
echo
echo
exit
exit
fi
fi


mkdir -p "$FIRMWAREDIR"
mkdir -p "$FIRMWAREDIR"
echo "#include \"Particle.h\"
echo "#include \"Particle.h\"


void setup() // Put setup code here to run once
void setup() // Put setup code here to run once
{
{


}
}


void loop() // Put code here to loop forever
void loop() // Put code here to loop forever
{
{


}" > "$FIRMWAREDIR/main.cpp"
}" > "$FIRMWAREDIR/main.cpp"


cp "$HOME/.po-util/doc/po-util-README.md" "$FIRMWAREDIR/../README.md"
cp "$HOME/.po-util/doc/po-util-README.md" "$FIRMWAREDIR/../README.md"


if [ "$DEVICE_TYPE" != "" ];
if [ "$DEVICE_TYPE" != "" ];
then
then
echo "---
echo "---
cmd: po $DEVICE_TYPE build
cmd: po $DEVICE_TYPE build


targets:
targets:
Build:
Build:
args:
args:
- $DEVICE_TYPE
- $DEVICE_TYPE
- build
- build
cmd: po
cmd: po
keymap: ctrl-alt-1
keymap: ctrl-alt-1
name: Build
name: Build
Flash:
Flash:
args:
args:
- $DEVICE_TYPE
- $DEVICE_TYPE
- flash
- flash
cmd: po
cmd: po
keymap: ctrl-alt-2
keymap: ctrl-alt-2
name: Flash
name: Flash
Clean:
Clean:
args:
args:
- $DEVICE_TYPE
- $DEVICE_TYPE
- clean
- clean
cmd: po
cmd: po
keymap: ctrl-alt-3
keymap: ctrl-alt-3
name: Clean
name: Clean
DFU:
DFU:
args:
args:
- $DEVICE_TYPE
- $DEVICE_TYPE
- dfu
- dfu
cmd: po
cmd: po
keymap: ctrl-alt-4
keymap: ctrl-alt-4
name: DFU
name: DFU
OTA:
OTA:
args:
args:
- $DEVICE_TYPE
- $DEVICE_TYPE
- ota
- ota
- --multi
- --multi
cmd: po
cmd: po
keymap: ctrl-alt-5
keymap: ctrl-alt-5
name: DFU
name: DFU
" >> "$FIRMWAREDIR/../.atom-build.yml"
" >> "$FIRMWAREDIR/../.atom-build.yml"


mkdir -p "$FIRMWAREDIR/../ci"
mkdir -p "$FIRMWAREDIR/../ci"


echo "dist: trusty
echo "dist: trusty
sudo: required
sudo: required
language: generic
language: generic


script:
script:
- ci/travis.sh
- ci/travis.sh


cache:
cache:
directories:
directories:
- $HOME/bin" > "$FIRMWAREDIR/../.travis.yml"
- $HOME/bin" > "$FIRMWAREDIR/../.travis.yml"


echo "#!/bin/bash
echo "#!/bin/bash
bash <(curl -sL https://master.po-util.com/ci-install)
bash <(curl -sL https://master.po-util.com/ci-install)
po lib clean . -f &> /dev/null
po lib clean . -f &> /dev/null
yes \"no\" | po lib setup # change to \"yes\" to prefer libraries from GitHub
yes \"no\" | po lib setup # change to \"yes\" to prefer libraries from GitHub
po $DEVICE_TYPE build" > "$FIRMWAREDIR/../ci/travis.sh"
po $DEVICE_TYPE build" > "$FIRMWAREDIR/../ci/travis.sh"


chmod +x "$FIRMWAREDIR/../ci/travis.sh"
chmod +x "$FIRMWAREDIR/../ci/travis.sh"


fi
fi


echo "bin/*" > "$FIRMWAREDIR/../.gitignore"
echo "bin/*" > "$FIRMWAREDIR/../.gitignore"
cd "$FIRMWAREDIR/.."
cd "$FIRMWAREDIR/.."
git init &> /dev/null
git init &> /dev/null


echo
echo
green_echo "Directory initialized as a po-util project for $DEVICE_TYPE"
green_echo "Directory initialized as a po-util project for $DEVICE_TYPE"
echo
echo
exit
exit
}
}


# End of helper functions
# End of helper functions


if [ "$1" == "" ] || [ "$1" == "help" ]; # Print help
if [ "$1" == "" ] || [ "$1" == "help" ]; # Print help
then
then
print_logo
print_logo


echo "Copyright (GPL) 2017 Nathan D. Robinson
echo "Copyright (GPL) 2017 Nathan D. Robinson


Usage: po DEVICE_TYPE COMMAND DEVICE_NAME
Usage: po DEVICE_TYPE COMMAND DEVICE_NAME
po DFU_COMMAND
po DFU_COMMAND
po install [full_install_path]
po install [full_install_path]
po library LIBRARY_COMMAND
po library LIBRARY_COMMAND


Run \"man po\" for help.
Run \"man po\" for help.
"
"
exit
exit
fi
fi


if [ "$1" == "setup-atom" ];
if [ "$1" == "setup-atom" ];
then
then
echo
echo
blue_echo "Installing Atom packages to enhance po-util experience..."
blue_echo "Installing Atom packages to enhance po-util experience..."
echo
echo
apm install build minimap file-icons language-particle
apm install build minimap file-icons language-particle
echo
echo
exit
exit
fi
fi


CWD="$(pwd)" # Global Current Working Directory variable
# Configuration file is created at "~/.po-util/config"
# Configuration file is created at "~/.po-util/config"
SETTINGS=~/.po-util/config
SETTINGS=$CWD/config
BASE_DIR=~/.po-util/src # These
FIRMWARE_PARTICLE=$CWD/submodules/particle_firmware
FIRMWARE_PARTICLE=$BASE_DIR/particle
FIRMWARE_DUO=$BASE_DIR/redbearduo
FIRMWARE_PI=$BASE_DIR/pi
BRANCH="release/stable" # can
BRANCH_DUO="duo"
BRANCH_PI="feature/raspberry-pi"
BINDIR=~/.po-util/bin # be
DFUBAUDRATE=14400 # changed in the "~/.po" file.
DFUBAUDRATE=14400 # changed in the "~/.po" file.
CWD="$(pwd)" # Global Current Working Directory variable
MODEM="$(ls -1 /dev/* | grep "ttyACM" | tail -1)"
MODEM="$(ls -1 /dev/* | grep "ttyACM" | tail -1)"
MODEM_DUO="$(ls -1 /dev/* | grep "ttyACM" | tail -1)" #TODO: SORT THIS OUT FOR LINUX
GCC_ARM_VER=gcc-arm-none-eabi-4_9-2015q3 # Updated to 4.9
GCC_ARM_VER=gcc-arm-none-eabi-4_9-2015q3 # Updated to 4.9
GCC_ARM_PATH=$BINDIR/gcc-arm-embedded/$GCC_ARM_VER/bin/
GCC_ARM_PATH=$BINDIR/gcc-arm-embedded/$GCC_ARM_VER/bin/
CUSTOM_BAUD_PATH=$BINDIR/custom-baud
CUSTOM_BAUD_PATH=$BINDIR/custom-baud
PATH="$PATH:$GCC_ARM_PATH"
PATH="$PATH:$GCC_ARM_PATH"
PATH="$PATH:$CUSTOM_BAUD_PATH"
PATH="$PATH:$CUSTOM_BAUD_PATH"


if [ "$1" == "config" ];
if [ "$1" == "config" ];
then
then
if [ -f "$SETTINGS" ];
if [ -f "$SETTINGS" ];
then
then
rm "$SETTINGS"
rm "$SETTINGS"
fi
fi
config
config
exit
exit
fi
fi


# Check if we have a saved settings file. If not, create it.
if [ ! -f $SETTINGS ]
then
echo
blue_echo "Your \"$SETTINGS\" configuration file is missing. Let's create it!"
config
fi

# Import our overrides from the ~/.po file.
source "$SETTINGS"


if [ "$1" == "info" ];
if [ "$1" == "info" ];
then
then
echo "
echo "
$(tput bold)$(tput setaf 3)$(date)$(tput sgr0)
$(tput bold)$(tput setaf 3)$(date)$(tput sgr0)


$(tput bold)Configured Settings:$(tput sgr0)
$(tput bold)Configured Settings:$(tput sgr0)


$(tput bold)Firmware Branches:$(tput sgr0)
$(tput bold)Firmware Branches:$(tput sgr0)
$(tput bold)Particle: $(tput setaf 6)$BRANCH$(tput sgr0)
$(tput bold)Particle: $(tput setaf 6)$BRANCH$(tput sgr0)
$(tput bold)Duo: $(tput setaf 6)$BRANCH_DUO$(tput sgr0)
$(tput bold)Duo: $(tput setaf 6)$BRANCH_DUO$(tput sgr0)


$(tput bold)DFU Baud Rate: $(tput setaf 6)$DFUBAUDRATE$(tput sgr0)
$(tput bold)DFU Baud Rate: $(tput setaf 6)$DFUBAUDRATE$(tput sgr0)
$(tput bold)Automatic Headers: $(tput setaf 6)$AUTO_HEADER$(tput sgr0)
$(tput bold)Automatic Headers: $(tput setaf 6)$AUTO_HEADER$(tput sgr0)
"
"
exit
exit
fi
fi


Text moved from lines 560-604
addHeaders()
{
[ "$1" != "" ] && HEADER="$1" || HEADER="$LIB_NAME"
if [ "$AUTO_HEADER" == "true" ];
then
if (grep "#include \"$HEADER/$HEADER.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ;
then
echo "Already imported" &> /dev/null
else
echo "#include \"$HEADER/$HEADER.h\"" > "$FIRMWAREDIR/main.cpp.temp"
cat "$FIRMWAREDIR/main.cpp" >> "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi
fi
}

rmHeaders()
{
if [ "$AUTO_HEADER" == "true" ];
then
if (grep "#include \"$1/$1.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ;
then
grep -v "#include \"$1/$1.h\"" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi

if (grep "#include \"$1.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ; # Backwards support
then
grep -v "#include \"$1.h\"" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi

if (grep "#include <$1.h>" "$FIRMWAREDIR/main.cpp") &> /dev/null ; # Other support
then
grep -v "#include <$1.h>" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi

fi
}


addLib()
{

if (ls -1 "$LIBRARY" | grep "$LIB_NAME") &> /dev/null ; # Try to automatically get library if not found
then
echo "FOUND LIBRARY!" &> /dev/null
else
getLib "$LIB_NAME" || ( echo && exit)
fi

if [ -f "$FIRMWAREDIR/$LIB_NAME.cpp" ] || [ -f "$FIRMWAREDIR/$LIB_NAME.h" ] || [ -d "$FIRMWAREDIR/$LIB_NAME" ];
then
echo
red_echo "Library $LIB_NAME is already added to this project..."
else
echo
green_echo "Adding library $LIB_NAME to this project..."

# Include library as a folder full of symlinks -- This is the new feature

mkdir -p "$FIRMWAREDIR/$LIB_NAME"

if [ -d "$LIBRARY/$LIB_NAME/firmware" ];
then
ln -s $LIBRARY/$LIB_NAME/firmware/* "$FIRMWAREDIR/$LIB_NAME"
else
if [ -d "$LIBRARY/$LIB_NAME/src" ];
then
cd $FIRMWAREDIR/$LIB_NAME
ln -s ../../../../lib/$LIB_NAME/src/* .
else
cd $FIRMWAREDIR/$LIB_NAME
ln -s ../../../../lib/$LIB_NAME/* .
fi
fi
fi
Text moved from lines 456-523
}

getLibURL()
{
TOKEN="$(grep 'token' ~/.particle/particle.config.json | grep -oE '([0-Z])\w+' | grep -v 'token')"
DATA=$(curl -sLH "Authorization: Bearer $TOKEN" "https://api.particle.io/v1/libraries/$1" | json_pp)
LIBURL=$(echo "$DATA" | grep "url" | grep -oE '"((?:\\.|[^"\\])*)"' | grep "http" | tr -d '"')
}

getLib() # "$i" "$LIB_NAME"
{

if [ "$2" == "" ];
then
LIB_QUERY="$1"
else
LIB_QUERY="$2"
fi

if (ls -1 "$LIBRARY" | grep -Fx "$LIB_QUERY") &> /dev/null ;
then
echo
blue_echo "Library $LIB_QUERY is already installed..."
else

if grep -q "://" <<<"$1";
then
GIT_ARGS=($1)

if [ "${GIT_ARGS[1]}" == "" ];
then
git clone "${GIT_ARGS[0]}" || ( echo ; red_echo "Could not download Library. Please supply a valid URL to a git repository." )
else
git clone "${GIT_ARGS[0]}" "${GIT_ARGS[1]}" || ( echo ; red_echo "Could not download Library. Please supply a valid URL to a git repository." )
fi

else
echo
getLibURL "$LIB_QUERY"

if ( echo "$LIBURL" | grep "github" ) > /dev/null ;
then
green_echo "$LIB_QUERY is availiable on GitHub!"
read -rp "Would you prefer to download it this way? (yes/no): " answer

if [ "$answer" == "yes" ] || [ "$answer" == "y" ] || [ "$answer" == "Y" ];
then
echo
cd "$LIBRARY"
git clone "$LIBURL" "$LIB_QUERY"
echo
blue_echo "Downloaded $LIB_QUERY from GitHub."
return 0
fi

echo
fi

blue_echo "Attempting to download $LIB_QUERY using Particle Libraries 2.0..."
echo
cd "$LIBRARY/.."
particle library copy "$LIB_QUERY" || ( echo && particle library search "$LIB_QUERY" && echo && return 1 )
echo
fi
fi

}

#Import nvm if installed
#Import nvm if installed
export NVM_DIR="$HOME/.nvm"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm


if [ "$1" == "install" ]; # Install : "$2 specify alternate" : "$3 if 'basic' then just install arm toolchain for CI use"
if [ "$1" == "install" ]; # Install : "$2 specify alternate" : "$3 if 'basic' then just install arm toolchain for CI use"
then
then


if [ "$3" == "basic" ];
if [ "$3" == "basic" ];
then
then
BASIC_INSTALL="true"
BASIC_INSTALL="true"
fi
fi


if [ "$(uname -s)" == "Darwin" ]; #Force homebrew version on macOS.
if [ "$(uname -s)" == "Darwin" ]; #Force homebrew version on macOS.
then
then
# Install via Homebrew
# Install via Homebrew
echo
echo
blue_echo "You are on macOS. po-util will be installed via Homebrew"
blue_echo "You are on macOS. po-util will be installed via Homebrew"


if hash brew 2>/dev/null;
if hash brew 2>/dev/null;
then
then
echo
echo
blue_echo "Homebrew is installed."
blue_echo "Homebrew is installed."
else
else
echo
echo
blue_echo "Installing Brew..."
blue_echo "Installing Brew..."
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
fi


echo
echo
blue_echo "Installing po-util with \"brew\""
blue_echo "Installing po-util with \"brew\""


brew tap nrobinson2000/po
brew tap nrobinson2000/po
brew install po
brew install po
po install
po install
exit
exit


fi
fi


if hash curl 2>/dev/null;
if hash curl 2>/dev/null;
then
then
echo "CURL FOUND!" > /dev/null
echo "CURL FOUND!" > /dev/null
else
else
red_echo "
red_echo "
po-util requires curl for the installation and updating of various tools.
po-util requires curl for the installation and updating of various tools.
Please install \"curl\" with your package manager.
Please install \"curl\" with your package manager.
"
"
exit
exit
fi
fi


if [ -f po-util.sh ];
if [ -f po-util.sh ];
then
then
if [ "$CWD" != "$HOME" ];
if [ "$CWD" != "$HOME" ];
then
then
cp po-util.sh ~/po-util.sh #Replace ~/po-util.sh with one in current directory.
cp po-util.sh ~/po-util.sh #Replace ~/po-util.sh with one in current directory.
fi
fi
chmod +x ~/po-util.sh
chmod +x ~/po-util.sh
else
else
if [ -f ~/po-util.sh ];
if [ -f ~/po-util.sh ];
then
then
chmod +x ~/po-util.sh
chmod +x ~/po-util.sh
else
else
curl -fsSLo ~/po-util.sh https://raw.githubusercontent.com/nrobinson2000/po-util/master/po-util.sh
curl -fsSLo ~/po-util.sh https://raw.githubusercontent.com/nrobinson2000/po-util/master/po-util.sh
chmod +x ~/po-util.sh
chmod +x ~/po-util.sh
fi
fi
fi
fi


if [ -f /usr/local/bin/po ]
if [ -f /usr/local/bin/po ]
then
then
blue_echo "po already linked in /usr/local/bin.
blue_echo "po already linked in /usr/local/bin.
"
"
else
else
blue_echo "Creating \"po\" link in /usr/local/bin..."
blue_echo "Creating \"po\" link in /usr/local/bin..."
sudo ln -s ~/po-util.sh /usr/local/bin/po
sudo ln -s ~/po-util.sh /usr/local/bin/po
fi
fi


blue_echo "Installing bash completion for po..."
blue_echo "Installing bash completion for po..."
sudo curl -fsSLo /etc/bash_completion.d/po https://raw.githubusercontent.com/nrobinson2000/homebrew-po/master/completion/po
sudo curl -fsSLo /etc/bash_completion.d/po https://raw.githubusercontent.com/nrobinson2000/homebrew-po/master/completion/po


# create doc dir
# create doc dir
[ -d "$HOME/.po-util/doc" ] || mkdir -p "$HOME/.po-util/doc" # If BASE_DIR does not exist, create it
[ -d "$HOME/.po-util/doc" ] || mkdir -p "$HOME/.po-util/doc" # If BASE_DIR does not exist, create it


# Download po-util-README.md
# Download po-util-README.md
curl -fsSLo ~/.po-util/doc/po-util-README.md https://raw.githubusercontent.com/nrobinson2000/po-util/master/po-util-README.md
curl -fsSLo ~/.po-util/doc/po-util-README.md https://raw.githubusercontent.com/nrobinson2000/po-util/master/po-util-README.md


# Check to see if we need to override the install directory.
# Check to see if we need to override the install directory.
if [ "$2" ] && [ "$2" != $BASE_DIR ]
if [ "$2" ] && [ "$2" != $BASE_DIR ]
then
then
BASE_DIR="$2"
BASE_DIR="$2"
echo BASE_DIR="$BASE_DIR" > $SETTINGS
echo BASE_DIR="$BASE_DIR" > $SETTINGS
fi
fi


# create base dir
# create base dir
[ -d "$BASE_DIR" ] || mkdir -p "$BASE_DIR" # If BASE_DIR does not exist, create it
[ -d "$BASE_DIR" ] || mkdir -p "$BASE_DIR" # If BASE_DIR does not exist, create it


# create Particle dir
# create Particle dir
[ -d "$FIRMWARE_PARTICLE" ] || mkdir -p "$FIRMWARE_PARTICLE" # If FIRMWARE_PARTICLE does not exist, create it
[ -d "$FIRMWARE_PARTICLE" ] || mkdir -p "$FIRMWARE_PARTICLE" # If FIRMWARE_PARTICLE does not exist, create it
# create redbearduo dir
# create redbearduo dir
[ -d "$FIRMWARE_DUO" ] || mkdir -p "$FIRMWARE_DUO" # If FIRMWARE_DUO does not exist, create it
[ -d "$FIRMWARE_DUO" ] || mkdir -p "$FIRMWARE_DUO" # If FIRMWARE_DUO does not exist, create it
# create raspberry-pi dir
# create raspberry-pi dir
[ -d "$FIRMWARE_PI" ] || mkdir -p "$FIRMWARE_PI" # If FIRMWARE_DUO does not exist, create it
[ -d "$FIRMWARE_PI" ] || mkdir -p "$FIRMWARE_PI" # If FIRMWARE_DUO does not exist, create it


LIBRARY=~/.po-util/lib # Create library directory
LIBRARY=$CWD/lib # Create library directory
if [ -d "$LIBRARY" ]; # if it is not found.
if [ -d "$LIBRARY" ]; # if it is not found.
then
then
LIBRARY=~/.po-util/lib
LIBRARY=$CWD/lib
else
else
mkdir -p "$LIBRARY"
mkdir -p "$LIBRARY"
fi
fi


if [ -f "$LIBRARY/../project.properties" ]; # Structure library directory
if [ -f "$LIBRARY/../project.properties" ]; # Structure library directory
then
then
echo "Exists!" > /dev/null
echo "Exists!" > /dev/null
else
else
cd "$LIBRARY/.."
cd "$LIBRARY/.."
echo "name=particle-lib" > "project.properties"
echo "name=particle-lib" > "project.properties"
fi
fi


# clone Particle firmware repository
# clone Particle firmware repository
cd "$FIRMWARE_PARTICLE" || exit
cd "$FIRMWARE_PARTICLE" || exit


if hash git 2>/dev/null;
if hash git 2>/dev/null;
then
then
NOGIT="false"
NOGIT="false"
echo
echo
blue_echo "Installing Particle firmware from Github..."
blue_echo "Installing Particle firmware from Github..."
git clone https://github.com/spark/firmware.git
git clone https://github.com/spark/firmware.git
else
else
NOGIT="true"
NOGIT="true"
fi
fi


# clone RedBear DUO firmware repository
# clone RedBear DUO firmware repository
cd "$FIRMWARE_DUO" || exit
cd "$FIRMWARE_DUO" || exit


if hash git 2>/dev/null;
if hash git 2>/dev/null;
then
then
NOGIT="false"
NOGIT="false"
echo
echo
blue_echo "Installing RedBear Duo firmware from Github..."
blue_echo "Installing RedBear Duo firmware from Github..."
git clone https://github.com/redbear/firmware.git
git clone https://github.com/redbear/firmware.git
else
else
NOGIT="true"
NOGIT="true"
fi
fi


# clone RedBear DUO firmware repository
# clone RedBear DUO firmware repository
cd "$FIRMWARE_PI" || exit
cd "$FIRMWARE_PI" || exit


if hash git 2>/dev/null;
if hash git 2>/dev/null;
then
then
NOGIT="false"
NOGIT="false"
echo
echo
blue_echo "Installing Particle-Pi firmware from Github..."
blue_echo "Installing Particle-Pi firmware from Github..."
git clone https://github.com/spark/firmware.git
git clone https://github.com/spark/firmware.git
else
else
NOGIT="true"
NOGIT="true"
fi
fi


if hash apt-get 2>/dev/null; # Test if on a Debian-based system
if hash apt-get 2>/dev/null; # Test if on a Debian-based system
then
then
DISTRO="deb" # Debian
DISTRO="deb" # Debian
INSTALLER="apt-get install -y"
else
if hash yum 2>/dev/null;
then
DISTRO="rpm" # Fedora / Centos Linux
INSTALLER="yum -y install"
else
if hash pacman 2>/dev/null; # Arch Linux
then
DISTRO="arch"
INSTALLER="pacman -Syu"
fi
fi
fi

cd "$BASE_DIR" || exit

echo

# Install dependencies

if hash arm-none-eabi-gcc 2>/dev/null; #Test for arm-none-eabi-gcc
then
blue_echo "ARM toolchain version $GCC_ARM_VER is already installed... Continuing..."
else

blue_echo "Installing ARM toolchain and dependencies locally in $BINDIR/gcc-arm-embedded/..."
mkdir -p $BINDIR/gcc-arm-embedded && cd "$_" || exit

if [ -d "$GCC_ARM_VER" ]; #
then
echo
blue_echo "ARM toolchain version $GCC_ARM_VER is already downloaded... Continuing..."
else
curl -LO https://launchpad.net/gcc-arm-embedded/4.9/4.9-2015-q3-update/+download/gcc-arm-none-eabi-4_9-2015q3-20150921-linux.tar.bz2 #Update to v4.9
echo
blue_echo "Extracting ARM toolchain..."
tar xjf gcc-arm-none-eabi-*-linux.tar.bz2
fi
fi

if [ "$DISTRO" != "arch" ];
then

# Install Node.js
curl -Ss https://nodejs.org/dist/ > node-result.txt
grep "<a href=\"v" "node-result.txt" > node-new.txt
tail -1 node-new.txt > node-oneline.txt
sed -n 's/.*\"\(.*.\)\".*/\1/p' node-oneline.txt > node-version.txt
NODEVERSION="$(cat node-version.txt)"
NODEVERSION="${NODEVERSION%?}"
INSTALLVERSION="node-$NODEVERSION"
rm node-*.txt
if [ "$(node -v)" == "$NODEVERSION" ];
then
blue_echo "Node.js version $NODEVERSION is already installed."
else
# MESSAGE="Installing Node.js version $NODEVERSION..." ; blue_echo
curl -Ss https://api.github.com/repos/nodesource/distributions/contents/"$DISTRO" | grep "name" | grep "setup_"| grep -v "setup_iojs"| grep -v "setup_dev" > node-files.txt
tail -1 node-files.txt > node-oneline.txt
sed -n 's/.*\"\(.*.\)\".*/\1/p' node-oneline.txt > node-version.txt
# MESSAGE="Installing Node.js version $(cat node-version.txt)..." blue_echo
# curl -sL https://"$DISTRO".nodesource.com/"$(cat node-version.txt)" | sudo -E bash -
curl -sL https://"$DISTRO".nodesource.com/setup_6.x | sudo -E bash -
rm -rf node-*.txt
fi
fi

if [ "$DISTRO" == "deb" ];
then
sudo $INSTALLER git nodejs python-software-properties python g++ make build-essential pkg-config libusb-1.0-0-dev libarchive-zip-perl screen libc6-i386 autoconf automake
fi

if [ "$DISTRO" == "rpm" ];
then
sudo $INSTALLER git nodejs python make automake gcc gcc-c++ kernel-devel libusb glibc.i686 vim-common perl-Archive-Zip-1.58-1.fc24.noarch screen autoconf
fi

if [ "$DISTRO" == "arch" ];
then
sudo $INSTALLER g