#!/bin/sh
EDIT_WARNING="##### WARNING: please do not edit this auto generated spec file.#####\n##### Only modify apache2.spec file and run pre_checkin.sh! #####\n"

cp apache2.changes apache2-tls13.changes

sed "
    # Store the 'old' package name and add disclaimer to avoid editing manually the file
    s/^%define apache_name apache2.*/${EDIT_WARNING}\n\n%define apache_name apache2/;
    
    # Update package name
    s/^\(Name:.*\)$/\1-tls13/;

    # Provide both "apache" and "apache2" to ensure an smooth transition
    s/^Provides:[ ]*apache = %{version}/Provides:       apache = %{version}\nProvides:       apache2 = %{version}-%{release}\nConflicts:      apache2 < %{version}-%{release}/;

    # let's build against OpenSSL 1.1 for TLS 1.3
    s/^BuildRequires:[ ]*libopenssl\-devel/\# build against OpenSSL 1.1 so we enable tls1.3\nBuildRequires:  libopenssl-1_1-devel/;

    # 202: main package requires it's MPM subpackage counterpart
    s/^Requires:[ ]*%{name}-MPM-notls13/Requires:       %{name}-MPM-tls13/g;

    # 203: main package suggests it's MPM subpackage counterpart
    s/^Suggests:[ ]*apache2-%{default_mpm}/Suggests:       apache2-tls13-%{default_mpm}/;

    # 210: require the execution of post script for the current package
    s/^Requires(post):[ ]*%{name}-utils/Requires(post): %{name}-tls13-utils/;

    # 211: provide both standard and tls13 packages for mod_macro and its obsoletes
    s/^Provides:[ ]*%{name}-mod_macro = %{version}/Provides:       %{name}-tls13-mod_macro = %{version}\nProvides:       %{name}-mod_macro = %{version}/;
    s/^Obsoletes:[ ]*%{name}-mod_macro <= 1.2.1/Obsoletes:      %{name}-mod_macro <= 1.2.1\nObsoletes:      %{name}-tls13-mod_macro <= 1.2.1/;

    # make the package conflict with opposing utils, fix for bsc#1214454
    s/^Conflicts:[ ]*apache\-tls13\-utils/Conflicts:      apache-notls13-utils/;

    # make the subpackages conflict against their non-TLS 1.3 counterparts
    s/^Provides:[ ]*apache\-notls13/Provides:       apache-tls13/;
    s/^Conflicts:[ ]*apache\-tls13/Conflicts:      apache-notls13/;

    # replace all requires to the subpackages to the base apache2 package with tls13 version
    s/%{name}[ ]*=[ ]*%{version}/%{name}-tls13 = %{version}/g;

    # 247, 257, 268: provide both standard and tls13 packages
    s/^Provides:[ ]*%{name}-MPM-notls13/Conflicts:      %{name}-MPM-notls13/g;
    s/^Conflicts:[ ]*%{name}-MPM-tls13/Provides:       %{name}-MPM-tls13/g;

    # 253: subpackage *** worker *** tls13 version also provides standard worker package
    s/^%package[ ]*worker/%package worker\nProvides:       %{name}-worker\nProvides:       %{name}-worker = %{version}-%{release}\nConflicts:      %{name}-worker < %{version}-%{release}\nConflicts:      %{name}-MPM < %{version}-%{release}\nProvides:       %{name}-MPM = %{version}-%{release}/;
        
    # 256: subpackage *** prefork *** tls13 version also provides standard prefork package
    s/^%package[ ]*prefork/%package prefork\nProvides:       %{name}-prefork\nProvides:       %{name}-prefork = %{version}-%{release}\nConflicts:      %{name}-prefork < %{version}-%{release}\nConflicts:      %{name}-MPM < %{version}-%{release}\nProvides:       %{name}-MPM = %{version}-%{release}/;

    # 284: subpackage *** event *** tls13 version also provides standard event package
    s/^%package[ ]*event/%package event\nProvides:       %{name}-event\nProvides:       %{name}-event = %{version}-%{release}\nConflicts:      %{name}-event < %{version}-%{release}\nConflicts:      %{name}-MPM < %{version}-%{release}\nProvides:       %{name}-MPM = %{version}-%{release}/;

    # # 311: subpackage *** devel ***
    s/^%package[ ]*devel/%package devel\nProvides:       %{name}-devel\nProvides:       %{name}-devel = %{version}-%{release}\nConflicts:      %{name}-devel < %{version}-%{release}/;
    s/^Requires:[ ]*apache2-prefork/Requires:       apache2-tls13-prefork/g;
    
    # 327: subpackage *** doc ***
    s/^%package[ ]*doc/%package doc\nProvides:       %{name}-doc\nProvides:       %{name}-doc = %{version}-%{release}\nConflicts:      %{name}-doc < %{version}-%{release}/;

    # 342: subpackage example-pages
    s/^%package[ ]*example-pages/%package example-pages\nProvides:       %{name}-example-pages\nProvides:       %{name}-example-pages = %{version}-%{release}\nConflicts:      %{name}-example-pages < %{version}-%{release}/;

    # 352: subpackage *** utils ***
    s/^%package[ ]*utils/%package utils\nProvides:       %{name}-utils\nProvides:       %{name}-utils = %{version}-%{release}\nConflicts:      %{name}-utils < %{version}-%{release}/;  

    # everything else, is renamed from {name} -> {apache_name} to not to break
    # the build and to have the same files in the same places as the standard
    # package
    s/%{name}/%{apache_name}/g;

    # fix the sysconfig file for apache2-tls13
    s/%{fillup_only apache2}/%{fillup_only -n apache2}/;
    s/%{fillup_and_insserv apache2}/%{fillup_and_insserv -n apache2}/;
    
    # " < apache2.spec > apache2-tls13.spec
