#!/bin/bash
SCRIPT_PATH=$(pwd)
if [ ${#1} -lt 1 ]
	then
		echo "YES Reset Script"
		echo "Syntax:"
		echo "./yes_reset.sh <relativer pfad zu www-data ohne abschliessendes Slash>" 
		echo
		echo "Funktionsweise:"
		echo "Es werden von clean die komplette DB, die Templates und die Bilder sowie das ebaymich/ Verzeichnis heruntergeladen. Ausser den beiden configuration.php Dateien, die beiden application_top.php, der cron_application_top.php, .htusers.php fuer den Dateimanager sowie der config.ebayapi.inc.php werden alle Dateien geloescht und das System von clean uebernommen. Die Konfigurationsdateien bleiben also erhalten - falls also ein neuer RU_NAME oder EBAY Prefix notwendig ist oder eine andere Domain hinterlegt werden soll, muss das per Hand angepasst werden. Nach dem Resetten ist das System also einsatzbereit und die Konfigurationsdateien muessen nicht angepasst werden." 
		echo
		echo "Bedingungen:"
		echo "Das aktuelle System muss am CVS Server registriert sein. Die SSH Zugangsdaten fuer clean und evtl fuer den CVS Server werden benoetigt. ALLE Daten des Systems (Datei- und DB-seitig) gehen mit dem Reset verloren und werden durch die aktuelle Clean-Installation ersetzt."
		exit
fi
if [ -d "$1/www-data/yes" ]
	then
		echo "YES Reset Script"
		pathtowww="$1"
	else
		echo
		echo "Fehler: Verzeichnis $1/www-data/ existiert nicht."
		echo "./yes_reset <relativer pfad zu www-data ohne abschliessendes Slash>" 
		exit
fi

DB_SERVER=`php -r "error_reporting(E_ERROR);include('$1/www-data/yes/includes/configure.php'); echo DB_SERVER;"`
DB_SERVER_USERNAME=`php -r "error_reporting(E_ERROR);include('$1/www-data/yes/includes/configure.php'); echo DB_SERVER_USERNAME;"`
DB_SERVER_PASSWORD=`php -r "error_reporting(E_ERROR);include('$1/www-data/yes/includes/configure.php'); echo DB_SERVER_PASSWORD;"`
DB_DATABASE=`php -r "error_reporting(E_ERROR);include('$1/www-data/yes/includes/configure.php'); echo DB_DATABASE;"`
HTTP_SERVER=`php -r "error_reporting(E_ERROR);include('$1/www-data/yes/includes/configure.php'); echo HTTP_SERVER;"`

# configs sichern
cp "$1/www-data/yes/includes/configure.php" ./shop_configure.php
cp "$1/www-data/yes/includes/application_top.php" ./shop_application_top.php
cp "$1/www-data/yes/admin/includes/configure.php" ./admin_configure.php
cp "$1/www-data/yes/admin/includes/application_top.php" ./admin_application_top.php
cp "$1/www-data/yes/admin/includes/cron_application_top.php" ./admin_cron_application_top.php
cp "$1/www-data/yes/admin/includes/config.ebayapi.inc.php" ./config.ebayapi.inc.php
cp "$1/www-data/yes/admin/upload/.htusers.php" ./.htusers.php
# configs sichern ende

echo "AKTUELLES SYSTEM VON CLEAN IMPORTIEREN"
echo
echo "SSH PASSWORT FUER CLEAN EINGEBEN"
ssh ssh-399165-clean@agronalin.abcde.biz "./auto_export.sh"
curl -o filesystem_ebaymich.tar.bz2 "http://clean.agronalin.abcde.biz/filesystem_ebaymich.tar.bz2"
curl -o filesystem_yes.tar.bz2 "http://clean.agronalin.abcde.biz/filesystem_yes.tar.bz2"
curl -o db.sql.gz "http://clean.agronalin.abcde.biz/db.sql.gz"
clear
echo "DATEN IMPORTIERT - SAEUBERE CLEAN"
echo SSH PASSWORT FUER CLEAN EINGEBEN
ssh ssh-399165-clean@agronalin.abcde.biz "./auto_export_clean.sh"
mv filesystem_ebaymich.tar.bz2 "$pathtowww/www-data/"
cd "$pathtowww/www-data/"
echo "ENTPACKE ebaymich..."
rm -Rf ebaymich/
tar xfvj filesystem_ebaymich.tar.bz2
rm -f filesystem_ebaymich.tar.bz2
rm -Rf yes/
cvs co yes
echo "Templates einspielen"
mv $SCRIPT_PATH/filesystem_yes.tar.bz2 ./
tar xfvjk filesystem_yes.tar.bz2
rm -f filesystem_yes.tar.bz2
echo "Berechtigungen setzen."
  chmod -R 777 ebaymich
  chmod -R 777 yes/images/
  chmod -R 777 yes/media/
  chmod -R 777 yes/templates/
  chmod -R 777 yes/templates_c/
  chmod -R 777 yes/cache/
  chmod -R 777 yes/export/
  chmod -R 777 yes/pub/
  chmod -R 777 yes/download/
  chmod -R 777 yes/elog/
  chmod -R 777 yes/admin/templates/
  chmod -R 777 yes/admin/importdata/
  chmod -R 777 yes/admin/exportdata/
  chmod -R 777 yes/admin/SAX/
  chmod -R 777 yes/admin/rss/
  chmod -R 777 yes/admin/postbank/
  chmod -R 777 yes/admin/pdf/
  chmod -R 777 yes/admin/payments/
  chmod -R 777 yes/admin/cron/
  chmod -R 777 yes/admin/backups/
  chmod -R 777 yes/admin/bestellscheine/
  cd yes
  ln -s ../ebaymich/ ebaymich
  cd $SCRIPT_PATH
echo "FILESYSTEM ERFOLGREICH EINGESPIELT"
echo
echo "BEGINNE DATENBANK INSTALLATION"
echo
echo "Hole Aktuelle DB Indizes"
curl -o update_index.sql "http://clean.agronalin.abcde.biz/yes/repair_db.php?mode=sql"
gunzip -c db.sql.gz | /usr/bin/mysql -u$DB_SERVER_USERNAME -p$DB_SERVER_PASSWORD -h$DB_SERVER $DB_DATABASE --default_character_set utf8
/usr/bin/mysql -u$DB_SERVER_USERNAME -p$DB_SERVER_PASSWORD -h$DB_SERVER $DB_DATABASE < update_index.sql
rm -f update_index.sql
rm -f db.sql.gz
/usr/bin/mysql -u$DB_SERVER_USERNAME -p$DB_SERVER_PASSWORD -h$DB_SERVER $DB_DATABASE --execute="UPDATE configuration SET configuration_value='$PWD/ebayapi/' WHERE configuration_key='ABS_PATH_TO_PHPAPI';UPDATE configuration SET configuration_value='"$HTTP_SERVER"images/product_images/popup_images/' WHERE configuration_key='EBAY_PICPATH_WEBSERVER';UPDATE configuration SET configuration_value='"$HTTP_SERVER"ebay_account_edit.php' WHERE configuration_key='EBAY_USERLOGINURL'"
echo "DB Updated"

rm -f filesystem_ebayapi.tar.bz2

# configs zurueckspielen
mv -f ./shop_configure.php "$pathtowww/www-data/yes/includes/configure.php"
mv -f ./shop_application_top.php "$pathtowww/www-data/yes/includes/application_top.php"
mv -f ./admin_configure.php "$pathtowww/www-data/yes/admin/includes/configure.php"
mv -f ./admin_application_top.php "$pathtowww/www-data/yes/admin/includes/application_top.php"
mv -f ./admin_cron_application_top.php "$pathtowww/www-data/yes/admin/includes/cron_application_top.php"
mv -f ./config.ebayapi.inc.php "$pathtowww/www-data/yes/admin/includes/config.ebayapi.inc.php"
mv -f ./.htusers.php "$pathtowww/www-data/yes/admin/upload/.htusers.php"
# configs zurueckspielen ende

echo "INSTALLATION ERFOLGREICH ;-)"
