###
#Declarring vars for testing
domain=Domain.exp
###

#Pre-confiuring Env
webname=$(sed -e 's/\.[a-z]*$//' <<< $domain)
webname=$(sed 's/\./-/g'  <<< $webname)
apt install bindfs -y


#creating SFTP-user 
groupadd $webname
useradd $webname -g $webname
mkdir -p /home/$webname/.ssh
echo "ExampleKey" >> /home/$webname/.ssh/authorized_keys
chown -R $webname:$webname /home/$webname


#creating folder and setting mount
mkdir -p /web/$webname/html
chmod 755 /web
chmod 755 /web/$webname
chown $webname:$webname /web/$webname/html
mkdir -p /var/www/$domain
chown www-data:www-data /var/www/$domain
echo "bindfs#/var/www/$domain /web/$webname/html fuse force-user=${webname},force-group=${webname},create-for-user=www-data,create-for-group=www-data,create-with-perms=0770,chgrp-ignore,chown-ignore,chmod-ignore 0 0" >> /etc/fstab


#Configuring sshd for SFTP only user
sed -i 's/Subsystem\s*sftp\s*\/usr\/lib\/openssh\/sftp-server/Subsystem sftp internal-sftp/g' /etc/ssh/sshd_config
cat <<EOF >> /etc/ssh/sshd_config
#Begin of $webname 
   Match User $webname
   ChrootDirectory /web/$webname
   ForceCommand internal-sftp
   X11Forwarding no
   AllowTcpForwarding no
#End of $webname 
EOF
service sshd restart

mount -a