#!/bin/sh

# makeversion a script which creates the version modules.

# Copyright (C) 2000-2019 Free Software Foundation, Inc.

#This file is part of GCC.

#GCC is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 3, or (at your option)
#any later version.

#GCC is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with GCC; see the file COPYING3.  If not see
#<http://www.gnu.org/licenses/>.


doM2 () {
/bin/rm -f M2Version.mod

cat << EOF >> M2Version.mod
IMPLEMENTATION MODULE M2Version ;

FROM DynamicStrings IMPORT InitString, Mark, Slice ;


(* Generated by makeversion - do not edit *)

PROCEDURE GetGM2Version () : String ;
BEGIN
   RETURN InitString('${GM2VERSION}')
END GetGM2Version ;


PROCEDURE GetGM2Date () : String ;
BEGIN
   RETURN InitString("`date '+%Y%m%d'`")
END GetGM2Date ;


PROCEDURE GetGCCVersion () : String ;
BEGIN
   RETURN InitString('${GCCVERSION}')
END GetGCCVersion ;


PROCEDURE GetYear () : String ;
BEGIN
   RETURN Slice(Mark(InitString(__DATE__)), -4, 0)
END GetYear ;


END M2Version.
EOF
}


doC () {

cat <<EOF > gm2version.c
/* Generated by makeversion - do not edit */
#include "config.h"
#include "system.h"

void gm2_version (int need_to_exit);

void gm2_version (int need_to_exit)
{
   printf("GNU Modula-2  ${GM2VERSION}  (`date '+%Y%m%d'`)\n");
   printf("  grafted onto GCC ${GCCVERSION}\n");
   printf("Copyright (C) `date '+%Y'` Free Software Foundation, Inc.\n");
   printf("License GPLv2: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n") ;
   printf("This is free software: you are free to change and redistribute it.\n") ;
   printf("There is NO WARRANTY, to the extent permitted by law.\n") ;
   if (need_to_exit)
     exit (0);
}
EOF
}


doCP () {

cat <<EOF > gm2version.c
/* Generated by makeversion - do not edit */
#include "config.h"
#include "system.h"

void gm2_version (int need_to_exit);

void gm2_version (int need_to_exit)
{
   printf("GNU Modula-2  ${GM2VERSION}  (`date '+%Y%m%d'`)\n");
   printf("  grafted onto GCC ${GCCVERSION}\n");
   printf("Copyright (C) `date '+%Y'` Free Software Foundation, Inc.\n");
   printf("License GPLv2: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n") ;
   printf("This is free software: you are free to change and redistribute it.\n") ;
   printf("There is NO WARRANTY, to the extent permitted by law.\n") ;
   if (need_to_exit)
     exit (0);
}
EOF
}


doTexi () {

cat <<EOF > version.texi

@c Generated by makeversion - do not edit

@set version-GM2     ${GM2VERSION}
@set version-GCC     ${GCCVERSION}
@set version-update  ${TEXIUPDATE}

EOF
}


progname=$0

usage () {
   cat <<EOF
Usage: $progname [-m][-c][-t]
   -m  generates a Modula-2 module M2Version.mod
   -c  generates a C file gm2version.c
   -p  generates a C++ file gm2version.c
   -t  generates a texi file version.texi
EOF
   exit 0
}

M2=no
C=no
CP=no
TEXI=no

while getopts ":mchpt" opt; do
   case $opt in

   m )  M2=yes ;;
   c )  C=yes ;;
   p )  CP=yes ;;
   t )  TEXI=yes ;;
   h )  usage ;;
   \\?) usage ;;
   esac
done

shift $(($OPTIND - 1))

SRCDIR=$1
GM2VERSION=`cat ${SRCDIR}/m2/version.c | sed -e '/version_string/!d' -e 's/[^0-9.]*\([0-9.]*\).*/\1/g'`
GCCVERSION=`cat ${SRCDIR}/BASE-VER`

pos=`grep -n texi ${SRCDIR}/m2/ChangeLog | head -1 | cut -f1 -d':'`
THEDATE=`head -n ${pos} ${SRCDIR}/m2/ChangeLog | grep ^2 | head -1 | cut -f1 -d' ' | cut -f1`

if date --date=${THEDATE} +'%A %e %B %Y' > /dev/null 2>&1 ; then
    #
    #  GNU date
    #
    TEXIUPDATE=`date --date=${THEDATE} +'%A %e %B %Y'`
elif date -j -f "%Y-%m-%d" ${THEDATE} +"%d %b %Y" > /dev/null 2>&1 ; then
    #
    #  assuming FreeBSD date
    #
    TEXIUPDATE=`date -j -f "%Y-%m-%d" ${THEDATE} +"%d %b %Y"`
else
    #
    #  no idea - so we just use the date in ChangeLog format
    #
    TEXIUPDATE=${THEDATE}
fi

if [ "${M2}" = "yes" ] ; then
   doM2
elif [ "${C}" = "yes" ] ; then
   doC
elif [ "${CP}" = "yes" ] ; then
   doCP
elif [ "${TEXI}" = "yes" ] ; then
   doTexi
else
   usage
fi

#
#  now we test the consistency of the release number in
#  $(srcdir)/m2/gm2-libs/config-host.in
#

if [ "`grep AC_INIT ${SRCDIR}/m2/gm2-libs/config-host.in | grep ${GM2VERSION}`" = "" ] ; then
   echo "makeversion has found a consistency error:  the ${SRCDIR}/m2/gm2-libs/config-host.in does not match the GNU Modula-2 release number"
   exit 1
fi
