#
# MailWasher Enterprise Server for Linux Installation Script 3.0
#
# Description: Performs the installation of the MWES daemon and binaries
# copyright: Firetrust Limited 2015
# Contact: sales@firetrust.com
# Website: www.firetrust.com
#

# check we're running as root
if [ `id -u` != 0 ]; then 
 echo "Error: Installation must be performed as root user"
 exit 1
fi

# Variables/locations
USER=mwes
GROUP=mwes
DEST=/opt/mwes
LOGS=/var/log
BIN=/sbin
RUN=/var/run
INIT=/etc/init.d
SYSTEMD=/etc/systemd/system

# check if user exists, creating if it doesnt

groupadd $GROUP
useradd $USER -g $GROUP -c "MailWasher Enterprise Server"


# Create the mwes directory, adding requires files, arrange permissions
if [ ! -d $DEST ]
  then 
    mkdir -p $DEST
fi 
cp -ur mwes/ mwes-new/ fonts/ CustomSpamFilter.xml libchartdir.so.6.0 libcrypto.so.1.1 libssl.so.1.1 mail-mwes.sh mwes.run uninstall $DEST 
chown -R $USER:$GROUP $DEST
chmod 6755 $DEST/mwes.run


# add the start/stop daemon to the /sbin dir
#cp -u start-stop-daemon /sbin
#chown root:root $BIN/start-stop-daemon
#chmod 700 $BIN/start-stop-daemon

#create the logs dir
if [ ! -d $LOGS/mwes ]
  then 
    mkdir -p $LOGS/mwes
    chown $USER:$GROUP $LOGS/mwes
fi

# create run directory
if [ ! -d $RUN/mwes ]
  then 
    mkdir $RUN/mwes
    chown $USER:$GROUP $RUN/mwes
fi

# cp config file to /etc
cp -u mwes.conf /etc
chown root:root /etc/mwes.conf

# cp init file to /etc/init.d
cp -u mwesd $INIT
chown root:root $INIT/mwesd
chmod 755 $INIT/mwesd

# cp systemd file to /etc/systemd/system
cp -u mwes.service $SYSTEMD
chown root:root $SYSTEMD/mwes.service
chmod 755 $SYSTEMD/mwes.service

# completed
echo "Installation completed"
echo "Note: To control the {init.d} service execute /etc/init.d/mwesd {start|stop|restart|status}"
echo "Note: To control the {systemd} service execute sudo systemctl {enable|disable|start|stop|restart|status} mwes"
