#! /usr/bin/env bash
# Copyright (c) 2016 - 2019 DisplayLink (UK) Ltd.

set -e
set -u

files=(
  checkpatch.pl
  const_structs.checkpatch
  spelling.txt
)

KVER="v$(grep --only-matching --perl-regexp 'KVER=\K[\d\.]*' .travis.yml | tail -n 1)"

for file in ${files[@]}; do
  wget -N -q "https://raw.githubusercontent.com/torvalds/linux/${KVER}/scripts/${file}"
done

chmod +x checkpatch.pl

set +e

result=0
./checkpatch.pl --terse --no-tree --ignore NEW_TYPEDEFS -f ./library/*
result=$((result || $?))

./checkpatch.pl --terse --no-tree --ignore LINUX_VERSION_CODE,PREFER_PR_LEVEL -f ./module/*
result=$((result || $?))

rm ${files[@]}

exit ${result}
