From c4878edd11e43e9011c63d5a244bbbb719c0af16 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Tue, 2 Feb 2010 17:32:37 +0000
Subject: [PATCH] Enhancement of debian package

---
 build/deb/config                              | 18 +++---
 build/deb/examples                            |  1 +
 ....forced.php => install.forced.php.install} |  4 +-
 build/deb/postinst                            | 40 ++++++++++--
 build/deb/postrm                              | 64 +++++++++++++++----
 build/deb/templates                           |  8 +--
 build/tgz/tar_exclude.txt                     |  2 +
 build/zip/zip_exclude.txt                     |  2 +
 8 files changed, 104 insertions(+), 35 deletions(-)
 create mode 100755 build/deb/examples
 rename build/deb/{install.forced.php => install.forced.php.install} (80%)
 mode change 100644 => 100755

diff --git a/build/deb/config b/build/deb/config
index bec2d02cc52..55116163de1 100644
--- a/build/deb/config
+++ b/build/deb/config
@@ -13,6 +13,7 @@
 
 set -e
 
+
 . /usr/share/debconf/confmodule
 
 db_version 2.0
@@ -24,8 +25,8 @@ echo Run the config script
 if [ "$1" = "reconfigure" ] ; then
 	config="/etc/dolibarr/apache.conf"
 	for i in $(seq 8 -1 0) ; do
-		if [ -f ${config}.$i ]
-			then mv ${config}.$i ${config}.$(($i +1))
+		if [ -f ${config}.$i ] ; then
+			mv ${config}.$i ${config}.$(($i +1))
 		fi
 	done
 	mv ${config} ${config}.0
@@ -35,11 +36,12 @@ fi
 
 db_capb backup
 
+db_title "dolibarr" || true
+
 
 # Ask for web server type.
 #db_input critical "dolibarr/webserver" || true
 
-db_title "dolibarr" || true
 if db_go ; then
     okcancel="1"
 else
@@ -47,9 +49,6 @@ else
 fi
 
 
-# Ask on wich host the DBMS is installed.
-#db_input critical "dolibarr/db/host" || true
-
 # Get the database administrator name and password.
 #db_beginblock
 #	db_input critical "dolibarr/db/admin/name" || true
@@ -66,9 +65,12 @@ fi
 #db_input critical "dolibarr/db/user/password" || true
 
 # Ask for deleting all the database on package purge.
-#db_input medium "dolibarr/postrm" || true
-#onsuccess='finished="true"'
+#db_input critical "dolibarr/postrm" || true
 
+# Launch input screens
+#db_go || true
 
+onsuccess='finished="true"'
 db_stop
+
 exit 0
diff --git a/build/deb/examples b/build/deb/examples
new file mode 100755
index 00000000000..aa4824411d6
--- /dev/null
+++ b/build/deb/examples
@@ -0,0 +1 @@
+usr/share/dolibarr/htdocs/conf/conf.php.example
\ No newline at end of file
diff --git a/build/deb/install.forced.php b/build/deb/install.forced.php.install
old mode 100644
new mode 100755
similarity index 80%
rename from build/deb/install.forced.php
rename to build/deb/install.forced.php.install
index 78f656957d1..105228e906c
--- a/build/deb/install.forced.php
+++ b/build/deb/install.forced.php.install
@@ -8,8 +8,8 @@ $force_install_createdatabase='1';
 $force_install_databaselogin='dolibarrmysql';
 $force_install_databasepass='';
 $force_install_createuser='1';
-$force_install_databaserootlogin='root';
-$force_install_databaserootpass='';
+$force_install_databaserootlogin='__SUPERUSERLOGIN__';
+$force_install_databaserootpass='__SUPERUSERPASSWORD__';
 $force_install_dolibarrlogin='admin';
 $force_install_nophpinfo='1';
 $force_install_lockinstall='1';
diff --git a/build/deb/postinst b/build/deb/postinst
index e2467eaf80f..97cf449b7d2 100644
--- a/build/deb/postinst
+++ b/build/deb/postinst
@@ -15,7 +15,9 @@ set -e
 #          <conflicting-package> <version>
 # for details, see /usr/share/doc/packaging-manual/
 
+
 . /usr/share/debconf/confmodule
+
 db_version 2.0
 
 #
@@ -53,11 +55,23 @@ case "$1" in
 		mkdir -p /etc/dolibarr
 		cp -p $fileorig $config
 
