#! /bin/sh
#
#  checkit:    Test for correct versions of utilities
#  (see the end of the file for bypassing these checks)
#
#  Copyright 2005-2007 John Coffman
#  Copyright 2009-2010 Joachim Wiedorn
#  All rights reserved.
#
#  Licensed under the terms contained in the file 'COPYING'
#  in the source directory.
#


ret=0
rc=0

# min. version of BCC, AD86, LD86
bccmin="0.16.14"

# min. version of GCC, CPP
gccmin="3.3.5"

vers_min() {
	local M m p N n r

	rc=0
# get our version major, minor, rev
	M=`echo $1 | cut -d . -f 1`
	m=`echo $1 | cut -d . -f 2`
	p=`echo $1 | cut -d . -f 3`
	if [ -z "$p" ]; then p=0; fi
#echo vers_min1 $M $m $p
	N=`echo $2 | cut -d . -f 1`
	n=`echo $2 | cut -d . -f 2`
	r=`echo $2 | cut -d . -f 3`
#echo vers_min2 $N $n $r
	if [ "$M" -lt "$N" ]; then rc=1
	elif [ "$M" -gt "$N" ]; then rc=0
	elif [ "$m" -lt "$n" ]; then rc=1
	elif [ "$m" -gt "$n" ]; then rc=0
	elif [ -z "$r" ]; then rc=0
	elif [ "$p" -lt "$r" ]; then rc=1
	fi
#echo vers_min returns $rc
	return $rc
}

#echo
echo GCC version $gccmin or later is required
gcc -v 1>foo1 2>foo2
V=`cat foo1 foo2 | cut -d' ' -f1-3 | grep -i version | tr '-' ' ' | cut -d' ' -f 3`
rm -f foo1 foo2
if [ -z "$V" ]; then
	echo gcc is not present
	ret=1
else
	vers_min $V $gccmin
	echo gcc version $V
	if [ $rc = 0 ]; then echo OKAY; else echo ERROR; ret=1; fi
fi

echo
echo AS86 version $bccmin or later is required
as86 -v 1>foo1 2>foo2
A=`cat foo1 foo2 | grep version | cut -d " " -f 3`
rm -f foo1 foo2
if [ -z "$A" ]; then
	echo as86 is not present
	ret=1
else
	vers_min $A $bccmin
	echo as86 version $A
	if [ $rc = 0 ]; then echo OKAY; else echo ERROR; ret=1; fi
fi

echo
echo LD86 version $bccmin or later is required
ld86 -v 1>foo1 2>foo2
L=`cat foo1 foo2 | grep version | cut -d " " -f 3`
rm -f foo1 foo2
if [ -z "$L" ]; then
	echo ld86 is not present
	ret=1
else
	vers_min $L $bccmin
	echo ld86 version $L
	if [ $rc = 0 ]; then echo OKAY; else echo ERROR; ret=1; fi
fi

echo
echo BCC version $bccmin or later is recommended
bcc -v 1>foo1 2>foo2
B=`cat foo1 foo2 | grep version | cut -d " " -f 3`
rm -f foo1 foo2
if [ -z "$B" ]; then
	echo bcc is not present
	echo You will not be able to make floppy2, diag2.img, or lilo.com
else
	vers_min $B $bccmin
	echo bcc version $B
	if [ $rc = 0 ]; then echo OKAY; else echo ERROR; fi
fi
echo

#
# Uncomment the line below to bypass all the checks
#
#exit 0
exit $ret
