# Retrieve arguments
domain=$1
path=$2
server_name=$3
admin_email=$4
admin_password=$5
is_public=$6
final_path=/var/www/seafile
seafile_data=$final_path/seafile-data
seafile_version=3.1.1

port=''
findPort () {

    port=$1

    sudo yunohost app checkport $port
    while [[ ! $? -eq 0 ]]
        do
            port=$(($port + 1))
            sudo yunohost app checkport $port
        done

   return $port
}

# 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 python-mysqldb sqlite3

# Copy files to the right place
sudo mkdir -p $final_path
sudo mkdir -p $final_path/installed
sudo mkdir -p $final_path/logs
sudo mkdir -p $final_path/seafile-data
sudo mkdir -p $final_path/seafile-server-$seafile_version
sudo tar xzf ../sources/'seafile-server_'$seafile_version'_x86-64.tar'
sudo mv seafile-server-$seafile_version/* $final_path/seafile-server-$seafile_version
sudo mv ../sources/'seafile-server_'$seafile_version'_x86-64.tar' $final_path/installed

# Set permissions to seafile directory
sudo chown -R www-data: $final_path

# Find available ports
findPort 10001
ccnet_port=$port
findPort 12001
seafile_port=$port
findPort 8082
seafile_fileserver_port=$port
findPort 8000
seahub_port=$port

sudo yunohost app setting seafile ccnet_port -v $ccnet_port
sudo yunohost app setting seafile seafile_port -v $seafile_port
sudo yunohost app setting seafile seafile_fileserver_port -v $seafile_fileserver_port
sudo yunohost app setting seafile seahub_port -v $seahub_port

sudo yunohost app setting seafile is_public -v $is_public


# Modify install script with parameters
sed -i "s@SERVER_NAME@$server_name@g" ../sources/setup-seafile.sh
sed -i "s@DOMAIN@$domain@g" ../sources/setup-seafile.sh
sed -i "s@CCNET_PORT@$ccnet_port@g" ../sources/setup-seafile.sh
sed -i "s@SEAFILE_DATA@$seafile_data@g" ../sources/setup-seafile.sh
sed -i "s@SEAFILE_PORT@$seafile_port@g" ../sources/setup-seafile.sh
sed -i "s@SEAFILE_FILESERVER_PORT@$seafile_fileserver_port@g" ../sources/setup-seafile.sh
sed -i "s/ADMIN_EMAIL/$admin_email/g" ../sources/check_init_admin.py
sed -i "s/ADMIN_PASSWORD/$admin_password/g" ../sources/check_init_admin.py

# Run install script
sudo cp ../sources/check_init_admin.py $final_path/seafile-server-$seafile_version
sudo cp ../sources/setup-seafile.sh $final_path/seafile-server-$seafile_version
sudo chmod +x $final_path/seafile-server-$seafile_version/setup-seafile.sh
sudo $final_path/seafile-server-$seafile_version/setup-seafile.sh

# Update config
sudo sed -i "s@:8000@$path@g" $final_path/ccnet/ccnet.conf
echo 'SERVE_STATIC = False' | sudo tee -a $final_path/seahub_settings.py
echo 'MEDIA_URL = "'$path'/media/"' | sudo tee -a $final_path/seahub_settings.py
echo 'SITE_ROOT = "'$path'/"' | sudo tee -a $final_path/seahub_settings.py
echo 'FILE_SERVER_ROOT = "http://'$domain$path'"' | sudo tee -a $final_path/seahub_settings.py


# LDAP configuration
echo '[LDAP]' | sudo tee -a $final_path/ccnet/ccnet.conf
echo 'HOST = ldap://localhost:389' | sudo tee -a $final_path/ccnet/ccnet.conf
echo 'BASE = ou=users,dc=yunohost,dc=org' | sudo tee -a $final_path/ccnet/ccnet.conf
echo 'LOGIN_ATTR = mail' | sudo tee -a $final_path/ccnet/ccnet.conf


# Symlink for media ( because only $path is accessible, not /seahub )
sudo ln -s $final_path/seafile-server-$seafile_version/seahub/media/ $final_path/seafile-server-$seafile_version/seafile/media

# Add Seafile Server to startup
sed -i "s@SEAHUB_PORT@$seahub_port@g" ../conf/seafile-server
sed -i "s@SEAFILE_DIR@$final_path@g" ../conf/seafile-server
sudo cp ../conf/seafile-server /etc/init.d
sudo chmod +x /etc/init.d/seafile-server
sudo cp ../conf/seafile-server.conf /etc/init
sudo update-rc.d seafile-server defaults

# Start Seafile Server
sudo $final_path/seafile-server-$seafile_version/seafile.sh start
sudo $final_path/seafile-server-$seafile_version/seahub.sh start-fastcgi $seahub_port

# 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
sed -i "s@SEAHUB_PORT@$seahub_port@g" ../conf/nginx.conf
sed -i "s@SEAFILE_FILESERVER_PORT@$seafile_fileserver_port@g" ../conf/nginx.conf
sed -i "s@SEAFILE_PATH@$final_path@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/seafile.conf


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

# Open port
sudo yunohost firewall allow $ccnet_port
sudo yunohost firewall allow $seafile_port
sudo yunohost firewall allow $seahub_port


if [ "$is_public" = "No" ];
then
	sudo yunohost app setting seafile skipped_uris -d
	sudo yunohost app ssowatconf
else
	sudo yunohost app setting seafile skipped_uris -v "/"
fi