-		# Copy install.forced.php
-		fileorig="/usr/share/dolibarr/build/deb/install.forced.php"
+		# Create install.forced.php
+		fileorig="/usr/share/dolibarr/build/deb/install.forced.php.install"
 		config="/usr/share/dolibarr/htdocs/install/install.forced.php"
-		cp -p $fileorig $config
-
+		superuserlogin=''
+		superuserpassword=''
+		if [ -f /etc/mysql/debian.cnf ] ; then
+			# Load superuser login and pass
+			superuserlogin=$(/bin/grep --max-count=1 "user" /etc/mysql/debian.cnf | /bin/sed -e 's/^user[ =]*//g')
+			superuserpassword=$(/bin/grep --max-count=1 "password" /etc/mysql/debian.cnf | /bin/sed -e 's/^password[ =]*//g')
+		fi
+		echo Mysql superuser found to use is $superuserlogin
+		if [ -z "$superuserlogin" ] ; then
+			cat $fileorig | sed -e 's/__SUPERUSERLOGIN__/root/g' | sed -e 's/__SUPERUSERPASSWORD__//g' > $config
+		else
+			cat $fileorig | sed -e 's/__SUPERUSERLOGIN__/'$superuserlogin'/g' | sed -e 's/__SUPERUSERPASSWORD__/'$superuserpassword'/g' > $config
+		fi
+		
 		# Create empty conf.php
 		if [ ! -f /usr/share/dolibarr/htdocs/conf/conf.php ]
 		then 
@@ -150,14 +164,26 @@ case "$1" in
 		#    perl -pi -e "s/DBPASS/$dbpass/" /usr/share/dolibarr/htdocs/conf/conf.php
 		#fi
 
+		# Restart servers
 		servers="apache2-ssl apache2 mysql"
-		. /usr/share/wwwconfig-common/restart.sh
-
+		if [ -f /usr/share/wwwconfig-common/restart.sh ] ;
+		then
+			. /usr/share/wwwconfig-common/restart.sh
+		else
+			# Another way to restart
+			for server in $servers ; do
+        		if [ -x /usr/sbin/invoke-rc.d ]; then
+        	    	invoke-rc.d $server reload || true
+        		else
+        	    	/etc/init.d/$server reload || true
+        		fi
+			done
+		fi
+		
 		echo "Launch Dolibarr on page http://localhost/dolibarr/ to complete the installation and use Dolibarr."
 	;;
 
 	abort-upgrade|abort-remove|abort-deconfigure)
-
 	;;
 
 	*)
diff --git a/build/deb/postrm b/build/deb/postrm
index 1929717794f..03d9dd5b915 100644
--- a/build/deb/postrm
+++ b/build/deb/postrm
@@ -1,4 +1,4 @@
-#! /bin/sh
+#!/bin/sh
 # postrm script for dolibarr
 #
 # see: dh_installdeb(1)
@@ -16,10 +16,13 @@ set -e
 #        * <disappearer's-postrm> `disappear' <r>overwrit>r> <new-version>
 # for details, see /usr/share/doc/packaging-manual/
 
+
 . /usr/share/debconf/confmodule
 
 db_version 2.0
 
+echo Run the postrm script
+
 #echo "postrm db_get dolibarr/webserver"
 # We disable set -e to avoid premature end of script if error
 set +e
@@ -40,23 +43,40 @@ case "$1" in
 	purge)
 		echo "postrm purge webservers=$webservers includefile=$includefile"
 
-		#echo "postrm db_get dolibarr/postrm"
+		# Ask if we must delete database
+		echo "postrm db_input dolibarr/postrm"
+		db_input critical "dolibarr/postrm" || true
+		db_go || true
+
+		echo "postrm db_get dolibarr/postrm"
 		# We disable set -e to avoid premature end of script if error
 		set +e
 		db_get "dolibarr/postrm"
 		set -e
 	
 		if [ "$RET" = "true" ] ; then
+			echo postrm Mysql database deletion
 			# Get database configuration
 			dbserver="localhost"
-			db_get "dolibarr/db/name"
-			dbname="$RET"
-			db_get "dolibarr/db/admin/name"
-			dbadmin="$RET"
-			db_get "dolibarr/db/admin/password"
-			dbadmpass="$RET"
+			dbname="dolibarr"
+			#db_get "dolibarr/db/name"
+			#dbname="$RET"
+			superuserlogin=''
+			superuserpassword=''
+			if [ -f /etc/mysql/debian.cnf ] ; then
+				# Load superuser login and pass
+				superuserlogin=$(/bin/grep --max-count=1 "user" /etc/mysql/debian.cnf | /bin/sed -e 's/^user[ =]*//g')
+				superuserpassword=$(/bin/grep --max-count=1 "password" /etc/mysql/debian.cnf | /bin/sed -e 's/^password[ =]*//g')
+			fi
+			echo postrm Mysql superuser found to use is $superuserlogin
+			dbadmin="$superuserlogin"
+			dbadmpass="$superuserpassword"
+			#db_get "dolibarr/db/admin/name"
+			#dbadmin="$RET"
+			#db_get "dolibarr/db/admin/password"
+			#dbadmpass="$RET"
 			dbtype="mysql"
