#!/bin/bash
# shellcheck disable=SC1091
###############################################################################
# Copyright (C) Intel Corporation
#
# SPDX-License-Identifier: MIT
###############################################################################
# Install dependencies.
#
# This script should not call other scripts in the repository so that it can
# potentialy be an argument to the COPY command in a Dockerfile

set -o errexit

if [ -f "/etc/os-release" ]; then
  . /etc/os-release
fi
if [ -z "$ID_LIKE" ]; then
  ID_LIKE="$ID"
fi
if [ "$ID_LIKE" == "debian" ]; then
  apt-get update
  # basic build tools
  apt-get install -y --no-install-recommends \
    build-essential \
    cmake \
    pkg-config
  # tool build dependencies
  apt-get install -y --no-install-recommends \
    libdrm-dev \
    libva-dev \
    libx11-dev \
    libx11-xcb-dev \
    libxcb-present-dev \
    libxcb-dri3-dev \
    libwayland-dev \
    wayland-protocols

  # lint prerequisites
  apt-get install -y --no-install-recommends \
    git \
    pipx
  pipx ensurepath --force
  pipx install pre-commit
elif [ "$ID_LIKE" == "fedora" ]; then
  # Add media driver repositories
  dnf install -y 'dnf-command(config-manager)'
  dnf config-manager --add-repo \
      https://repositories.intel.com/gpu/rhel/${VERSION_ID}/lts/2350/unified/intel-gpu-${VERSION_ID}.repo
  dnf clean all && dnf makecache && dnf upgrade -y
  # basic build tools
  dnf install -y cmake gcc-c++
  # prerequsites to build examples
  dnf install -y libva-devel
  dnf install -y libX11-devel
  dnf install -y wayland-protocols-devel
  dnf install -y python3-pip
  # libdrm from source
  dnf install -y python3-pip git
  pip3 install meson ninja
  pushd /tmp
  if [ ! "$(pkg-config --modversion pciaccess)" = "0.17" ]; then
    git clone -b libpciaccess-0.17 https://gitlab.freedesktop.org/xorg/lib/libpciaccess.git
    pushd libpciaccess
    meson builddir --prefix=/usr
    ninja -C builddir install
    popd
    rm -rf /libpciaccess
  fi
  if [ ! "$(pkg-config --modversion libdrm)" = "2.4.113" ]; then
    git clone -b libdrm-2.4.113 https://gitlab.freedesktop.org/mesa/drm.git
    pushd drm
    meson builddir --prefix=/usr
    ninja -C builddir install
    popd
    rm -rf drm
  fi
  popd
  # lint dependencies
  pip3 install pre-commit
elif [ "$ID_LIKE" == "rhel fedora" ]; then
  # basic build tools
  yum install -y centos-release-scl
  yum-config-manager --enable rhel-server-rhscl-7-rpms
  yum install -y devtoolset-9

  # cmake
  if [ ! "$(cmake --version |head -1|cut -f3 -d' ')" = "3.22.1" ]; then
    yum install -y openssl-devel
    cd /tmp
    rm -rf cmake-3.*
    curl -O -L --retry 5 \
         https://github.com/Kitware/CMake/releases/download/v3.22.1/cmake-3.22.1.tar.gz
    tar zxvf cmake-3.*
    cd cmake-3.*
    source /opt/rh/devtoolset-9/enable
    ./bootstrap --prefix=/usr/local --parallel="$(nproc)"
    make -j"$(nproc)"
    make install
  fi

  # xcb and wayland
  yum -y update
  yum -y install wayland-devel libX11-devel libXext-devel libXfixes-devel libpciaccess-devel
  if [ ! "$(pkg-config --modversion wayland-protocols)" = "1.15" ]; then
    cd /tmp
    rm -rf wayland-protocols-1.15
    curl -O -L --retry 5 \
         https://wayland.freedesktop.org/releases/wayland-protocols-1.15.tar.xz
    tar -xJf wayland-protocols-1.15.tar.xz
    cd wayland-protocols-1.15
    ./configure --prefix="/usr" --bindir="/usr/bin" --libdir="/usr/lib64"
    make install
  fi

  # libva
  yum install -y \
    bzip2 \
    libdrm-devel
  if [ ! "$(pkg-config --variable=libva_version libva)" = "2.10.0" ]; then
    cd /tmp
    rm -rf libva-2.10.0
    curl -O -L --retry 5 \
         https://github.com/intel/libva/releases/download/2.10.0/libva-2.10.0.tar.bz2
    tar xjf libva-2.10.0.tar.bz2
    source /opt/rh/devtoolset-9/enable
    cd libva-2.10.0
    ./configure --prefix="/usr" --enable-wayland --enable-x11 --bindir="/usr/bin" --libdir="/usr/lib64"
    make
    make install
  fi

  # pre-commit prerequisites
  yum install -y git
  yum install -y \
      openssl-devel \
      bzip2-devel \
      libffi-devel \
      sqlite-devel
  if [ ! "$(python3 --version |head -1|cut -f2 -d' ')" == "3.8.9" ]; then
    cd /tmp
    rm -rf Python-3.*
    curl -O -L --retry 5 \
         https://www.python.org/ftp/python/3.8.9/Python-3.8.9.tgz
    tar xvf Python-3.8.9.tgz
    source /opt/rh/devtoolset-9/enable
    cd Python-3.*/
    ./configure --enable-optimizations --enable-loadable-sqlite-extensions \
                --prefix=/usr --with-openssl=/etc/pki/tls --with-openssl-rpath=auto
    ./configure --enable-optimizations --enable-loadable-sqlite-extensions
    make install
  fi
  # lint dependencies
  pip3 install pre-commit
else
  if [ -z ${ID_LIKE+x} ]; then
    echo "Error: unknown OS distribution"
  else
    echo "Error: unknown OS distribution '$ID_LIKE'"
  fi
  exit 1
fi
