##
##  aica.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         aica
Summary:      Certification Authority and PKI Tools
URL:          http://www-ailab.elcom.nitech.ac.jp/security/
Vendor:       Akira Iwata Laboratory
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Security
License:      AiCrypto
Version:      0.81
Release:      20100227

#   list of sources
Source0:      http://www-ailab.elcom.nitech.ac.jp/security/AiCA-%{version}.tar.gz
Source1:      aica.cnf
Patch0:       aica.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Certification Authority Tools
    This product includes software developed by Akira Iwata Laboratory,
    Nagoya Institute of Technology in Japan (http://mars.elcom.nitech.ac.jp/)

%track
    prog aica = {
        version   = %{version}
        url       = http://www-ailab.elcom.nitech.ac.jp/security/download.html
        regex     = AiCA-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n AiCA
    %patch -p0

%build
    #   change path to templates and vendor certs
    %{l_shtool} subst \
        -e 's;strncpy(buf,PREFIX;strncpy(buf,"%{l_prefix}/lib/aica";' \
        aica/aica_conf.c

    #   change path to config file
    for f in */Makefile.in; do
        %{l_shtool} subst \
            -e 's;/lib/aica.cnf;/etc/aica/aica.cnf;g' \
            $f
    done

    #   change default path to certificates
    %{l_shtool} subst \
        -e 's;"%s/certs",PREFIX;"%s/certs","%{l_prefix}/lib/aica";' \
        aicrypto/ssl/ssl_vfy.c

    #   mangle dangerous filenames that contain whitespace
    for f in templates/*; do
        mv "$f" `echo "$f" | sed -e 's/ /_/g'`
    done

    #   patch source to reference mangled filenames
    %{l_shtool} subst \
        -e 's;"ARL Profile template";"ARL_Profile_template";g' \
        -e 's;"CRL Profile template";"CRL_Profile_template";g' \
        -e 's;"CRL-All Profile template";"CRL-All_Profile_template";g' \
        -e 's;"Sub-CA Profile template";"Sub-CA_Profile_template";g' \
        -e 's;"SMIME user Profile template";"SMIME_user_Profile_template";g' \
        -e 's;"Cross Cert Profile template";"Cross_Cert_Profile_template";g' \
        aica/aica_new.c \
        ca/ca_asn1.c

    #   configure the sources
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    ./configure \
        --prefix=%{l_prefix}

    #   build the program
    %{l_make} %{l_mflags -O}

%install

    #   manually install components
    %{l_shtool} mkdir -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/lib \
        $RPM_BUILD_ROOT%{l_prefix}/etc/aica \
        $RPM_BUILD_ROOT%{l_prefix}/include/aica \
        $RPM_BUILD_ROOT%{l_prefix}/lib/aica/certs \
        $RPM_BUILD_ROOT%{l_prefix}/lib/aica/templates \
        $RPM_BUILD_ROOT%{l_prefix}/var/aica \
        $RPM_BUILD_ROOT%{l_prefix}/share/aica
    %{l_shtool} install -c -s -m 755 \
        aica/aica bin/aistore bin/asn1view bin/certconv \
        bin/certreq bin/certvfy bin/certview \
        $RPM_BUILD_ROOT%{l_prefix}/bin/
    %{l_shtool} install -c -m 755 \
        bin/newca.sh $RPM_BUILD_ROOT%{l_prefix}/bin/newca
    %{l_shtool} install -c -m 644 \
        lib/lib*.a $RPM_BUILD_ROOT%{l_prefix}/lib/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE aica.cnf} $RPM_BUILD_ROOT%{l_prefix}/etc/aica/
    %{l_shtool} install -c -m 644 \
        aicrypto/include/* aicrypto/aiconfig.h \
        $RPM_BUILD_ROOT%{l_prefix}/include/aica/
    %{l_shtool} install -c -m 644 \
        certs/* $RPM_BUILD_ROOT%{l_prefix}/lib/aica/certs/
    %{l_shtool} install -c -m 644 \
        templates/* $RPM_BUILD_ROOT%{l_prefix}/lib/aica/templates/
    %{l_shtool} install -c -m 644 \
        COPYRIGHT README $RPM_BUILD_ROOT%{l_prefix}/share/aica/

    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/aica/aica.cnf'

%files -f files

%clean

