#! /bin/sh

if [ "$(id -u)" != 0 ]; then
    echo Trying to lauch $0 with root privilege
    sudo $0
    exit 0
else

    backup=/var/tmp/db.sqlite3-$(date +"%Y-%m-%d")

    cat <<EOF
+-------------------------------------------------------------------------+
| You are about to lauch SLM in demonstration mode. The current database  |
| will be overwritten by a demo database, so you must save it now if      |
| you want to use the current database later again.                       |
+-------------------------------------------------------------------------+
N.B.:
-----
The file /var/lib/slm/db.sqlite3 will be automatically copied
to $backup before being overwritten.

EOF

    echo -n "Are you ready to start SLM in demo mode [yN]? "
    read response

    case $response in
	y* | Y*)
	    echo launching demo session
	    if [ -f "$backup" ]; then
		echo There is already a copy of the database: $backup
		echo ... there will be no other copy done.
	    else
		echo "copying: /var/lib/slm/db.sqlite3 ==> $backup"
		cp /var/lib/slm/db.sqlite3 $backup
	    fi
	    cat <<EOF
+-------------------------------------------------------------------------+
| When the demo server is up and running, you can browse the service at   |
| http://127.0.0.1:8000/                                                  |
+-------------------------------------------------------------------------+
| The administor's account is: login = admin password = slmslm1234        |
+-------------------------------------------------------------------------+
EOF
	    cp /var/lib/slm/outils/db_test0.sqlite3 /var/lib/slm/db.sqlite3
	    setfacl -m u:www-data:rw /var/lib/slm/db.sqlite3
	    (cd /var/lib/slm; ./manage.py runserver)
	    ;;
	*)
	    echo NOT launching demo session
	    ;;
    esac
fi

