#!/bin/bash

CLIENT_PATH="3dprinteros-client"
DEV_URL="https://acorn-update.3dprinteros.com"
LIVE_URL="https://cli-cloud-update.3dprinteros.com"

if [ -f /usr/local/bin/3dprinteros-client ]; then
    echo "Alredy installed skip. To launch type: 3dprinteros-client"
    exit 0
fi

echo "Installing 3DPrinterOS-Client..."

if [ ! -f /usr/local/bin/brew ]; then
    echo "Installing Brew..."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    echo "... Brew install complete"
else
    echo "Brew installed. skip..."
fi

echo "Installing 3DPrinterOS-Client dependencies..."

brew list python@3.9 &>/dev/null || brew install python@3.9
brew list libusb &>/dev/null || brew install libusb

pip3.9 install aiohttp opencv-python pyusb numpy requests pyserial rumps

echo "... dependencies installed"

URL=$(curl -s -H "Content-Type: application/json" -X POST -d '{"platform": "mac", "client_version": "0.0.0", "branch": "beta", "build": "git"}' "$DEV_URL")

echo $URL
mkdir $CLIENT_PATH
cd $CLIENT_PATH
curl -LO $URL
unzip $(basename $URL)
rm -f $(basename $URL)
cd ..
sudo mv -f $CLIENT_PATH /usr/local/
echo -e '#!/bin/bash\n\ncd /usr/local/3dprinteros-client/\npython3.9 launcher.py\n' > /usr/local/bin/3dprinteros-client
chmod +x /usr/local/bin/3dprinteros-client

echo "Install complete. To launch type: 3dprinteros-client"

