#
# This 'mk.besync'-script creates a bectl(8) create/destroy wrapper named 'besync' and it's database.
# It also creates a pkg-upgrade(8) wrapper, named 'update', which integrates 'besync'.
# Finally 'mk.besync' backs up all scripts to /root/.besync/ .
#
# 'besync' will execute rolling BE-snapshots with a destroy-offset=2 and unix-timestamps.
# The 'besync'-database will be located in /root/.besync/db/ .
#
# 'update' will execute a pkg-upgrade(8) dry run to check out at first and then if it gets confirmed,
# it continues to execute 'besync', pkg-upgrade(8) and pkg-autoremove(8).
# To exit after dry run use Ctrl+C.
#
# See also bectl(8), pkg-upgrade(8), pkg-autoremove(8).
#
# files: /root/.besync/
#
# file permissions: rwxr-x---
#

#!/bin/sh

# # build initial besync database & create boot environments
echo 'initializing besync-db...'
mkdir -p /root/.besync/db/ &&
chmod 750 /root/.besync/db/
chmod 750 /root/.besync/
STAMP=$(date +%s)
bectl create -r $STAMP
echo $STAMP >/root/.besync/db/0
chmod 640 /root/.besync/db/0
sleep 1
STAMP=$(date +%s)
bectl create -r $STAMP
echo $STAMP >/root/.besync/db/1
chmod 640 /root/.besync/db/1
sleep 1
STAMP=$(date +%s)
bectl create -r $STAMP
echo $STAMP >/root/.besync/db/2
chmod 640 /root/.besync/db/2

# # build besync script
echo 'building besync...'
echo '#' >besync
echo '# 'besync' will execute rolling BE-snapshots with a destroy-offset=2 and unix-timestamps.' >>besync
echo '# The 'besync'-database will be located in /root/.besync/db/ .' >>besync
echo '#' >>besync
echo '# See also bectl(8).' >>besync
echo '#' >>besync
echo '# files: /root/.besync/' >>besync
echo '#' >>besync
echo '# file permissions: rwxr-x---' >>besync
echo '#' >>besync
echo '#!/bin/sh' >>besync
echo >>besync
echo '# # update boot environments' >>besync
echo 'STAMP=$(date +%s)' >>besync
echo 'bectl create -r $STAMP &&' >>besync
echo 'bectl destroy $(cat /root/.besync/db/0)' >>besync
echo >>besync
echo '# # sync besync database' >>besync
echo 'cat /root/.besync/db/1 >/root/.besync/db/0' >>besync
echo 'cat /root/.besync/db/2 >/root/.besync/db/1' >>besync
echo 'echo $STAMP >/root/.besync/db/2' >>besync
chmod 750 besync

# # build update script with integrated besync
echo 'building update...'
echo '#' > update
echo '# 'update' will execute a pkg-upgrade(8) dry run to check out at first and then if it gets confirmed,' >>update
echo '# it continues to execute 'besync', pkg-upgrade(8) and pkg-autoremove(8). To exit use Ctrl+C' >>update
echo '#' >>update
echo '# See also bectl(8), pkg-upgrade(8), pkg-autoremove(8).' >>update
echo '#' >>update
echo '# files: /root/.besync/' >>update
echo '#' >>update
echo '# file permissions: rwxr-x---' >>update
echo '#' >>update
echo '#!/bin/sh' >>update
echo >>update
echo '# # upgrade check' >>update
echo 'pkg upgrade -n' >>update
echo 'echo "# Continue to BE-snapshot & OS-update?"' >>update
echo 'read -p "# Continue=< ENTER > || Exit=< ^C >:" INPUT || exit' >>update
echo >>update
echo '# # besync && upgrade' >>update
echo '# update boot environments' >>update
echo 'STAMP=$(date +%s)' >>update
echo 'bectl create -r $STAMP &&' >>update
echo 'bectl destroy $(cat /root/.besync/db/0)' >>update
echo '# sync besync database' >>update
echo 'cat /root/.besync/db/1 >/root/.besync/db/0' >>update
echo 'cat /root/.besync/db/2 >/root/.besync/db/1' >>update
echo 'echo $STAMP >/root/.besync/db/2' >>update
echo '# upgrade' >>update
echo 'pkg upgrade -y &&' >>update
echo 'pkg autoremove -y' >>update
chmod 750 update

# # backup
echo 'backing up scripts to /root/.besync/ ...'
cp besync /root/.besync/
cp update /root/.besync/
echo 'done'
