###WARNING:###
#this system does not do version management an also syncs deletions from remote mailserver
#make sure version managent and/or backups and/or snapshots are setup!!
###

#for proxmox CT users please read inst-prxct first
#clone this project

#Apt list
apt install -y dovecot-imapd nginx php7.3-xml php7.3-fpm php7.3-curl offlineimap sudo
#place all configfiles to the repective location
#dirs and perms
mkdir /mail
chmod 777 /mail
chown www-data:www-data -R /var/www
touch /opt/mailsync.sh
#restart services or the system
systemctl enable nginx dovecot
systemctl stop nginx dovecot
systemctl start nginx dovecot
#Setup cronjobs

# Explenation of job definition:
#     .---------------- minute (0 - 59)
#     |  .------------- hour (0 - 23)
#     |  |  .---------- day of month (1 - 31)
#     |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
#     |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
#     |  |  |  |  |
#     *  *  *  *  * 
echo '30 2   * *  4   root   bash /opt/mailsync.sh >/dev/null 2>&1' >> /etc/crontab

#add user/sync job
adduser <localname>
#replace <username> with the the chosen localname and update the remote IMAP settings
#for ease to acces edit /var/www/index.html and add the new credentials
nano /home/<localname>/.offlineimaprc
echo "sudo -u <localname> offlineimap" >> /opt/mailsync.sh
