#!/bin/sh
# Copyright (C) 2010 Brady Miller <brady@sparmy.com>
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This script is called by the the OpenEMR developer demos and
# developer appliance. It configures the Developer Demos and the
# Developer Appliance with the most recent git repository.
# (Note it first actually grabs the cvs version)
# Logs errors.
#

# PATH VARIABLES
WEB=/var/www/html
OPENEMR=$WEB/openemr
LOG=$WEB/log/logSetup.txt
CVS=/home/openemr/cvs
GITMAIN=/home/openemr/git
GIT=$GITMAIN/openemr
TMPDIR=/tmp/openemr-tmp

# PATH OF INSTALL SCRIPT
INST=$OPENEMR/contrib/util/installScripts/InstallerAuto.php
INSTTEMP=$OPENEMR/contrib/util/installScripts/InstallerAutoTemp.php

# Turn off apache to avoid users messing up while setting up
#  (start it again below after install/configure openemr
service httpd stop

# Connect to a new mandriva package repository (official repository no longer supports mandriva 2008)
echo "Updating new Mandriva Package repository. This is a 100MB download, so may take several minutes."
echo "Updating new Mandriva Package repository. This is a 100MB download, so may take several minutes." >> $LOG
urpmi.addmedia main http://ftp.ussg.iu.edu/linux/mandrake/official/2008.0/i586/media/main/release with media_info/hdlist.cz
urpmi.addmedia --update main_updates http://ftp.ussg.iu.edu/linux/mandrake/official/2008.0/i586/media/main/updates with media_info/hdlist.cz
urpmi.addmedia contrib http://ftp.ussg.iu.edu/linux/mandrake/official/2008.0/i586/media/contrib/release with media_info/hdlist.cz
urpmi.addmedia --update contrib_updates http://ftp.ussg.iu.edu/linux/mandrake/official/2008.0/i586/media/contrib/updates with media_info/hdlist.cz

# Install php-gd (graphics), php-mbstring (multibyte character support), php-zip, php-xsl and perl-CGI packages
#  and restart apache (done after below suhosin patch).
echo "Installing gd(graphics) and mbstring(multibyte support) and zip php packages and xsl php package and perl-CGI package and git packages..."
echo "Installing gd(graphics) and mbstring(multibyte support) and zip php packages and xsl php package and perl-CGI package and git packages..." >> $LOG
urpmi --auto php-gd php-mbstring php-zip php-xsl php-curl php-mcrypt perl-CGI patch git-core qgit >> $LOG

# COLLECT THE GIT REPO (if it doesn't exist)
if ! [ -d $GITMAIN ]; then
 echo "Downloading the OpenEMR git repository"
 echo "Downloading the OpenEMR git repository" >> $LOG
 mkdir -p $GITMAIN
 cd $GITMAIN
 git clone git://github.com/openemr/openemr.git
 cd openemr
 git checkout origin/rel-400
 cd ..
else
 echo "The OpenEMR git repository already exist"
 echo "The OpenEMR git repository already exist" >> $LOG
fi

