#!/bin/sh
#
# @(#)makeinc	1.10 20/09/04 Copyright 1998-2020 J. Schilling
###########################################################################
# Copyright 1998-2020 by J. Schilling
###########################################################################
#
# Automake script for the Schily (SING) makefile system
#
###########################################################################
#
#	Creates the following files:
#
#	./RULES/os-sunos.id		included first after mk-$(XMAKEPROG).id
#	./RULES/os-sunos.def		included next after RULES/os-sunos.id
#	./DEFAULTS/Defaults.sunos	included after RULES/os-sunos.def
#					Now incs/Dcc.$(PARCH)$(-O_ARCH) is
#					included in parparation of the next
#					file. This may call conf/cc-config.sh
#	./RULES/sun4c-sunos-gcc.rul	included after DEFAULTS/Defaults.sunos
#
###########################################################################
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# See the file CDDL.Schily.txt in this distribution for details.
# A copy of the CDDL is also available via the Internet at
# http://www.opensource.org/licenses/cddl1.txt
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file CDDL.Schily.txt from this distribution.
###########################################################################
MAILADDR=schilytools@mlists.in-berlin.de
UPSTREAMURL=https://codeberg.org/schilytools/schilytools

###########################################################################
# Option processing
###########################################################################
case $1 in

-r)
	shift
	SRCROOT=$1
	shift
	;;
esac

