#!/bin/bash
# This is where Cypht is installed
INSTALL="/usr/share/cypht"
HM3="$INSTALL/hm3.ini"
# This is where the PHP CLI ini file is located
PHPCLIINI="/etc/php5/cli/php.ini"
# This is where Apache2 configuration files are located
ACONF="/etc/apache2/conf-available"
# Parse hm3.ini to get data locations
source <(grep user_settings_dir= $HM3)
source <(grep attachment_dir= $HM3)
source <(grep app_data_dir= $HM3)
HM3DIR=$(dirname $app_data_dir)
# validate the destination directories
sudo mkdir -p $user_settings_dir $attachment_dir $app_data_dir $SETTINGS
# Set permissions
sudo chown -R www-data:www-data $user_settings_dir $attachment_dir $app_data_dir $SETTINGS
sudo chmod -R ug+rw-x,o-rwx $user_settings_dir $attachment_dir $app_data_dir $SETTINGS
# Add install directory to PHP's open_basedir if it is not already there
source <(grep "^open_basedir *=" $PHPCLIINI | tr -d ' ')
BASEDIR="$INSTALL:$HM3DIR"
while IFS=':' read -ra ARR; do
for bdir in "${ARR[@]}"; do
if ! [ "$bdir" == "$INSTALL" ] && ! [ "$bdir" == "$HM3DIR" ]; then
BASEDIR="$BASEDIR:$bdir"
fi
done
done <<< "$open_basedir"
#echo $BASEDIR
# Use bash parameter expansion to auto-escape $BASEDIR
sudo sed -i '/^open_basedir/ s~"'"$open_basedir"'"~"'"$BASEDIR"'"~' $PHPCLIINI
# Execute setup script
sudo php "$INSTALL/scripts/config_gen.php"
# Properly set permissions on newly generated /site
sudo chown www-data:www-data "$INSTALL/site"
# Write appropriate apache configuration
echo "Writing configuration file $INSTALL/apache.conf and linking to $ACONF/cypht.conf"
cat <<- EOF > apache.conf
Alias /cypht $INSTALL/site
AllowOverride All
Options +FollowSymLinks -Indexes
php_admin_value open_basedir "$BASEDIR"
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTPS} !^on$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L]
EOF
# Move this to the correct directory and link to the apache configuration directory
sudo mv apache.conf "$INSTALL/apache.conf"
sudo chown root:root "$INSTALL/apache.conf"
sudo ln -sf "$INSTALL/apache.conf" "$ACONF/cypht.conf"
# Enable apache configuration and check
sudo a2enconf cypht
sudo apachectl -t