# Retrieve arguments
domain=$1
path=$2
final_path=/var/www/seafile
seafile_version=3.1.1

# Check domain/path availability
sudo yunohost app checkurl $domain$path -a seafile
if [[ ! $? -eq 0 ]]; then
    exit 1
fi

# Check dependencies
sudo apt-get install -y python2.7 python-setuptools python-simplejson python-imaging sqlite3

# Copy files to the right place
sudo mkdir -p $final_path
sudo cp -R ../sources/* $final_path/

# Set permissions to seafile directory
#sudo chown -R www-data: $final_path
sudo chmod +x $final_path/seafile-server-$seafile_version/seafile.sh

# Change configuration
# Port
# Domain
# Admin account

# Add Seafile Server to startup
# TODO

# Modify Nginx configuration file and copy it to Nginx conf directory
sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/seafile.conf

# Start Seafile Server
cd $final_path/seafile-server-*
sudo ./seafile.sh start
sudo ./seahub.sh start

# Reload Nginx and regenerate SSOwat conf
sudo service nginx reload
sudo yunohost app ssowatconf
