2015-05-31 13:46:25 +02:00
#!/bin/bash
2015-06-01 00:00:38 +02:00
set -e
# Delete db and user if exit with an error
function exit_properly
{
set +e
root_pwd=$(sudo cat /etc/yunohost/mysql)
mysql -u root -p$root_pwd -e "DROP DATABASE limesurvey ; DROP USER limesurvey@localhost ;"
sudo rm -Rf /var/www/limesurvey
exit 1
}
2015-05-31 13:46:25 +02:00
app="limesurvey"
# Retrieve arguments
domain=$1
path=$2
admin=$3
language=$4
# Check if admin exists
2015-06-01 00:00:38 +02:00
sudo yunohost user list --json | grep -qi "\"username\": \"$admin\"" \
|| (echo "User does not exist: $admin" && exit 1)
2015-05-31 13:46:25 +02:00
sudo yunohost app setting $app admin -v $admin
sudo yunohost app setting $app language -v $language
# Check domain/path availability
2015-06-01 00:00:38 +02:00
sudo yunohost app checkurl $domain$path -a $app \
|| (echo "Path not available: $domain$path" && exit 1)
# Remove trailing "/" for next commands
path=${path%/}
2015-05-31 13:46:25 +02:00
# Generate random password
db_pwd=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d '[A-Za-z0-9]' | sed -n 's/\(.\{24\}\).*/\1/p')
# Use 'opensondage' as database name and user
db_user=$app
# Initialize database and store mysql password for upgrade
sudo yunohost app initdb $db_user -p $db_pwd
sudo yunohost app setting $app mysqlpwd -v $db_pwd
2015-06-01 00:00:38 +02:00
# Delete db and user if exit with an error
trap exit_properly ERR
2015-05-31 13:46:25 +02:00
# Copy files to the right place
final_path=/var/www/$app
2015-06-01 00:00:38 +02:00
upload_path=/home/yunohost.app/$app/upload
2015-05-31 13:46:25 +02:00
sudo mkdir -p $final_path
2015-06-01 00:00:38 +02:00
sudo mkdir -p $upload_path
2015-05-31 13:46:25 +02:00
sudo cp -a ../sources/* $final_path
2015-06-01 00:00:38 +02:00
sudo rm -Rf $final_path/upload
sudo cp -a ../sources/upload/* $upload_path
sudo ln -s $upload_path ../sources/upload
2015-05-31 13:46:25 +02:00
# Db installation
mysql -u $db_user -p$db_pwd $db_user < ../sources/installer/sql/create-mysql.sql
sudo sed -i "s/yunoadmin/$admin/g" ../conf/mysql_data.sql
mysql -u $db_user -p$db_pwd $db_user < ../conf/mysql_data.sql
sudo cp ../conf/config.php.template $final_path/application/config/config.php
sudo sed -i "s/yunouser/$db_user/g" $final_path/application/config/config.php
sudo sed -i "s/yunopass/$db_pwd/g" $final_path/application/config/config.php
sudo sed -i "s/yunobase/$db_user/g" $final_path/application/config/config.php
2015-05-31 22:36:35 +02:00
for user in $(sudo yunohost user list | grep '^ [^ ]*:' | sed -e "s/^ \([^ ]*\):/\1/g")
do
if [ $user != $admin ];
2015-06-01 00:00:38 +02:00
then
mysql -u $db_user -p$db_pwd $db_user -e "INSERT INTO prefix_users (users_name) VALUES ('$user');INSERT INTO prefix_permissions (entity,entity_id,uid,permission,create_p,read_p,update_p,delete_p,import_p,export_p) SELECT 'global',0,uid,'surveys',1,1,1,1,0,1 FROM prefix_users WHERE users_name='$user'"
fi
2015-05-31 22:36:35 +02:00
done
2015-05-31 13:46:25 +02:00
# Set permissions
2015-06-01 00:00:38 +02:00
sudo chown -R www-data:www-data $final_path
sudo chown -R www-data:www-data $upload_path
sudo chmod -R 664 $final_path
sudo find $final_path -type d -print0 | xargs -0 sudo chmod 775 \
|| echo "No file to modify"
sudo chmod -R 664 $upload_path
sudo find $upload_path -type d -print0 | xargs -0 sudo chmod 775 \
|| echo "No file to modify"
2015-05-31 13:46:25 +02:00
sudo chmod u+w $final_path/tmp
2015-06-01 00:00:38 +02:00
sudo chmod u+w $upload_path
2015-05-31 13:46:25 +02:00
sudo chmod u+w $final_path/application/config/
sudo yunohost app addaccess $app -u $admin
# Modify Nginx configuration file and copy it to Nginx conf directory
2015-06-01 00:00:38 +02:00
sudo sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sudo sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf
2015-05-31 13:46:25 +02:00
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
# Reload Nginx and regenerate SSOwat conf
sudo service nginx reload
sudo yunohost app setting $app skipped_uris -v "/"
#sudo yunohost app setting $app protected_uris -v "/index.php?r=admin,/index.php?r=plugins,/scripts"
sudo yunohost app ssowatconf
2015-05-31 22:36:35 +02:00