#!/bin/sh -ex

#IMAGE_DEST=../public
IMAGE_DEST=public
#IMAGE_DEST=/local/screenshots
DB_HOST=localhost
#DB_HOST=torf

echo Getting screenshot files
rsync -av debshots@roy.workaround.org:debshots/public/screenshots/ public/screenshots/

echo Dropping existing database
set +e
psql -h $DB_HOST postgres debshots -c 'drop database debshots'
psql -h $DB_HOST postgres debshots -c 'drop database debshots_dev'
psql -h $DB_HOST postgres debshots -c 'drop database debshots_test'
set -e

echo Creating new database
psql -h $DB_HOST postgres debshots -c 'create database debshots owner debshots'
psql -h $DB_HOST postgres debshots -c 'create database debshots_dev owner debshots'
psql -h $DB_HOST postgres debshots -c 'create database debshots_test owner debshots'

echo Getting PostgreSQL dump
ssh root@roy.workaround.org 'pg_dump debshots' > /tmp/debshots.sql

echo Restoring PostgreSQL dump
psql -h $DB_HOST debshots debshots < /tmp/debshots.sql
psql -h $DB_HOST debshots_dev debshots < /tmp/debshots.sql
psql -h $DB_HOST debshots_test debshots < /tmp/debshots.sql

bundle exec rake db:migrate

