Instantiate CRM

Instantiate CRM

echo "Creating domain records..."

domain_args="debug=1 sequential=1 name=${domain} is_transactional=0"
if [ -n "${contact_id}" ]; then
    domain_args="${domain_args} contact_id=${contact_id}"
fi
if [ -n "${group_id}" ]; then
    domain_args="${domain_args} group_id=${group_id}"
fi
drush_output="$(drush -r ${drupal_root} cvapi MultisiteDomain.create ${domain_args})"
domain_id=$(php -r '$matches = array(); preg_match("#\[id\] => (\d+)#", $argv[1], $matches); echo $matches[1];' "${drush_output}")

echo -e "\nDebugging info...\n"
echo -e "Drush output is:\n${drush_output}\n"
echo -e "Domain ID is:\n${domain_id}\n"

echo "Updating CRM config..."

cp ${abs_callpath}/civicrm.settings.tpl.php ${config_dir}/civicrm.settings.php
sed -i "s#'%CRM_DOMAIN_ID%'#${domain_id}#" ${config_dir}/instance.settings.php

echo "Copying scheduled jobs config..."

INSERT INTO `civicrm_job` (`domain_id`, `run_frequency`, `scheduled_run_date`, `name`, `description`, `api_entity`, `api_action`, `parameters`, `is_active`) 
SELECT ${domain_id}, `run_frequency`, `scheduled_run_date`, `name`, `description`, `api_entity`, `api_action`, `parameters`, `is_active` 
FROM `civicrm_job`
WHERE `domain_id` = 1 ;

echo "Installing cron job..."

    if [ "${multisite_env}" == "prod" ]; then
        cronjob="\"/usr/local/bin/drush -r ${drupal_root} -l ${domain} -u civicrm_cron cvapi job.execute --quiet\""
        sudo sh -c "echo ${cronjob} >> /usr/local/bin/civicrm-cron.sh"
    fi