#!/bin/sh
# Import de l'ancienne base (dump phpMyAdmin passé en argument) puis test de l'écran des dossiers repris.
set -e
cd "$(dirname "$0")/.."
DUMP="${1:-/mnt/user-data/uploads/prise_en_charge__1_.sql}"
export HALCOMI_DSN="sqlite:/tmp/halcomi_test2.db" HALCOMI_UPLOADS=/tmp/halcomi_test2_uploads HALCOMI_DEBUG=1
rm -rf /tmp/halcomi_test2.db /tmp/halcomi_test2_uploads
python3 - "$DUMP" <<'PY'
import sqlite3, re, sys
src = open(sys.argv[1], encoding='utf-8').read()
db = sqlite3.connect('/tmp/halcomi_test2.db')
db.execute("create table prise_en_charge(id integer primary key,antenne_destination,journee,num_conteneur,num_dau,type_conteneur,proprietaire,designation,lieu,agents_ecor,tel_chauffeur,tel_proprietaire,heure,responsable,photo_url)")
b = src[src.index("INSERT INTO `prise_en_charge`"):]; b = b[:b.index("\nCOMMIT;")]
b = b.replace("\\'", "''").replace('\\"', '"').replace("\\n", "\n").replace("\\r", "")
b = re.sub(r"INSERT INTO `prise_en_charge` \([^)]*\) VALUES", "INSERT INTO prise_en_charge VALUES", b)
db.executescript(b); db.commit()
PY
php tools/install.php >/dev/null
php tools/import_legacy.php >/dev/null
php -S 127.0.0.1:8099 -t public public/index.php >/tmp/halcomi_server2.log 2>&1 &
PID=$!
trap "kill $PID 2>/dev/null; rm -f tools/identifiants_initiaux.csv tools/rapport_import.txt" EXIT
sleep 1
php tests/e2e_repris.php