-
+			
 			. /usr/share/wwwconfig-common/${dbtype}-dropuser.sh
 			. /usr/share/wwwconfig-common/${dbtype}-dropdb.sh
 		fi
@@ -84,36 +104,50 @@ case "$1" in
 
 		rm -rf /etc/dolibarr
 
-		# Restart
+		# Restart servers
 		servers="apache2-ssl apache2 mysql"
 		if [ -f /usr/share/wwwconfig-common/restart.sh ] ;
 		then
 			. /usr/share/wwwconfig-common/restart.sh
+		else
+			# Another way to restart
+			for server in $servers ; do
+        		if [ -x /usr/sbin/invoke-rc.d ]; then
+        		     invoke-rc.d $server reload || true
+        		else
+        		     /etc/init.d/$server reload || true
+        		fi
+			done
 		fi
-
+		
 		# Remove file and conf file
 		for dir in /usr/share/dolibarr ; do
 			if [ -d ${dir} ] ; then rm -rf ${dir} ; fi
 		done
 		
+		db_reset "dolibarr/postrm"
 		db_purge
 	;;
+
 	remove)
-		echo "Force remove of /usr/share/dolibarr/htdocs/install"
+		echo "postrm Force remove of /usr/share/dolibarr/htdocs/install"
 		rm -fr /usr/share/dolibarr/htdocs/install
 
-		echo "Remove menu entry and image"
+		echo "postrm Remove menu entry and image"
 		fileorig="/usr/share/applications/dolibarr.desktop"
 		rm -f $fileorig
 		fileorig="/usr/share/pixmaps/dolibarr.xpm"
 		rm -f $fileorig
-		# Not sure this is usefulle
+		# Not sure this is usefull
 		if test -x /usr/bin/update-menus; then update-menus; fi
 	;;
+
 	upgrade)
 	;;
+
 	failed-upgrade|abort-install|abort-upgrade|disappear)
 	;;
+
 	*)
 		echo "postrm called with unknown argument \`$1'" >&2
 		exit 0
@@ -122,4 +156,6 @@ esac
 
 db_stop
 
+#DEBHELPER#
+
 exit 0
diff --git a/build/deb/templates b/build/deb/templates
index ced3f34b027..703aa9fc2db 100644
--- a/build/deb/templates
+++ b/build/deb/templates
@@ -96,7 +96,7 @@ Description: Please, insert the DBMS administrator password (if any):
 Template: dolibarr/postrm
 Type: boolean
 Default: true
-Description: Delete *ALL*?
- Datas subjected to this question are the DBMS account, the DB and the
- Dolibarr users ang group files. Do I have to delete *ALL* datas whenever
- the Dolibarr package will be completely removed from the system?
+Description: Delete database ?
+ Do I have to delete also the Dolibarr MySQL database and all its datas
+ (datas subjected to this quetion are the Dolibarr DBMS account and all
+ Dolibarr tables) ?
diff --git a/build/tgz/tar_exclude.txt b/build/tgz/tar_exclude.txt
index 09ae0565a05..4d3873c11f9 100644
--- a/build/tgz/tar_exclude.txt
+++ b/build/tgz/tar_exclude.txt
@@ -21,6 +21,8 @@ doc/wiki
 document
 documents
 htdocs/conf/conf.php
+htdocs/conf/conf.php.mysql
+htdocs/conf/conf.php.postgres
 htdocs/avoir
 htdocs/document
 htdocs/documents
diff --git a/build/zip/zip_exclude.txt b/build/zip/zip_exclude.txt
index 8d0c40042e4..bb5fef6e7a8 100644
--- a/build/zip/zip_exclude.txt
+++ b/build/zip/zip_exclude.txt
@@ -20,6 +20,8 @@
 */document/*
 */documents/*
 */htdocs/conf/conf.php
+*/htdocs/conf/conf.php.mysql
+*/htdocs/conf/conf.php.postgres
 */htdocs/avoir/*
 */htdocs/document/*
 */htdocs/documents/*
-- 
GitLab