#!/usr/bin/perl
#
# Extract the kernel version from the kernel version header file.  Takes the
# kernel source path as its only argument.  If the version header couldn't be
# found, print nothing and exit quietly.

use warnings;

my $ksrc = shift;
unless ($ksrc && (-f "$ksrc/include/linux/version.h" || -f "$ksrc/include/generated/uapi/linux/version.h")) {
    exit 0;
}
my $found = 0;
my $line;
if (open (VERSION, "$ksrc/include/linux/version.h")) {
    if (defined(VERSION) && ($line = <VERSION>)) {
        if ($line =~ /"(.+)"/) {
            print "$1\n";
            $found = 1;
        }
    }
}
exit 0 if $found;
if (open (VERSION, "$ksrc/include/generated/utsrelease.h")) {
    if (defined(VERSION) && ($line = <VERSION>)) {
        if ($line =~ /UTS_RELEASE *"(.+)"/) {
            print "$1\n";
            $found = 1;
        }
    }
}
exit 0 if $found;
# kernel.release is no longer useful since 3.1.0
unless (open (VERSION, "$ksrc/include/config/kernel.release")) {
    exit 0;
}
if (defined(VERSION) && ($line = <VERSION>)) {
    print "$line";
}
exit 0;
