#!/bin/sh
# Dump the pouet.net DDL into a SQL text file and remove any runtime related
# information. The goal is to only keep the SQL structure as a clean DDL file.
# This works on OS X provided that you have a working "pouet" ssh alias that
# connects to pouet.net
# Support for other OSes can be added at will.

# Needed by sed to not throw out some warnings
LANG=C

# Make sure we are in the good folder
if [ ! -f bootstrap.inc.php ]
then
    echo "Launch me in the root folder"
    exit 1
fi

if [ "$USER" = "pouet" ]
then
  echo "Dumping the current DDL locally..."
  mysqldump --skip-lock-tables \
            --no-data \
            --result-file=pouet.sql \
            pouet
else
  echo "Dumping the current DDL..."
  ssh pouet mysqldump --skip-lock-tables \
                      --no-data \
                      --result-file=pouet.sql \
                      pouet
  
  echo "Downloading the DDL..."
  scp pouet:pouet.sql .
fi

echo "Cleaning up the DDL..."
# Remove any AUTO_INCREMENT option
sed -e 's/ AUTO_INCREMENT=[0-9]*//' -i pouet.sql

# Delete the last 2 lines (empty line + date of dump)
sed -e 'N;$!P;$!D;$d' -i pouet.sql

# Remove the MySQL client version header
sed -e '/-- MySQL dump .*  Distrib .*, for .*/d' -i pouet.sql

# Remove the MySQL server version header
sed -e '/-- Server version.*/d' -i pouet.sql

# Remove the host/database information
sed -e '/-- Host:.*Database:.*/d' -i pouet.sql

echo "Dump of the DDL completed in pouet.sql"
