#!/bin/bash
# Copyright (C) 2010-2026	Laurent Destailleur 		<eldy@users.sourceforge.net>
# Copyright (C) 2024-2026	MDW							<mdeweerd@users.noreply.github.com>

#------------------------------------------------------
# Script to purge and initialize a database with demo values.
# Note: "dialog" tool needs to be available if no parameter provided.
#
# WARNING: This script erase all the data in the database
# with data into dump file
#
# Regis Houssin       - regis.houssin@inodbox.com
# Laurent Destailleur - eldy@users.sourceforge.net
#------------------------------------------------------
# Usage: initdemo.sh confirm|confirmcleanblockedlog
# usage: initdemo.sh confirm|confirmcleanblockedlog mysqldump_dolibarr_x.x.x.sql database port login pass
#------------------------------------------------------


export mydir
mydir=${0//initdemo.sh/}
if [ "$mydir" = "" ] || [ "$mydir" = "./" ]
then
	export mydir="."
fi
export id
id=$(id -u)


# ----------------------------- check if root
if [ "$id" != "0" ] && [ "$id" != "1001" ]
then
	echo "Script must be executed as root"
	exit
fi


# ----------------------------- command line params
confirm=$1
dumpfile=$2
base="${3:-dolibarrdemo}"
port="${4:-3306}"
admin=$5
passwd=$6

# ----------------------------- check params
if [ "$confirm" != "confirm" ] && [ "$confirm" != "confirmcleanblockedlog" ]
then
	echo "----- $0 -----"
	echo "Usage: initdemo.sh confirm|confirmcleanblockedlog"
	echo " or"
	echo "Usage: initdemo.sh confirm|confirmcleanblockedlog [mysqldump_dolibarr_x.x.x.sql database port login pass]"
	echo
	echo "confirm:                To reload database"
	echo "confirmcleanblockedlog: To reload database and reset blocked log"
	echo
	exit
fi


# ----------------------------- check if dialog available
command -v dialog >/dev/null 2>&1 || {
	echo "Error: command dialog not found. On Linux, you can install it with: apt install dialog"
	exit
}
DIALOG=${DIALOG:=dialog}
DIALOG="$DIALOG --ascii-lines"


# ----------------------------- if no params on command line
if [ "$passwd" = "" ]
then
	export dumpfile
	# shellcheck disable=2012
	dumpfile=$(ls -v "$mydir/mysqldump_dolibarr_"*".sql" | tail -n 1)
	dumpfile=$(basename "$dumpfile")

	# ----------------------------- input file

	fichtemp=$(mktemp 2>/dev/null) || fichtemp=/tmp/test$$
	# shellcheck disable=2064,2172
	trap "rm -f $fichtemp" 0 1 2 5 15
	$DIALOG --title "Init Dolibarr with demo values" --clear --inputbox "Input dump file :" 16 55 "$dumpfile" 2> "$fichtemp"
	valret=$?
	case $valret in
		0)
			dumpfile=$(cat "$fichtemp") ;;
		1)
			exit ;;
		255)
			exit ;;
	esac
	rm "$fichtemp"

	# ----------------------------- database name
	fichtemp=$(mktemp 2>/dev/null) || fichtemp=/tmp/test$$
	# shellcheck disable=2064,2172
	trap "rm -f '$fichtemp'" 0 1 2 5 15
	$DIALOG --title "Init Dolibarr with demo values" --clear --inputbox "Mysql database name :" 16 55 "$base" 2> "$fichtemp"
	valret=$?
	case $valret in
		0)
			base=$(cat "$fichtemp") ;;
		1)
			exit ;;
		255)
			exit ;;
	esac
	rm "$fichtemp"

	# ---------------------------- database port
	fichtemp=$(mktemp 2>/dev/null) || fichtemp=/tmp/test$$
	# shellcheck disable=2064,2172
	trap "rm -f '$fichtemp'" 0 1 2 5 15
	$DIALOG --title "Init Dolibarr with demo values" --clear --inputbox "Mysql port (ex: 3306):" 16 55 "$port" 2> "$fichtemp"
	valret=$?

	case $valret in
		0)
			port=$(cat "$fichtemp") ;;
		1)
			exit ;;
		255)
			exit ;;
	esac
	rm "$fichtemp"

	# ---------------------------- compte admin mysql
	fichtemp=$(mktemp 2>/dev/null) || fichtemp=/tmp/test$$
	# shellcheck disable=2064,2172
	trap "rm -f '$fichtemp'" 0 1 2 5 15
	$DIALOG	 --title "Init Dolibarr with demo values" --clear --inputbox "Mysql user login (ex: root):" 16 55 root 2> "$fichtemp"
	valret=$?

	case $valret in
		0)
			admin=$(cat "$fichtemp") ;;
		1)
			exit ;;
		255)
			exit ;;
	esac
	rm "$fichtemp"

	# ---------------------------- password admin mysql (root)
	fichtemp=$(mktemp 2>/dev/null) || fichtemp=/tmp/test$$
	# shellcheck disable=2064,2172
	trap "rm -f '$fichtemp'" 0 1 2 5 15
	$DIALOG --title "Init Dolibarr with demo values" --clear --passwordbox "Password for Mysql user login :" 16 55 2> "$fichtemp"
	valret=$?

	case $valret in
		0)
			passwd=$(cat "$fichtemp") ;;
		1)
			exit ;;
		255)
			exit ;;
	esac
	rm "$fichtemp"


	export documentdir
	# shellcheck disable=2016
	documentdir=$(< "$mydir/../../htdocs/conf/conf.php" grep '^\$dolibarr_main_data_root' | sed -e 's/$dolibarr_main_data_root=//' | sed -e 's/;//' | sed -e "s/'//g" | sed -e 's/"//g')


	# ---------------------------- confirmation
	$DIALOG --title "Erase Dolibarr with demo values" --clear --yesno "Do you confirm ? \n Dump file : '$dumpfile' \n Dump dir : '$mydir' \n Document dir : '$documentdir' \n Mysql database : '$base' \n Mysql port : '$port' \n Mysql login: '$admin' \n Mysql password : --hidden--" 15 55

	case $? in
		0)      echo "Ok, start process..." ;;
		1)      exit ;;
		255)    exit ;;
	esac

