#!/bin/bash

ZIPFILE=$1
VERSION=$2

# Enter the MySQL Database information (only alphanumeric user/pass).
DB_NAME=cpaddons-update-db
DB_USER=cpaddonsupdate
DB_PASS=cpaddonspasswd
TABLE_PREFIX=cpg15x_

# Identify where the website will be located (internal FS and web URL).
HOST=$(hostname)
ROOT=/usr/local/apache/htdocs

# Make sure we won't destroy any data.
echo "SHOW GRANTS ON '$DB_USER'@'localhost';" | mysql 2>/dev/null && exit 1
echo "USE \`$DB_NAME\`;" | mysql 2>/dev/null && exit 1

# Create a temporary directory for this install.
NAME=$(cd "$ROOT"; mktemp -d Coppermine-XXXXX)
DIR=$ROOT/$NAME

function cleanup() {
    rm -rf "$DIR"
    echo "DROP DATABASE \`$DB_NAME\`;" | mysql
    echo "DROP USER '$DB_USER'@'localhost';" | mysql
}
trap cleanup EXIT

# Create a temporary database for this install.
echo "CREATE DATABASE \`$DB_NAME\`;" | mysql
echo "CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql
echo "GRANT ALL ON \`$DB_NAME\`.* TO '$DB_USER'@'localhost';" | mysql

# Extract the contents.
unzip -q -d "$DIR" "$ZIPFILE"

# The zip file is distributed with its own root directory, and we can't
# do --strip-components like with tar.  So figure out the actual path.
ACTUAL_DIR=$(ls -d -- "$DIR"/*/)
SUBDIR=${ACTUAL_DIR:${#DIR}}

# Force Coppermine to work on PHP 5.4 and later.
cat <<PATCH | patch -p1 -d "$ACTUAL_DIR" >/dev/null
diff --git a/install.php b/install.php
index bf0947e..0d25248 100644
--- a/install.php
+++ b/install.php
@@ -1,4 +1,6 @@
 <?php
+# Remove E_STRICT, since EasyApache didn't do it for PHP 5.4 (Case 82421).
+error_reporting(error_reporting() & ~E_STRICT);
 /*************************
   Coppermine Photo Gallery
   ************************
diff --git a/install_classic.php b/install_classic.php
index 5ff1bb1..1ef21c2 100644
--- a/install_classic.php
+++ b/install_classic.php
@@ -27,9 +27,8 @@
          );
 } // end check
 
-// Report all errors except E_NOTICE
-// This is the default value set in php.ini
-error_reporting (E_ALL ^ E_NOTICE);
+# Remove E_STRICT, since EasyApache didn't do it for PHP 5.4 (Case 82421).
+error_reporting(error_reporting() & ~E_STRICT);
 
 require('include/sql_parse.php');
 // ---------------------------- TEST PREREQUIRED --------------------------- //
PATCH

# Make sure Apache can access the files.
chown nobody:nobody -R "$DIR"

# We could auto-determine this, but I generally expect this will always hold true.
IMPATH=/usr/local/cpanel/3rdparty/bin

# Run the installation script.
URL=http://$HOST/$NAME/$SUBDIR/install_classic.php
curl --silent "$URL" \
    -F "admin_username=$DB_USER" \
    -F "admin_password=$DB_PASS" \
    -F "admin_email=fake-admin@cpanel.tld" \
    -F "dbserver=localhost" \
    -F "dbname=$DB_NAME" \
    -F "dbuser=$DB_USER" \
    -F "dbpass=$DB_PASS" \
    -F "table_prefix=$TABLE_PREFIX" \
    -F "impath=$IMPATH" \
    | grep -q "Installation completed" || exit 1

# Extract the mysql data that we came for.
./mysql_normalized_dump.pl -t "$TABLE_PREFIX" -u "$DB_USER" -p "$DB_PASS" -d "$DB_NAME" -o "$VERSION.mysql"

# Cleanup
exit
