#!/bin/bash

if [ "$EUID" -ne 0 ]
  then echo "Please run as root!"
  exit
fi

echo 'Prepare system'

if [ -n "`which apt-get`" ]; then
  apt-get -y install \
    build-essential \
    patchutils\
    libproc-processtable-perl \
    linux-headers-$(uname -r) \
    git

  systemctl disable apt-daily.service
  systemctl disable apt-daily.timer
elif [ -n "`which yum`" ]; then
  yum -y group install "Development Tools"
  yum -y install \
    perl-CPAN \
    "kernel-devel-uname-r == $(uname -r)" \
    kernel-headers \
    wget \
    perl-core \
    elfutils-libelf-devel

  (echo y; echo o conf prerequisites_policy follow; echo o conf commit) | cpan
  cpan Proc::ProcessTable
  cpan Digest::SHA
fi

cd /tmp
wget https://github.com/DigitalDevices/dddvb/archive/0.9.38.tar.gz
tar xvzf /tmp/0.9.38.tar.gz
cd /tmp/dddvb-0.9.38

sed -i -e 's/^#if defined(CONFIG_DVB_MAX_ADAPTERS).*$/#if 0/g' include/linux/media/dvbdev.h
sed -i -e 's/DVB_MAX_ADAPTERS 64/DVB_MAX_ADAPTERS 256/g' include/linux/media/dvbdev.h
#sed -i -e 's/^\(#define MAX_DVB_MINORS*\).*/\1 512/g' dvb-core/dvbdev.c

make -j 5
make install

mkdir -p /etc/depmod.d
echo 'search extra updates built-in' >/etc/depmod.d/extra.conf
depmod -a

modprobe ddbridge
modinfo ddbridge | grep 'version:'

echo 'Done! Please reboot the server'