# COPY THE GIT REPO OPENEMR COPY TO THE WEB DIRECTOY
echo "Copy git OpenEMR to web directory"
echo "Copy git OpenEMR to web directory" >> $LOG
rm -fr $OPENEMR/*
rsync --recursive --exclude .git $GIT/* $OPENEMR/

#INSTALL AND CONFIGURE OPENEMR
echo "Configuring OpenEMR"
echo "Configuring OpenEMR" >> $LOG
#
# Set file and directory permissions
chmod 666 $OPENEMR/sites/default/sqlconf.php
chown -R apache:apache $OPENEMR/sites/default/documents
chown -R apache:apache $OPENEMR/sites/default/edi
chown -R apache:apache $OPENEMR/sites/default/era
chown -R apache:apache $OPENEMR/library/freeb
chown -R apache:apache $OPENEMR/sites/default/letter_templates
chown -R apache:apache $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache
chown -R apache:apache $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
chown -R apache:apache $OPENEMR/gacl/admin/templates_c
#
# Run installer class for the demo (note to avoid malicious use, script is activated by removing an exit command,
#   and the active script is then removed after completion.
sed -e 's@^exit;@ @' <$INST >$INSTTEMP
php -f $INSTTEMP >> $LOG
rm -f $INSTTEMP
#
#reinstitute file permissions
chmod 644 $OPENEMR/sites/default/sqlconf.php
echo "Done configuring OpenEMR"
echo "Done configuring OpenEMR" >> $LOG

#Patch the suhosin bug
SUHOSINCONF=/etc/php.d/Z98_suhosin.ini
echo "Disabling suhosin patch which is not compatible with OpenEMR"
echo "Disabling suhosin patch which is not compatible with OpenEMR" >> $LOG
echo "   In /etc/php.d/Z98_suhosin.ini file commenting out:"
echo "   In /etc/php.d/Z98_suhosin.ini file commenting out:" >> $LOG
echo "    extension = suhosin.so"
echo "    extension = suhosin.so" >> $LOG
echo "   Then will restart apache."
echo "   Then will restart apache." >> $LOG
echo ""
sed -i 's/^extension = suhosin.so/; extension = suhosin.so/' $SUHOSINCONF
service httpd start
echo ""

#Replace manual with newest version and add the refresh script
echo "Configuring appliance"
echo "Configuring appliance" >> $LOG
cp -f $OPENEMR/contrib/util/installScripts/cvsDemoMisc/developerApplianceManual.txt /home/openemr/docs/manual.txt
chown openemr:openemr /home/openemr/docs/manual.txt
cp -f $OPENEMR/contrib/util/installScripts/cvsDemoMisc/developerApplianceManual.txt $WEB/manual.txt
cp -f $OPENEMR/contrib/util/installScripts/cvsDemoMisc/refresh /home/openemr/git/
chown openemr:openemr /home/openemr/git/refresh
chmod +x /home/openemr/git/refresh
echo "Done configuring appliance"
echo "Done configuring appliance" >> $LOG

#Package the development version into a tarball and zip file to be available thru web browser
# This is basically to allow download of most recent cvs version from the cvs Demo appliance
# It will also ease transfer/testing openemr on windows systems when using the Developer appliance
echo "Creating OpenEMR Development packages"
echo "Creating OpenEMR Development packages" >> $LOG

# Prepare the development package
mkdir -p $TMPDIR/openemr
rsync --recursive --exclude .git $GIT/* $TMPDIR/openemr/
chmod    a+w $TMPDIR/openemr/sites/default/sqlconf.php
chmod -R a+w $TMPDIR/openemr/sites/default/documents
chmod -R a+w $TMPDIR/openemr/sites/default/edi
chmod -R a+w $TMPDIR/openemr/sites/default/era
chmod -R a+w $TMPDIR/openemr/library/freeb
chmod -R a+w $TMPDIR/openemr/sites/default/letter_templates
chmod -R a+w $TMPDIR/openemr/interface/main/calendar/modules/PostCalendar/pntemplates/cache
chmod -R a+w $TMPDIR/openemr/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
chmod -R a+w $TMPDIR/openemr/gacl/admin/templates_c

# Create the web file directory
mkdir $WEB/files

# Save the tar.gz cvs package
cd $TMPDIR
rm -f $WEB/files/openemr-cvs.tar.gz
tar -czf $WEB/files/openemr-cvs.tar.gz openemr
cd $WEB/files
md5sum openemr-cvs.tar.gz > openemr-linux-md5.txt

# Save the .zip cvs package
cd $TMPDIR
rm -f $WEB/files/openemr-cvs.zip
zip -rq $WEB/files/openemr-cvs.zip openemr
cd $WEB/files
md5sum openemr-cvs.zip > openemr-windows-md5.txt

# Create the time stamp
date > date-cvs.txt

# Clean up
rm -fr $TMPDIR
echo "Done creating OpenEMR Development packages"
echo "Done creating OpenEMR Development packages" >> $LOG

