##
##  h2.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 version
%define       V_vers 2.4.240
%define       V_date 2025-09-22

#   package information
Name:         h2
Summary:      H2 Database Engine
URL:          http://www.h2database.com/
Vendor:       H2 Group
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Database
License:      MPL/EPL
Version:      %{V_vers}
Release:      20250924

#   list of sources
Source0:      https://github.com/h2database/h2database/releases/download/version-%{V_vers}/h2-%{V_date}.zip
Source1:      h2.sh
Source2:      rc.h2

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101, java, JAVA-JDK

%description
    H2 is a Java SQL-based RDBMS. The main features of H2 are: very
    fast, open source, JDBC API; embedded and server modes, in-memory
    databases; browser based Console application; and small footprint.

%track
    prog h2 = {
        version   = %{V_date}
        url       = http://www.h2database.com/html/download.html
        regex     = h2-(\d+-\d+-\d+)\.zip
    }

%prep
    %setup -q -n h2

%build

%install
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/lib/h2 \
        $RPM_BUILD_ROOT%{l_prefix}/var/h2/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/h2/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/h2/db
    %{l_shtool} install -c -m 644 \
        bin/h2-*.jar $RPM_BUILD_ROOT%{l_prefix}/lib/h2/h2.jar
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE h2.sh} $RPM_BUILD_ROOT%{l_prefix}/bin/h2
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.h2} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/h2/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        su - %{l_rusr} -c \
            "LC_CTYPE=C; export LC_CTYPE; umask 077; \
            rm -rf $RPM_INSTALL_PREFIX/var/h2/db/*; \
            echo \"ALTER USER sa SET PASSWORD 'sa';\" | \
            $RPM_INSTALL_PREFIX/bin/h2 RunScript \
                -script /dev/stdin \
                -url 'jdbc:h2:$RPM_INSTALL_PREFIX/var/h2/db/default' \
                -user sa"
        if [ ! -f $RPM_INSTALL_PREFIX/var/h2/db/default.h2.db ]; then
            echo "ERROR: failed to create initial H2 default database" 1>&2
            exit 1
        fi
        ( echo "A H2 database named 'default' with the administrator user"
          echo "\"sa\" and password \"sa\" was created for you. In order to"
          echo "start a H2 server with this database and connect to it use:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc h2 start"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/h2 Shell \\ "
          echo "      -url \"jdbc:h2:tcp://localhost/default\" \\ "
          echo "      -user sa -password sa"
          echo "Then immediately set a new password with the SQL command:"
          echo "    ALTER USER sa SET PASSWORD '<new-password>';"
          echo "By default you can only connect from \"localhost\". For opening"
          echo "connections from outside, use the following string for the variable"
          echo "\"h2_server_flags\" in $RPM_INSTALL_PREFIX/etc/rc.d:"
          echo "    h2_server_flags=\"-tcp -pg -web \\ "
          echo "                     -tcpAllowOthers -pgAllowOthers -webAllowOthers\""
          echo "For connecting to the web-interface use the following URL:"
          echo "    http://localhost:8082/"
        ) | %{l_rpmtool} msg -b -t warn
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        %{l_rc} h2 stop 2>/dev/null
        rm -f $RPM_INSTALL_PREFIX/var/h2/db/*  >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/h2/run/* >/dev/null 2>&1 || true
    fi
    exit 0

