#!/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 [ "$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 \
    python3-pip

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

  # lint 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
    make install
  fi

else
  if [ -z ${ID_LIKE+x} ]; then
    echo "Error: unknown OS distribution"
  else
    echo "Error: unknown OS distribution '$ID_LIKE'"
  fi
  exit 1
fi

# lint dependencies
pip3 install pre-commit