if [ $# -lt 1 ]; then
	echo 'Usage: makeinc [-r srcroot] filename'	1>&2
	exit 1
fi

###########################################################################
# Find SRCROOT if not specified via option
###########################################################################
if [ .$SRCROOT = . ]; then
	SRCROOT=.
	loop=1
	while [ $loop -lt 100 ]; do
		if [ ! -d $SRCROOT ]; then
			# Abort on ENAMETOOLONG
			break
		fi
		if [ -r $SRCROOT/RULES/rules.top ]; then
			break
		fi
		if [ "$SRCROOT" = . ]; then
			SRCROOT=".."
		else
			SRCROOT="$SRCROOT/.."
		fi
		loop="`expr $loop + 1`"
	done
	if [ ! -r $SRCROOT/RULES/rules.top ]; then
		echo "Cannot find SRCROOT directory" 1>&2
		exit 1
	fi
fi

###########################################################################
# Real start of automake script
###########################################################################
echo 'NOTICE:		Automake script called'
if [ -r $1 ]; then
	echo "WARNING:	requested file '$1' already exists"
	exit
fi
echo "IMPORTANT:	Please report your architecture to $MAILADDR or open a ticket at $UPSTREAMURL"
echo
echo "Trying to create missing rule file '$1'"
sleep 1
echo

#
# Compiler for platforms we  do not yet know.
# $CC is only used with *Defaults.*)
#
CC=cc
gcc -v 2> /dev/null && CC=gcc
ARG_CC=$CC
echo "CC:		$CC"

#
# Call $CC and try to find out whether it might be "gcc" or "clang".
#
CC_V=`(eval "$CC -v > /dev/null") 2>&1`
GCC_V=`echo "$CC_V" | grep -i gcc-version `
CLANG_V=`echo "$CC_V" | grep -i clang `

if [ ".$GCC_V" != . ]; then
	if (eval "gcc -v 2> /dev/null") 2>/dev/null; then
		CC="gcc"
	fi
elif [ ".$CLANG_V" != . ]; then
	if (eval "clang -v 2> /dev/null") 2>/dev/null; then
		CC="clang"
	fi
fi
#
# Check whether "cc" or "gcc" are emulated by another compiler
#
if [ "$CC" != "$ARG_CC" ]; then
	echo "$ARG_CC is $CC"
fi

MANSTYLE=sysv
if [ -r /usr/man/*man7*/man.* -o -r /usr/share/man/*man7*/man.* \
	-o -r /usr/man/*man7*/mandoc.* -o -r /usr/share/man/*man7*/mandoc.* \
	-o -r /usr/man/*man7*/ascii* -o -r /usr/share/man/*man7*/ascii* ]; then 
	MANSTYLE=bsd
fi
echo "MANSTYLE:	$MANSTYLE"

	case $1 in

	*os-*.id)
		echo $1
		OSNAME=`echo $1 | sed -e 's;\(.*\)os-\(.*\)\.id;\2;'`
		echo '###########################################################################' > $1
		echo "# OS specific MACRO definitions for $OSNAME"	>> $1
		echo "# This file is auto generated and may be wrong"	>> $1
		echo '###########################################################################' >> $1
		echo "O_ARCH=		$OSNAME"	>> $1 
		echo '-O_ARCH=	-$(O_ARCH)'		>> $1 
		;;
	
	*os-*.def)
		echo $1
		OSNAME=`echo $1 | sed -e 's;\(.*\)os-\(.*\)\.def;\2;'`
		echo '###########################################################################' > $1
		echo "# Global os definitions for $OSNAME"		>> $1
		echo "# This file is auto generated and may be wrong"	>> $1
		echo '###########################################################################' >> $1
		echo "MANSTYLE=	$MANSTYLE"		>> $1 
		;;
	
	*Defaults.*)
		echo $1
		OSNAME=`echo $1 | sed -e 's;\(.*\)\.\(.*\);\2;'`
		echo '###########################################################################' > $1
		echo "# global definitions for $OSNAME Systems"		>> $1
		echo "# This file is auto generated and may be wrong"	>> $1
		echo '###########################################################################' >> $1
		if [ "$CC" = clang ]; then
			cat $SRCROOT/TEMPLATES/Defaults.clang >> $1
		elif [ "$CC" = gcc ]; then
			cat $SRCROOT/TEMPLATES/Defaults.gcc >> $1
		else
			cat $SRCROOT/TEMPLATES/Defaults.xcc >> $1
		fi
		echo							>> $1
		echo '###########################################################################' >> $1
		echo "# Do compilattion with minimal warnings"		>> $1
		echo '###########################################################################' >> $1
		echo "CWARNOPTS="					>> $1
		;;
	
	*-*-cc*.rul | \
	*-*-gcc*.rul | \
	*-*-clang*.rul)
		ARCH=`echo $1 | sed -e 's;\(.*\)/\(.*\)-\(.*\)-\(.*\).rul;\2;'`
		OSNAME=`echo $1 | sed -e 's;\(.*\)/\(.*\)-\(.*\)-\(.*\).rul;\3;'`
		CCOM=`echo $1 | sed -e 's;\(.*\)/\(.*\)-\(.*\)-\(.*\).rul;\4;'`
		DIR=`echo $1 | sed -e "s;\(.*\)/$ARCH-$OSNAME-$CCOM.rul;\1;"`
		echo "ARCH:		$ARCH"
		echo "OSNAME:		$OSNAME"
		echo "CCOM:		$CCOM"
		echo "DIR:		$DIR"
		echo $1
		echo "Trying to find similar architecture for '$1'"
		similar=`ls $DIR/*-$OSNAME-$CCOM.rul 2>/dev/null| head -1`
		SARCH=`echo $similar | sed -e "s;\(.*\)/\(.*\)-$OSNAME-$CCOM.rul;\2;"`

		if [ .$similar != . ]; then
			echo 'Found similar architecture             ' "'$similar'"
			echo 'creating symlink:' ln -s $SARCH-$OSNAME-$CCOM.rul $1
			echo "Please send a mail to $MAILADDR or open a ticket at $UPSTREAMURL"
			echo "to report whether this works or not."
			sleep 10
			ln -s $SARCH-$OSNAME-$CCOM.rul $1 || cp $SARCH-$OSNAME-$CCOM.rul $1
			exit
		else
			echo '###########################################################################' > $1
			echo "# global definitions for $OSNAME Systems"		>> $1
			echo "# This file is auto generated and may be wrong"	>> $1
			echo '###########################################################################' >> $1

			if [ $CCOM = clang -o $CCOM = clang32 -o $CCOM = clang64 ]; then
				echo 'Using generic CLANG machine configuration file' 
				cat $SRCROOT/TEMPLATES/temp-$CCOM.rul >> $1
			elif [ $CCOM = gcc -o $CCOM = gcc32 -o $CCOM = gcc64 ]; then
				echo 'Using generic GCC machine configuration file' 
				cat $SRCROOT/TEMPLATES/temp-$CCOM.rul >> $1
			else
				echo 'Using generic dumb cc machine configuration file' 
				cat $SRCROOT/TEMPLATES/temp-xcc.rul >> $1
			fi
		fi
		;;

	#######################################################################
	# This entry is not really needed...
	#######################################################################
	*TARGETS/Targetdirs*)
		echo "Linking $1 to $SRCROOT/TARGETS/Targetdirs"
		ln -s Targetdirs $1
		;;
	*)
		echo "Unknown rule to build: '$1'."
		exit 1
		;;

	esac

echo "Please send a mail to $MAILADDR or open a ticket at $UPSTREAMURL"
echo "to report whether this works or not."
sleep 10
