#!/bin/bash

RSYNC_TMP_DIR='/tmp/rsync_latest'

# MARK: Safety for not existing mounting ${RSYNC_TMP_DIR} directory
if [ ! -d "${RSYNC_TMP_DIR}" ]; then
        mkdir -p "${RSYNC_TMP_DIR}"
        echo "Directory '$RSYNC_TMP_DIR' created."
fi
echo "" && \
echo "========== Startng install rsync with latest version...  ==========" && \
cd "${RSYNC_TMP_DIR}" && \
echo "" && \
echo "(1/8) Checking current rsync version..." && \
echo "" && \
rsync -v &> 'rsync.version.0' ; head -n 3 'rsync.version.0' && \
echo "" && \
echo "" && \
echo "(1/8) Installing build dependencies..." && \
apt install -y gcc g++ gawk autoconf automake python3-cmarkgfm && \
apt install -y acl libacl1-dev && \
apt install -y attr libattr1-dev && \
apt install -y libxxhash-dev && \
apt install -y libzstd-dev && \
apt install -y liblz4-dev && \
apt install -y libssl-dev && \
echo "" && \
echo "" && \
echo "(2/8) Cloning rsync git repository... (Downloading latest rsync source codes...)" && \
git clone --depth 1 https://github.com/RsyncProject/rsync.git && \
cd rsync && \
echo "" && \
echo "" && \
echo "(3/8) Configuring sources... ('./configure')" && \
./configure && \
echo "" && \
echo "" && \
echo "(4/8) Makeing binary files with configured options above... ('make')" && \
make && \
echo "" && \
echo "" && \
echo "(5/8) Installing made binary on previous step... ('make installl')" && \
make install && \
echo "" && \
echo "" && \
echo "(7/8) Checking Installed rsync version..."
echo "" && \
rsync -v &> 'rsync.version.1' ; head -n 3 'rsync.version.1' && \
echo "" && \
echo "" && \
echo "(8/8) Diff version difference... (If it nothing displayed, it means the version haven't changed)" && \
echo "" && \
diff 'rsync.version.0' 'rsync.version.1' && \
echo "" && \
echo "========== Successfully installed latest rsync! ==========" && \
echo "" && \
echo "" && \
rm -rf "${RSYNC_TMP_DIR}"
