<?xml version="1.0"?>
<macros>
    <xml name="inputs_outputs">
        <inputs>
            <param format="tabular" help="" label="Input tabular dataset" name="in_file" type="data" />
            <yield />
        </inputs>
        <outputs>
            <data format="tabular" name="out_file" label="${tool.name} on ${on_string}" />
        </outputs>
    </xml>

    <xml name="requirements">
        <requirements>
            <requirement type="package" version="@PG_VERSION@">postgresql</requirement>
            <requirement type="package" version="0.1">pglite</requirement>
        </requirements>
    </xml>

    <token name="@PG_VERSION@">11.2</token>

    <token name="@HELP_FOOTER@">
    The postgresql tools, are using a postgresql database in user-space. Databases are packed into a tarball and stored in the user-history.
    </token>

    <token name="@UNTAR_INFILE@"><![CDATA[
        tar -xjvf '$infile' &&
        export PWD=`pwd` &&
        sed -i.bak "s|unix_socket_directories.*|unix_socket_directories = '\$PWD/postgresql/'|" ./postgresql/db/postgresql.conf
    ]]>
    </token>

    <token name="@PG_SETUP@"><![CDATA[
        pglite setup -d ./postgresql &&
        @PG_START@
    ]]>
    </token>

    <token name="@PSQL@"><![CDATA[
        psql "\$(pglite url -d ./postgresql)"
    ]]>
    </token>

    <token name="@STATUS_RUNNING@"><![CDATA[
        timeout 60 bash -c 'until pglite status -d ./postgresql | grep -F -q "server is running"; do sleep 1; done' &&
        pglite status -d ./postgresql &&
        timeout 60 bash -c 'until ls -la ./postgresql | grep -F -q ".s.PGSQL.5432"; do sleep 1; done'
        && pglite status -d ./postgresql 
    ]]>
    </token>

    <token name="@STOPPED@"><![CDATA[
        timeout 60 bash -c 'until pglite status -d ./postgresql | grep -F -q "no server running"; do sleep 1; done'
    ]]>
    </token>

    <token name="@PG_START@"><![CDATA[
        pglite start  -d ./postgresql &&
        @STATUS_RUNNING@
    ]]>
    </token>

    <token name="@PG_STOP@"><![CDATA[
        pglite stop -d ./postgresql &&
        @STOPPED@
    ]]>
    </token>

    <token name="@ARCHIVE_DATABASE@"><![CDATA[
        tar -cvjf postgresql_out.tar.bz2 postgresql
    ]]>
    </token>
    <xml name="citations">
        <citations></citations>
    </xml>
</macros>
