#!/bin/bash

# IMPORTANT SECURITY ADVICE: never execute scripts without first checking what they do!!
# you're doing good by looking at this :-)

# This scripts serves the function of installing the Picockpit-Client. It checks for the installed release of Raspberry Pi OS, older installs of picockpit-client and acts accordingly.

GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

PI_NAME="$$$PINAME$$$"
OTK="$$$OTK$$$"

# We test whether the user passes a name + OTK, in that case those are used to register the Pi.
if [ $# -eq 2 ]; then
                echo -e "${GREEN}Provided name and key via arguments.${NC}"
    echo -e "${GREEN}Name: '$1' and key: '$2'${NC}"
                PI_NAME=$1
                OTK=$2
fi

# Adding our key to your keyring.
curl -L http://repository.picockpit.com/picockpit.public.key | sudo gpg --dearmour | sudo tee /usr/share/keyrings/picockpit.gpg 2>&1 > /dev/null

os_release=$(lsb_release -cs)

case $os_release in
   "buster") repo='deb [arch=arm64,armhf signed-by=/usr/share/keyrings/picockpit.gpg] http://repository.picockpit.com/debian/ buster main';;
   "bullseye") repo='deb [arch=arm64,armhf signed-by=/usr/share/keyrings/picockpit.gpg] http://repository.picockpit.com/debian/ bullseye main';;
   "bookworm") repo='deb [arch=arm64,armhf signed-by=/usr/share/keyrings/picockpit.gpg] http://repository.picockpit.com/debian/ bookworm main';;
   *) echo -e "${RED}There is a problem with reading the installed version via lsb_release. This installation script is intended for Raspberry Pi OS based either on Buster, Bullseye or Bookworm. We recommend using Bookworm.${NC}";;
esac

# Since we are currently only using one repository, we just overwrite the current source (if it exists) and add the new one.
echo "$repo" | sudo tee /etc/apt/sources.list.d/picockpit.list 2>&1 > /dev/null

# After making sure the repository exists we simply update and install with apt-get.
echo -e "${GREEN}Updating the repositories so we can continue with the install.${NC}"
sudo apt-get update -y
echo -e "${GREEN}Installing Picockpit-Client from our Repo. This step might take a minute.${NC}"
sudo apt-get install picockpit-client -y --allow-change-held-packages



if dpkg -l "picockpit-client"; then
  echo -e "${GREEN}picockpit-client was installed succesfully.${NC}"
else
  echo -e "${RED}Something during the installation went wrong. Please reach out to us at support@picockpit.com if you suspect an issue on our side!${NC}"
  exit 1
fi

# As a last step we call the connect function of the picockpit-client.
if [ ${#OTK} == 41 ]; then
        sudo picockpit-client connect-otk --otk "$OTK" --name "$PI_NAME"
else
        echo -e "${GREEN}At this stage in the installation we ask you for an API-Key. If you aren't sure where to find it please look at https://picockpit.com/raspberry-pi/installation-of-picockpit/${NC}"
        sudo picockpit-client connect
fi

sudo systemctl enable picockpit-client.service
sudo systemctl restart picockpit-client.service

# copyright 2024

# IN NO EVENT WILL pi3g GmbH & Co. KG BE LIABLE FOR ANY INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES,
# INCLUDING WITHOUT LIMITATION DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER INTANGIBLE LOSSES,
# ARISING UNDER OR RELATING TO THIS AGREEMENT OR FROM THE USE OF, OR INABILITY TO USE, THE SERVICE.

# When in doubt do not use this software and service.