#!/bin/bash -e

#Updated on 2025-10-23

defUser=agentvi254346
defPass=d4phaCc4A4sd3Dds
defVersion_CM=2.1.1885
defVersion_Enterprise=2025.5.1-2211

defCoreAPIURL=
defPortalURL=
defAuthURL=

#defCoreAPIURL_CM=https://edge.innovi.io
#defPortalURL_CM=https://portal.innovi.io
#defAuthURL_CM=https://auth.innovi.io

defCoreAPIURL_CM=https://edge.irisity.io
defPortalURL_CM=https://portal.irisity.io
defAuthURL_CM=https://auth.irisity.io

defCoreAPIURL_Enterprise=https://edge.irisplus.ai
defPortalURL_Enterprise=https://portal.irisplus.ai
defAuthURL_Enterprise=https://auth.irisplus.ai


defEdgeModel=CUSTOM_IRIS_PLUS

if [[ $(lsb_release -rs) != "18.04" ]] && [[ $(lsb_release -rs) != "20.04" ]] && [[ $(lsb_release -rs) != "22.04" ]]; then
  echo "Edge CI can be installed only on Ubuntu versions: 18.04, 20.04, 22.04"
  exit 1
fi

if [ "$EUID" -ne 0 ]
  then echo "Please run with sudo"
  exit 1
fi

echo "
Please select an installation type:
1) Central Monitoring (CM)
2) Enterprise
"

read n
case $n in
  1) echo -e "You have selected CM installation \n";
     defVersion=$defVersion_CM;;
     # all other CM endpoints are DM defaults, so there is no need to define them
                  
  2) echo -e "You have selected Enterprise installation \n";
     defVersion=$defVersion_Enterprise
     defCoreAPIURL=$defCoreAPIURL_Enterprise
     defPortalURL=$defPortalURL_Enterprise
     defAuthURL=$defAuthURL_Enterprise;;
  
  *) echo "You have selected an invalid installation type"
     echo -e "Exit...\n"; exit;;
esac

export DOCKER_USERNAME=${1:-$defUser}
export DOCKER_PASSWORD=${2:-$defPass}
export DM_VERSION=${3:-$defVersion}
export DM_CORE_API_URL=${4:-$defCoreAPIURL}
export DM_PORTAL_URL=${5:-$defPortalURL}
export DM_AUTH_URL=${5:-$defAuthURL}

echo "
To change default settings, you can provide the following arguments in specified order:
1) Docker ID
2) Docker password
3) Version default $DM_VERSION
Example:
sudo ./custom_edge_installer_irisity.sh dockerid dockerpassword $DM_VERSION
"

sleep 3s

echo "Choose one primary network interface from list and press enter"
ip link | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2;getline}'
read primInterface

if cat /sys/class/net/$primInterface/operstate  | grep -q "up"; then
   echo "Network interface exist"
else
   echo "Network interface down or not exist"
   exit 1
fi
echo "Choose one second network interface  from list and press enter (or press enter to continue)"
ip link | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2;getline}'
read secInterface


#kernel upgrade
uname -a
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" --install-recommends linux-generic-hwe-$(lsb_release -rs)
uname -a

#utilities
apt-get install -y curl vim tcpdump ca-certificates htop ncdu iftop glances nfs-common iperf speedtest-cli

#install docker prerequisites
apt-get install -y gnupg lsb-release

mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


#if receiving a GPG error when running apt-get update
chmod a+r /etc/apt/keyrings/docker.gpg
apt-get update

# List the available versions:
#apt-cache madison docker-ce | awk '{ print $3 }'

VERSION_STRING=5:20.10.22~3-0~ubuntu-$(lsb_release -cs)
apt-get install -y --install-recommends docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-compose-plugin

#docker compose
curl -SL "https://github.com/docker/compose/releases/download/v2.14.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

sudo dpkg -l | grep -qw golang-docker-credential-helpers && sudo apt remove -y golang-docker-credential-helpers

#docker without sudo
sudo groupadd docker || true
sudo usermod -aG docker $USER || true
[[ ! -z "$SUDO_USER" ]] && sudo usermod -aG docker $SUDO_USER || true

systemctl start docker

docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}

mkdir -p /storage/recordings
mkdir -p /storage/logs

for c in $(docker ps -a --format="{{.Names}}")
do
       echo "save logs for container $c"
       docker logs $c > $c.log 2> $c.err &
done

echo "Clean containers and volumes"
docker stop $(docker ps -a -q) || true
docker rm $(docker ps -a -q) || true
docker volume prune -f || true

machine_id=$(sed "s/[^a-zA-Z0-9]//g" <<< $(openssl rand -base64 20))
echo "Edge model type $defEdgeModel -machine id = <$machine_id>"

addEnv=""
[[ -n $secInterface ]] && addEnv="    - NETWORK_S_INTERFACE=$secInterface"$'\n'
[[ -n $DM_CORE_API_URL ]] && addEnv+="    - CORE_API_URL=$DM_CORE_API_URL"$'\n'
[[ -n $DM_PORTAL_URL ]] && addEnv+="    - PORTAL_URL=$DM_PORTAL_URL"$'\n'
[[ -n $DM_AUTH_URL ]] && addEnv+="    - AUTH_URL=$DM_AUTH_URL"

#generate docker-compose file
cat > docker-compose.yml <<EOL
version: '3.3'

volumes:
  device-manager-data:

services:
  device-manager:
    image: agentvi/innovi-device-manager:${DM_VERSION}
    container_name: innovi-device-manager
    privileged: true
    labels:
    - "COMP_TYPE=innovi-device-manager"
    - "SETUP_ID=innovi-device-manager"
    - "CONFIGURATION_ID=CONFIGURATION_ID"
    - "VERSION_ID=VERSION_ID"
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock:rw
    - /run/systemd/resolve:/run/systemd/resolve:rw
    - /etc/netplan:/etc/netplan:rw
    - /proc/sysrq-trigger:/sysrq
    - /etc/systemd/timesyncd.conf:/etc/systemd/timesyncd.conf:rw
    - /etc/docker:/etc/docker:rw
    - /sys/class/net:/sys/class/net:rw
    - device-manager-data:/home/device-manager/data:rw
    environment:
    - EDGE_MODEL=$defEdgeModel
    - NETWORK_P_INTERFACE=$primInterface
    - MACHINE_ID=$machine_id
$addEnv
    restart: always
    network_mode: "host"
    logging:
      driver: json-file
      options:
        max-size: "50m"
        max-file: "20"
EOL

docker-compose up -d

echo "Done."

