grep -qxF "apt update" /etc/rc.local
if [ $? -ne 0 ]; then
  echo "adding apt update to rc.local"
  sed -i "2s/^/apt update\n/" /etc/rc.local
fi

install_package() {
    echo "Installing package '$1' via pip3"
    pip3 install "$1"
    if [ $? -eq 0 ]; then
       echo "Successfully installed pip3 package '$1'"
    else
       echo "Could not install pip3 package '$1'"
       exit 1
    fi
}

#install_package "smbus"
#install_package "requests"
#install_package "flask"
#install_package "python-logstash-async"
#install_package "RPi.GPIO"
#install_package "spidev"
#install_package "influxdb_client"

pip3 install smbus requests flask python-logstash-async RPi.GPIO spidev influxdb_client filelock

echo "Setting up opencv4"
pkg-config --modversion opencv4

echo "Setting up nginx rtmp server"
sudo /etc/init.d/nginx start

{
  echo 'load_module "modules/ngx_rtmp_module.so";'
  echo 'worker_processes auto;'
  echo 'rtmp_auto_push on;'
  echo 'events {}'
  echo 'rtmp {'
  echo '    server {'
  echo '        listen 1935;'
  echo '        listen [::]:1935 ipv6only=on;'
  echo '        application live {'
  echo '            live on;'
  echo '            record off;'
  echo '        }'
  echo '    }'
  echo '}'
} >| /etc/nginx/nginx.conf

echo "Starting pigpiod daemon"

sudo systemctl start pigpiod
sudo systemctl enable pigpiod

base64 -d << UPD
CiBfX19fX18gICAgIF9fX19fXyAgICAgX18gICAgX18gICAgIF9fX19fXyAgIF9fICAgICAgICAgX18gICAgIF9fX19fXyAgICAgICAgICAgICAgICAgIAovXCAgX19fXCAgIC9cICBfXyBcICAgL1wgIi0uLyAgXCAgIC9cICA9PSBcIC9cIFwgICAgICAgL1wgXCAgIC9cICA9PSBcICAgICAgICAgICAgICAgICAKXCBcIFxfX19fICBcIFwgXC9cIFwgIFwgXCBcLS4vXCBcICBcIFwgIF8tLyBcIFwgXF9fX18gIFwgXCBcICBcIFwgIF9fPCAgICAgICAgICAgICAgICAgCiBcIFxfX19fX1wgIFwgXF9fX19fXCAgXCBcX1wgXCBcX1wgIFwgXF9cICAgIFwgXF9fX19fXCAgXCBcX1wgIFwgXF9fX19fXCAgICAgICAgICAgICAgIAogIFwvX19fX18vICAgXC9fX19fXy8gICBcL18vICBcL18vICAgXC9fLyAgICAgXC9fX19fXy8gICBcL18vICAgXC9fX19fXy8gICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiBfXyAgX18gICAgIF9fX19fXyAgICAgIF9fX19fXyAgIF9fX19fXyAgICAgICAgX19fX18gICAgIF9fX19fXyAgICAgX19fX19fICAgX19fX19fICAgIAovXCBcL1wgXCAgIC9cICA9PSBcICAgIC9cX18gIF9cIC9cICBfXyBcICAgICAgL1wgIF9fLS4gIC9cICBfXyBcICAgL1xfXyAgX1wgL1wgIF9fX1wgICAKXCBcIFxfXCBcICBcIFwgIF8tLyAgICBcL18vXCBcLyBcIFwgXC9cIFwgICAgIFwgXCBcL1wgXCBcIFwgIF9fIFwgIFwvXy9cIFwvIFwgXCAgX19cICAgCiBcIFxfX19fX1wgIFwgXF9cICAgICAgICAgXCBcX1wgIFwgXF9fX19fXCAgICAgXCBcX19fXy0gIFwgXF9cIFxfXCAgICBcIFxfXCAgXCBcX19fX19cIAogIFwvX19fX18vICAgXC9fLyAgICAgICAgICBcL18vICAgXC9fX19fXy8gICAgICBcL19fX18vICAgXC9fL1wvXy8gICAgIFwvXy8gICBcL19fX19fLyA=
UPD
echo ""
echo ""

service complib restart
systemctl enable complib
