#!/bin/sh
# $Log: catChangeLog,v $
# Revision 1.10  2021-03-25 08:33:21+05:30  Cprogrammer
# use default changelog as doc/ChangeLog
#
# Revision 1.9  2021-03-25 08:28:06+05:30  Cprogrammer
# create changelog for rpm, debian and obs
#
# Revision 1.8  2021-02-28 21:58:46+05:30  Cprogrammer
# fixed catChangeLog
#
# Revision 1.7  2020-10-12 17:05:49+05:30  Cprogrammer
# fixed date lines in ChangeLog
#
# Revision 1.6  2020-09-13 20:44:29+05:30  Cprogrammer
# renamed ChangeLog-indimail to ChangeLog
#
# Revision 1.5  2020-06-05 23:36:37+05:30  Cprogrammer
# changed ChangeLog filename
#
# Revision 1.4  2020-05-30 16:43:18+05:30  Cprogrammer
# skip blank lines
#
# Revision 1.3  2020-05-24 12:02:18+05:30  Cprogrammer
# fixed release number
#
# Revision 1.2  2020-05-21 14:38:34+05:30  Cprogrammer
# fixed timestamp
#
# Revision 1.1  2019-05-29 00:47:06+05:30  Cprogrammer
# Initial revision
#
# utility to append changelog to indimail.spec
#
#
# $Id: catChangeLog,v 1.10 2021-03-25 08:33:21+05:30 Cprogrammer Exp mbhangui $
#
option=1
while test $# -gt 0; do
    case "$1" in
    -*=*)
	optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
	;;
    *)
	optarg=""
	;;
    esac

	valid=0
    case "$1" in
    --spec)
	option=1
	valid=1
    ;;
    --debian)
	option=2
	valid=1
    ;;
    --changes)
	option=3
	valid=1
    ;;
	--email=*)
	email=$optarg
	valid=1
	;;
	--version=*)
	version=$optarg
	valid=1
	;;
	--release=*)
	release=$optarg
	valid=1
	;;
	--name=*)
	name=$optarg
	valid=1;
	;;
	--state=*)
	state=$optarg
	valid=1;
	;;
	--urgency=*)
	urgency=$optarg
	valid=1;
	;;
    *)
	if [ -n "$optarg" ] ; then
    	echo "invalid option [$1]" 1>&2
		exit 1
	fi
    ;;
    esac
	if [ $valid -eq 0 ] ; then
		break
	fi
	shift
done
if [ $# -eq 0 ] ; then
	file=doc/ChangeLog
else
	file=$1
fi
if [ $option -eq 2 ] ; then
	if [ -z "$name" -o -z "$state" -o -z "$urgency" ] ; then
		echo "you have to give --name --state --urgency options" 1>&1
	fi
fi
if [ -z "$version" ] ; then
	if [ -f conf-version ] ; then
		version=$(cat conf-version)
	elif [ -f ../conf-version ] ; then
		version=$(cat ../conf-version)
	fi
fi
if [ -z "$release" ] ; then
	if [ -f conf-release ] ; then
		release=$(cat conf-release)
	elif [ -f ../conf-release ] ; then
		release=$(cat ../conf-release)
	fi
fi
if [ -z "$email" ] ; then
	if [ -f conf-email ] ; then
		email=$(cat conf-email)
	elif [ -f ../conf-email ] ; then
		email=$(cat ../conf-email)
	fi
fi

flag=0
nonewline=0
bullet=0
highlights=0
exec 0<$file
while read line
do
	echo "$line"|grep "^*" > /dev/null
    status=$?
	if [ $status -eq 0 -a $flag -ne 0 ] ; then
		break
	fi
    if [ $status -ne 0 -a $flag -eq 0 ] ; then
        continue
    fi
	echo "$line" | grep "^- [0-9]*/.*/.*" > /dev/null # skip dates
	if [ $? -eq 0 ] ; then
		continue
	fi
	if [ $flag -eq 0 ] ; then # first line
		flag=1
		if [ $option -eq 1 ] ; then
			echo "* `date -u +"%a %b %d %Y %H:%M:%S %z"` $email" "$version"-"$release"%{?dist}
		elif [ $option -eq 2 ] ; then
			echo "$name ($version-$release) $state; urgency=$urgency"
			echo
		elif [ $option -eq 3 ] ; then
			printf "%0*d\n" 67 |sed 's}0}-}g' 
			date "+%a, %b %d %H:%M:%S %Z %Y - $email"
			echo
		fi
		continue
	elif [ $flag -eq 1 ] ; then # second line
		if [ $option -eq 1 ] ; then
			echo "$line" | sed -e "s|@version\@|$version|g" -e "s|@release\@|$release|g"
		fi
		flag=2
		continue
	fi
	echo $line | grep -E "^o " > /dev/null
	if [ $? -eq 0 ] ; then
		bullet=1
	else
		bullet=0
	fi
	echo "$line" |grep -E "^[0-9]|^[0-9][0-9]|^[0-9][0-9][0-9]" > /dev/null
	if [ $? -eq 0 ] ; then
		if [ $option -eq 2 -a $nonewline -eq 1 ] ; then
			echo
		fi
		nonewline=0
		if [ $option -eq 1 -o $option -eq 3 ] ; then
			set $line
			shift
			echo "- $*"
		elif [ $option -eq 2 ] ; then
			set $line
			shift
			nonewline=1
			echo -n "  * $*"
		fi
	elif [ -n "$line" ] ; then
		if [ $highlights -eq 0 ] ; then
			nonewline=0
			echo "$line" | grep "Release Highlights" >/dev/null
			if [ $? -eq 0 ] ; then
				highlights=1
				if [ $option -eq 3 ] ; then
					echo $line
				fi
				continue
			fi
		fi
		if [ $highlights -eq 1 ] ; then
			echo "$line" | grep "^====" >/dev/null
			if [ $? -eq 0 ] ; then
				highlights=2
				if [ $option -ne 3 ] ; then
					continue
				fi
				if [ $nonewline -eq 1 ] ; then
					echo
					nonewline=0
				fi
				echo $line
				continue
			fi
		fi
		if [ $highlights -eq 1 ] ; then
			if [ $option -ne 3 ] ; then
				continue
			fi
			if [ $bullet -eq 1 ] ; then
				if [ $nonewline -eq 1 ] ; then
					echo ""
					nonewline=0
				fi
				echo "$line"
				bullet=0
			else
				if [ $nonewline -eq 1 ] ; then
					echo ""
					nonewline=0
				fi
				echo -n "  $line"
				nonewline=1
			fi
			continue
		fi
		if [ $option -eq 1 -o $option -eq 3 ] ; then
			echo "  $line"
		elif [ $option -eq 2 ] ; then
			echo -n " $line"
			nonewline=1
		fi
	fi
done
if [ $option -eq 2 ] ; then
	if [ $nonewline -eq 1 ] ; then
		echo
	fi
	echo
	echo " -- $email  `date -u +'%a, %d %b %Y %H:%M:%S %z'`"
elif [ $option -eq 3 ] ; then
	if [ $nonewline -eq 1 ] ; then
		echo
	fi
fi
