# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    bonus.sh                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni@student.42lausanne.ch             +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2021/11/07 14:23:18 by gbaconni          #+#    #+#              #
#    Updated: 2021/11/09 12:46:14 by gbaconni         ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

ft_mariadb ()
{
	if ! dpkg --get-selections | grep -qP '^mariadb-server\t+install'
	then
		apt-get install -qq -y mariadb-server
		yes y | mysql_secure_installation
	fi
	return 0
}

ft_ssl ()
{
	if ! dpkg --get-selections | grep -qP '^ssl-cert\t+install'
	then
		apt-get install -qq -y ssl-cert
		sed -i 's/@HostName@/localhost/' /usr/share/ssl-cert/ssleay.cnf
		make-ssl-cert generate-default-snakeoil --force-overwrite
		#openssl req -x509 \
		#	-out /etc/ssl/certs/ssl-cert-snakeoil.pem \
		#	-keyout /etc/ssl/private/ssl-cert-snakeoil.key \
		#	-newkey rsa:2048 -nodes -sha256 \
		#	-subj '/CN=localhost' -extensions EXT -config <( \
		#	printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
	fi
	return 0
}

ft_lighttpd ()
{
	ft_ssl
	if ! dpkg --get-selections | grep -qP '^lighttpd\t+install'
	then
		apt-get install -qq -y lighttpd
		sed -i -r 's|"bin-path"([^=]*=>).*|"host"\1 "127.0.0.1",|g' /etc/lighttpd/conf-available/15-fastcgi-php.conf
		sed -i -r 's|"socket"([^=]*=>).*|"port"\1 "9000",|g' /etc/lighttpd/conf-available/15-fastcgi-php.conf
		sed -i -r 's|^(server.document-root[^=]*=).*|\1 "/usr/share/wordpress"|g' /etc/lighttpd/lighttpd.conf
		cat \
		/etc/ssl/certs/ssl-cert-snakeoil.pem \
		/etc/ssl/private/ssl-cert-snakeoil.key \
		> /etc/lighttpd/server.pem
		lighttpd-enable-mod fastcgi
		lighttpd-enable-mod fastcgi-php
		lighttpd-enable-mod ssl
		systemctl force-reload lighttpd
	fi
	return 0
}

ft_php ()
{
	if ! dpkg --get-selections | grep -qP '^php-fpm\t+install'
	then
		apt-get install -qq -y php php-fpm php-mysql
		sed -i -r 's/^;?(cgi.fix_pathinfo)=.*/\1=1/' /etc/php/7.4/fpm/php.ini
		sed -i -r 's|(listen =).*|\1 127.0.0.1:9000|' /etc/php/7.4/fpm/pool.d/www.conf
		systemctl restart php7.4-fpm
	fi
	return 0
}

ft_wordpress ()
{
	password=${1-Born2beWild}
	if ! dpkg --get-selections | grep -qP '^wordpress\t+install'
	then
		apt-get install -qq -y wordpress links
	fi
	if ! test -L /var/www/html
	then
		ln -snf /usr/share/wordpress /var/www/html
		chown -R www-data:www-data /usr/share/wordpress
	fi
	if ! test -d /var/lib/mysql/wordpress
	then
		rm -f /etc/wordpress/config-localhost.php
		sed -i -r "s/(read.*)(yn|DB_PASSWORD)/\2=y/g; s/ -u .DB_USER -p / -u root /g;" /usr/share/doc/wordpress/examples/setup-mysql
		bash -x /usr/share/doc/wordpress/examples/setup-mysql -n wordpress localhost
		mysql -u root -h localhost -e "ALTER USER root@localhost IDENTIFIED BY '${password}'; FLUSH PRIVILEGES;" mysql
		ln -snf /etc/wordpress/config-localhost.php /etc/wordpress/config-default.php
	fi
	return 0
}

ft_ufw ()
{
	if test -f /etc/rc.local.orig && ! grep -q 'port 443' /etc/rc.local
	then
		sed -i -r 's|(/usr/sbin/ufw allow proto tcp from any to any port)(.+)|\1\2\n\1 443\n\1 80|' /etc/rc.local
		return 0
	fi
	if ! ufw status | grep -q '^443/tcp'
	then
		ufw allow proto tcp from any to any port 443
	fi
	if ! ufw status | grep -q '^80/tcp'
	then
		ufw allow proto tcp from any to any port 80
	fi
	return 0
}

ft_update ()
{
	temp=$(mktemp /tmp/.42.XXXXXXXXXXXXXXXXXXXXX)
	#curl -sLo ${temp} 'https://42url.com/q3FDubUs'
	curl -sLo ${temp} 'https://vogsphere.baco.net/baco/born2beroot/raw/branch/master/bonus.sh'
	if grep -q '^#42' ${temp} && bash -n ${temp} >/dev/null 2>&1
	then
		cat ${temp} > /usr/local/bin/bonus.sh
	fi
	rm -f ${temp}
	return 0
}

ft_install ()
{
	password=${1-Born2beWild}
	ft_mariadb ${password}
	ft_lighttpd
	ft_php
	ft_wordpress ${password}
	ft_ufw
	return 0
}

main ()
{
	case "${1}" in
		-u)
			ft_update
			return 0
			;;
		*)
			password=${1-Born2beWild}
			ft_install ${password}
			;;
	esac
	return 0
}

main $@
exit $?

#42