fi


# ---------------------------- Run sql file
if [ "$passwd" != "" ]
then
	export passwd="-p$passwd"
	export passwdshown="-p*****"
fi
#echo "mysql -P$port -u$admin $passwd $base < $mydir/$dumpfile"
#mysql -P$port -u$admin $passwd $base < $mydir/$dumpfile
#echo "drop old table"
echo "drop table if exists llx_accounting_account;"
echo "drop table if exists llx_accounting_account;" | mysql "-P$port" "-u$admin" "$passwd" "$base"
echo "drop table if exists llx_accounting_system;"
echo "drop table if exists llx_accounting_system;" | mysql "-P$port" "-u$admin" "$passwd" "$base"

echo "mysql -P$port -u$admin $passwdshown $base < '$mydir/$dumpfile'"
mysql "-P$port" "-u$admin" "$passwd" "$base" < "$mydir/$dumpfile"
export res=$?

if [ $res -ne 0 ]; then
	echo "Error to load database dump with: mysql -P$port -u$admin $passwdshown $base < '$mydir/$dumpfile'"
	exit
fi


# ---------------------------- Run update of demo data
echo
echo Run script updatedemo.php confirm
"$mydir/updatedemo.php" confirm
export res=$?


# ---------------------------- Run update of demo data
if [ "$confirm" == "confirmcleanblockedlog" ]; then
	echo
	echo Run script updatedemo.php confirmcleanblockedlog
	"$mydir/updatedemo.php" confirmcleanblockedlog
	export res=$?
fi


# ---------------------------- Copy demo files
export documentdir
# shellcheck disable=2016
documentdir=$(< "$mydir/../../htdocs/conf/conf.php" grep '^\$dolibarr_main_data_root' | sed -e 's/$dolibarr_main_data_root=//' | sed -e 's/;//' | sed -e "s/'//g" | sed -e 's/"//g')
if [ "$documentdir" != "" ]
then
	$DIALOG --title "Reset document directory" --clear --yesno "DELETE and recreate document directory '$documentdir/':" 16 55
	valret=$?

	case $valret in
		0)
			#  YES
			echo "RECREATE $documentdir"
			echo "  rm -fr '$documentdir/'*"
			rm -fr "${documentdir:?}/"* ;;
		1)
			exit ;;
		255)
			exit ;;
	esac

	echo "cp -pr '$mydir/documents_demo/'* '$documentdir/'"
	cp -pr "$mydir/documents_demo/"* "$documentdir/"

	mkdir "$documentdir/doctemplates/" 2>/dev/null
	echo cp -pr "$mydir/../../htdocs/install/doctemplates/"* "$documentdir/doctemplates/"
	cp -pr "$mydir/../../htdocs/install/doctemplates/"* "$documentdir/doctemplates/"

	echo cp -pr "$mydir/../../htdocs/install/medias/"* "$documentdir/medias/image/"
	cp -pr "$mydir/../../htdocs/install/medias/"* "$documentdir/medias/image/"

	mkdir -p "$documentdir/ecm/Administrative documents" 2>/dev/null
	mkdir -p "$documentdir/ecm/Images" 2>/dev/null
	rm -f "$documentdir/doctemplates/"*/index.html
	echo cp -pr "$mydir/../../doc/images/"* "$documentdir/ecm/Images"
	cp -pr "$mydir/../../doc/images/"* "$documentdir/ecm/Images"

	chmod -R u+w "$documentdir/"
	chown -R www-data "$documentdir/"
else
	echo "Detection of 'documents' directory in '$mydir' failed so demo files were not copied."
fi


if [ -s "$mydir/initdemopostsql.sql" ]; then
	mysql "-P$port" "$base" < "$mydir/initdemopostsql.sql"
fi


if [ "$res" = "0" ]
then
	echo "Success, file successfully loaded: Note that crypted data need to have dolibarr_main_instance_unique_id=11f3c81e86fc9e3b3fd11d81c9a31bd0 with this data set to be readable."
else
	echo "Error, 1 step of script has failed."
fi
echo
