<%- if @kernel == 'Linux' -%>
#!/bin/bash
<%- else -%>
#!/bin/sh
<%- end -%>
#
# A wrapper for Xtrabackup

ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>
DIR=<%= @backupdir %>

# Ensure backup directory exist.
mkdir -p $DIR

<%- if @kernel == 'Linux' -%>
set -o pipefail
<%- end -%>

<% if @prescript -%>
  <%- [@prescript].flatten.compact.each do |script| %>
<%= script %>
  <%- end -%>
<% end -%>


cleanup()
{
  <%- if @kernel == 'SunOS' -%>
    gfind "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | gxargs -0 -r rm -f
  <%- else -%>
    find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
  <%- end -%>
}

<% if @delete_before_dump -%>
cleanup
<% end -%>


<%- _innobackupex_args = '' -%>

<%- if @backupuser and @backuppassword -%>
  <%- _innobackupex_args = '--user="' + @backupuser + '" --password="' + @backuppassword + '"' -%>
<%- end -%>

<%- if @backupdatabases and @backupdatabases.is_a?(Array) and !@backupdatabases.empty? -%>
  <%- _innobackupex_args = _innobackupex_args + ' --databases="' + @backupdatabases.join(' ') + '"' -%>
<%- end -%>

<%- if @optional_args and @optional_args.is_a?(Array) -%>
  <%- @optional_args.each do |arg| -%>
    <%- _innobackupex_args = _innobackupex_args + ' ' + arg -%>
  <%- end -%>
<%- end -%>

<%= @backupmethod -%> <%= _innobackupex_args %> $@


<% unless @delete_before_dump -%>
if [ $? -eq 0 ] ; then
    cleanup
    touch /tmp/mysqlbackup_success
fi
<% end -%>


<% if @postscript -%>
  <%- [@postscript].flatten.compact.each do |script| %>
<%= script %>
  <%- end -%>
<% end -%>
