#!/bin/sh

set -e

cd /root

dsmver=$(get_key_value /etc/VERSION majorversion).$(get_key_value /etc/VERSION minorversion)

if [ "$dsmver" != 7.1 ]; then
  echo 1 > /proc/sys/kernel/sysrq
  echo c > /proc/sysrq-trigger
fi

wget -O SA6400-7.1-i915.tgz https://share.inyes.net/Download/sa6400-7.1-i915/SA6400-7.1-i915.tgz
tar -zxvf SA6400-7.1-i915.tgz

sed -i 's/insmod i915.ko/insmod i915.ko enable_guc=2/' /root/SA6400-7.1-i915/i915.sh

if [ -n "$I915_PARAM" ]; then
  sed -i "s/enable_guc=2/$I915_PARAM/" /root/SA6400-7.1-i915/i915.sh
fi

cat << EOF > /usr/lib/systemd/system/i915.service
[Unit]
Description=Load modules for Intel iGPU
After=multi-user.target
IgnoreOnIsolate=true

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/sh -c "cd /root/SA6400-7.1-i915 && sh i915.sh"

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable i915
systemctl start i915

echo success