# SliTaz package receipt.

PACKAGE="ocsreports"
VERSION="2.0.4"
CATEGORY="network"
SHORT_DESC="Inventory and package deployement system"
MAINTAINER="pascal.bellard@slitaz.org"
LICENSE="GPL2"
WANTED="ocsinventory"
SOURCE="OCSNG_UNIX_SERVER"
WEB_SITE="https://www.ocsinventory-ng.org/"
TAGS="system administration"

DEPENDS="php-apache php-mysql mysql"

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs/usr/share \
		$fs/etc/ocsreports
		
	cp -a $src/${PACKAGE} $fs/usr/share/
	cat > $fs/etc/ocsreports/dbconfig.inc.php <<EOT
<?php 
define("DB_NAME", "ocsweb");
define("SERVER_READ","localhost");
define("SERVER_WRITE","localhost");
define("COMPTE_BASE","ocs");
define("PSWD_BASE","ocs");
?>
EOT

	install -o root -g www -m 0755 $src/binutils/*.pl \
		$fs/usr/share/ocsreports/
		
	rm -f $fs/usr/share/${PACKAGE}/dbconfig.inc.php
	cd $fs/usr/share/${PACKAGE} ; ln -s /etc/ocsreports/dbconfig.inc.php ; cd -

	# Remove doc file.
	rm -f  $fs/usr/share/ocsreports/files/*.pdf
}

post_install()
{
	# Configure apache server
	if [ -f "$1/etc/apache/httpd.conf" ]; then
		if [ ! -f "$1/etc/apache/conf.d/$PACKAGE" ]; then
			cat > "$1/etc/apache/conf.d/$PACKAGE" <<EOT
<IfModule mod_alias.c>
    Alias /$PACKAGE /usr/share/$PACKAGE/
</IfModule>
<Directory "/usr/share/$PACKAGE">
	Options Indexes FollowSymLinks
	AllowOverride None
	Order deny,allow
	Allow from all
</Directory>

EOT
			if [ -z "$1" ]; then
				# Start Web server.
				test -f /var/run/apache/httpd.pid && \
					( kill -0 $(cat /var/run/apache/httpd.pid) && /etc/init.d/apache restart )
			fi
		fi
	fi

	# Configure every thing for ocsweb.
	if [ -z "$1" ]; then
		if ( ! mysqladmin -s ping > /dev/null ); then
			echo
			echo "Starting MySQL server"
			( /etc/init.d/mysql start ; status  ) || exit 
			sleep 4 #let the mysql daemon start
		fi
		if ( ! mysql -Be 'show databases' | grep -q ocsweb ); then
			echo
			echo -n "Create $PACKAGE database and schema"
			mysql -Be "create database ocsweb CHARACTER SET utf8 COLLATE utf8_bin" ; status
			mysql -s -u root ocsweb < /usr/share/$PACKAGE/files/ocsbase_new.sql > /dev/null 2>&1
			# We suppose that ocs user does not exist.
			# It may be false.
			echo  -n "Create user ocs with password ocs"
			mysql -Be "grant all privileges on ocsweb.* to 'ocs'@'localhost' 
				identified by 'ocs'" ; status
		fi
			
	fi
	true
}

post_remove()
{

	if [ -z "$1" ]; then
		echo -n "Would you like to remove data and database files.(y/n) "
		read answer

		case $answer in 
		y|Y)
			echo -n "Removing data directories..."
			rm -rf /usr/share/ocsreports
			if ( mysql -Be 'show databases' | grep -q ocsweb ); then
				echo -n "Deleting ocsreports database;"
				mysql -u root -Be "drop database ocsweb;" ; status
				echo  -n "Delete user ocs"
				mysql -u root -Be "delete from mysql.db where user=ocs;" ; status
			fi
			unset $answer
			;;
		*)
			;;
		esac
	fi
	
}