#!/bin/bash

echo "URL du site (sans www) :"
read siteName

echo "Matomo ID :"
read siteId

echo "New site name => $siteName"
echo "New site ID => $siteId"
echo "DB to duplicate => pk_imp_core"



mkdir $siteName

# app folders

ln -s /home/improd/core/packages $siteName/
ln -s /home/improd/core/app $siteName/

#files

cp /home/improd/core/.htaccess $siteName/

cp /home/improd/core/.bowerrc $siteName/
#ln -s /home/improd/core/.bowerrc $siteName/

cp /home/improd/core/bower.json $siteName/
#ln -s /home/improd/core/bower.json $siteName/

cp /home/improd/core/autoload.php $siteName/
#ln -s /home/improd/core/autoload.php $siteName/

cp /home/improd/core/index.php $siteName/
#ln -s /home/improd/core/index.php $siteName/

cp /home/improd/core/phpunit.xml.dist $siteName/
#ln -s /home/improd/core/phpunit.xml.dist $siteName/

cp /home/improd/core/gulpfile.js $siteName/
#ln -s /home/improd/core/gulpfile.js $siteName/

cp /home/improd/core/webpack.config.js $siteName/
#ln -s /home/improd/core/webpack.config.js $siteName/

cp /home/improd/core/pagekit $siteName/
#ln -s /home/improd/core/pagekit $siteName/

cp /home/improd/core/package.json $siteName/
#ln -s /home/improd/core/package.json $siteName/


cp /home/improd/core/composer.lock $siteName/
#ln -s /home/improd/core/composer.lock $siteName/

cp /home/improd/core/composer.json $siteName/
#ln -s /home/improd/core/composer.json $siteName/

ln -s /home/improd/core/CHANGELOG.md $siteName/
ln -s /home/improd/core/README.md $siteName/
ln -s /home/improd/core/LICENSE $siteName/

# create config.php

DBEXT=$siteName
DBEXT=${DBEXT//[-.]/_}
echo "db extention => $DBEXT"

printf "<?php return array (
  'application' =>
  array (
    'debug' => false,
  ),
  'database' =>
  array (
    'default' => 'mysql',
    'connections' =>
    array (
      'mysql' =>
      array (
        'host' => 'localhost',
        'user' => 'mepaxkit',
        'password' => 'Fr2uLCty',
        'dbname' => 'pk_imp_site_$DBEXT',
        'prefix' => 'pk_',
      ),
    ),
  ),
  'system' =>
  array (
    'secret' => 'zMmtmoXX.YBudCpKWYkioZ52kHtP1Qb5M.NtQ2prj/S8slLT7yhWSD/QWX617NOS',
  ),
  'system/cache' =>
  array (
    'caches' =>
    array (
      'cache' =>
      array (
        'storage' => 'auto',
      ),
    ),
    'nocache' => false,
  ),
  'system/finder' =>
  array (
    'storage' => '',
  ),
  'debug' =>
  array (
    'enabled' => false,
  ),
);" > $siteName/config.php


# tmp

mkdir $siteName/tmp
mkdir $siteName/tmp/cache
mkdir $siteName/tmp/temp
mkdir $siteName/tmp/sesssions
mkdir $siteName/tmp/logs
ln -s /home/improd/core/tmp/packages $siteName/tmp/packages


# storage

mkdir $siteName/storage
mkdir $siteName/storage/Local\ Press\ Files
mkdir $siteName/storage/Local\ Market\ Overview
mkdir $siteName/storage/Local\ Technical\ Articles
cp /home/improd/core/storage/default.jpg $siteName/storage/default.jpg
cp /home/improd/core/storage/Base-pagekit-logo.png $siteName/storage/Base-pagekit-logo.png
cp /home/improd/core/storage/pagekit-logo.svg $siteName/storage/pagekit-logo.svg

ln -s /home/improd/core/storage/imp-icon.png $siteName/storage/imp-icon.png
ln -s /home/improd/core/storage/imp-icon.svg $siteName/storage/imp-icon.svg

ln -s /home/improd/core/storage/Press\ Files $siteName/storage/Press\ Files
ln -s /home/improd/core/storage/b-top $siteName/storage/b-top
ln -s /home/improd/core/storage/b-side $siteName/storage/b-side
ln -s /home/improd/core/storage/b-bottom $siteName/storage/b-bottom
ln -s /home/improd/core/storage/b-news $siteName/storage/b-news

# google & bing
cp googlebd4d51f2c32ad21c.html $siteName/googlebd4d51f2c32ad21c.html
cp BingSiteAuth.xml $siteName/BingSiteAuth.xml

# fix rigth

chown www-data:www-data -R $siteName


# duplicate db

DBUSER=mepaxkit
DBPASSWORD=Fr2uLCty
DBSERVER=localhost
DBSNAME=pk_imp_core
DBNAME=pk_imp_site_$DBEXT

fCreateTable=""
fInsertData=""
echo "Copying database ... (may take a while ...)"
DBCONN="-h ${DBSERVER} -u ${DBUSER} --password=${DBPASSWORD}"
echo "DROP DATABASE IF EXISTS ${DBNAME}" | mysql ${DBCONN}
echo "CREATE DATABASE ${DBNAME}" | mysql ${DBCONN}
for TABLE in `echo "SHOW TABLES" | mysql $DBCONN $DBSNAME | tail -n +2`; do
        createTable=`echo "SHOW CREATE TABLE ${TABLE}"|mysql -B -r $DBCONN $DBSNAME|tail -n +2|cut -f 2-`
        fCreateTable="${fCreateTable} ; ${createTable}"
        insertData="INSERT INTO ${DBNAME}.${TABLE} SELECT * FROM ${DBSNAME}.${TABLE}"
        fInsertData="${fInsertData} ; ${insertData}"
done;
echo "$fCreateTable ; $fInsertData" | mysql $DBCONN $DBNAME

#db reset
echo "TRUNCATE TABLE pk_add" | mysql $DBCONN $DBNAME
echo "TRUNCATE TABLE pk_blog_post" | mysql $DBCONN $DBNAME
echo "TRUNCATE TABLE pk_formmaker_submission" | mysql $DBCONN $DBNAME
echo "TRUNCATE TABLE pk_imp_global" | mysql $DBCONN $DBNAME
echo "TRUNCATE TABLE pk_search_keywords" | mysql $DBCONN $DBNAME
echo "TRUNCATE TABLE pk_system_auth" | mysql $DBCONN $DBNAME
echo "TRUNCATE TABLE pk_system_session" | mysql $DBCONN $DBNAME


# apache config

# printf "<VirtualHost *:80>
# DocumentRoot /home/improd/www/$siteName
#
# ServerName www.$siteName
# ServerAlias $siteName
#
# <Directory \"/home/improd/www/$siteName\">
#     Options +FollowSymlinks
#     AllowOverride All
#     Require all granted
# </Directory>
#
# <FilesMatch \.php$>
#     SetHandler \"proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost/\"
# </FilesMatch>
#
# CustomLog /var/log/apache2/imp/$siteId-$siteName-access.log combined
# ErrorLog /var/log/apache2/imp/$siteId-$siteName-error.log
# </VirtualHost>" > /etc/apache2/sites-available/www.$siteName.conf
#
# ln -s /etc/apache2/sites-available/www.$siteName.conf /etc/apache2/sites-enabled/www.$siteName.conf
