#!/bin/sh -Ceux


set -Ceux


cd /root/files/fbsd/embedded || exit 1


branch=current
dist=univ-min.working

rsync_dist=1
rsync_overlay=1
rsync_usr_local=1
rsync_poe_daemon=1

opt=


case "$@"
in
	*current*) branch=current ;;
	*release*) branch=release ;;
esac

case "$@"
in
	*univ-min.working*) dist=univ-min.working ;;
	*univ-min*)         dist=univ-min         ;;
	*univ.working*)     dist=univ.working     ;;
	*univ*)             dist=univ             ;;
esac

case "$@"
in
	*dry-run*) opt=--dry-run ;;
esac


case "$branch"
in
	release) rsync_usr_local= ;;
esac


dist_dir="/root/files/fbsd/$branch/dist/$dist"
overlay_dir=/root/files/fbsd/embedded/overlays/npinc/root
usr_local_dir=/root/files/qemus/yellowgreen/usr/local
poe_daemon_dir=/home/ketas/files/bbb/poe-daemon

efiloader_src="/root/files/fbsd/$branch/dist/$dist/boot/loader.efi"


overlay=
efiloader_dest=
for overlay in npinc npinc-sd-standalone
do
	efiloader_dest="overlays/$overlay/boot/efi/boot/bootarm.efi"
	
	
	if [ ! -e "$efiloader_dest" -o "$efiloader_dest" -ot "$efiloader_src" ]
	then
		cp -av "$efiloader_src" "$efiloader_dest"
	fi
done
unset overlay efiloader_dest


[ "$rsync_dist" ] \
&& rsync $opt -avhhhiHAXUS \
--delete --delete-excluded \
--exclude=.cshrc \
--exclude=dot.cshrc \
--exclude=debug \
--exclude=examples \
--exclude=man \
--exclude=include \
--exclude=doc \
--exclude=dtrace \
--exclude=clang \
--exclude=games \
--exclude=mk \
--exclude=snmp \
--exclude=zfs \
--exclude=zoneinfo \
--exclude=vi \
--exclude=sendmail \
--exclude=bsdconfig \
--exclude=bsdinstall \
--exclude=dwatch \
--exclude=periodic \
--exclude=c++ \
--exclude=cc \
--exclude=clang-cpp \
--exclude=clang++ \
--exclude=cpp \
--exclude=ld.lld \
--exclude=objdump \
--exclude=gcov \
--exclude=dict \
--exclude=kernel.bin \
--exclude=c++filt \
--exclude=misc/pci_vendors \
'--exclude=var/*' \
'--exclude=llvm*' \
'--exclude=locale/zh*' \
'--exclude=locale/ko*' \
'--exclude=locale/ja*' \
'--exclude=*zfs*' \
'--exclude=*zpool*' \
'--exclude=*snmp*' \
'--exclude=*ftp*' \
'--exclude=*unbound*' \
'--exclude=*lpr*' \
'--exclude=*ppp*' \
'--exclude=*bootp*' \
'--exclude=*authpf*' \
'--exclude=*ngatm*' \
'--exclude=ng_*.ko' \
'--exclude=*sql*' \
'--exclude=*dtrace*' \
'--exclude=misc/magic*' \
'--exclude=misc/usb*' \
'--exclude=misc/scsi*' \
"$dist_dir/" \
rootfs/


[ "$rsync_overlay" ] \
&& rsync $opt -avhhhiHAXUS \
"$overlay_dir/" \
rootfs/


[ "$rsync_usr_local" ] \
&& rsync $opt -avhhhiHAXUS \
--exclude=python3.9 \
--exclude=ruby \
--exclude=man \
--exclude=doc \
--exclude=examples \
--exclude=info \
--exclude=licenses \
--exclude=locale \
--exclude=ri \
--exclude=include \
--exclude=pod \
--exclude=db5 \
--exclude=pkgconfig \
--exclude=readline \
--exclude=sgml \
--exclude=xml \
--exclude=cmake \
--exclude=rdoc \
--exclude=Test \
--exclude=Test2 \
--exclude=Pod \
--exclude=CPAN \
--exclude=TAP \
--exclude=bin/omshell \
--exclude=bin/dwp \
--exclude=bin/strip \
--exclude=bin/strings \
--exclude=bin/size \
--exclude=bin/rhash \
--exclude=lib/compat \
--exclude=bin/as \
--exclude=bin/gm4 \
'--exclude=*ruby*' \
'--exclude=*python*' \
'--exclude=*gettext*' \
'--exclude=*texinfo*' \
'--exclude=*periodic*' \
'--exclude=share/auto*' \
'--exclude=share/aclocal*' \
'--exclude=share/*tool*' \
'--exclude=share/*xsl*' \
'--exclude=share/*lisp*' \
'--exclude=share/*doc*' \
'--exclude=share/*gpg*' \
'--exclude=share/zsh*' \
'--exclude=share/bash*' \
'--exclude=share/polkit*' \
'--exclude=share/tex*' \
'--exclude=*bin/*pkg*' \
'--exclude=lib/*pkg*' \
'--exclude=bin/aclocal*' \
'--exclude=bin/auto*' \
'--exclude=bin/*make*' \
'--exclude=bin/*tool*' \
'--exclude=sbin/port*' \
'--exclude=bin/db_*' \
'--exclude=etc/pkg*' \
'--exclude=*bin/*zstd*' \
'--exclude=*bin/*zip*' \
'--exclude=*bin/yat*' \
'--exclude=*bin/xxh*' \
'--exclude=*bin/xs*' \
'--exclude=*bin/xml*' \
'--exclude=*bin/wheel*' \
'--exclude=*bin/*lz4*' \
'--exclude=*bin/*tex*' \
'--exclude=*bin/recode*' \
'--exclude=*bin/py*' \
'--exclude=*bin/pod*' \
'--exclude=*bin/pdf*' \
'--exclude=*bin/msg*' \
'--exclude=/.*' \
'--exclude=Module/Build*' \
'--exclude=Text/Unidecode*' \
'--exclude=XML/Parser*' \
'--exclude=Locale/RecodeData*' \
'--exclude=Unicode/Collate*' \
'--exclude=*-portbld-freebsd*.*' \
'--exclude=bin/c????' \
'--exclude=bin/ld*' \
'--exclude=bin/*-hash' \
'--exclude=lib/libopcodes*' \
'--exclude=lib/libbfd*' \
'--exclude=bin/sha*' \
'--exclude=bin/*sum' \
'--exclude=bin/*elf*' \
'--exclude=bin/*3.9' \
'--exclude=bin/ptar*' \
'--exclude=bin/hmac*' \
'--exclude=bin/*png*' \
'--exclude=bin/*2??' \
'--exclude=lib/**perl**.ph' \
'--exclude=lib/**perl**.h' \
'--exclude=bin/obj*' \
"$usr_local_dir/" \
rootfs/usr/local/


[ "$rsync_poe_daemon" ] \
&& rsync $opt -avhhhiHAXUS \
--no-owner \
--no-group \
--exclude=.git \
--exclude=httpd-static \
--exclude=check-js-func-order.sh \
--exclude=bbb-gpio-map.pl \
--exclude=README \
--exclude=.gitignore \
--exclude=ssl/client.crt \
--exclude=ssl/client.key \
'--exclude=*local-perl-module-patches' \
'--exclude=*test*.sh' \
'--exclude=*init*.sh' \
'--exclude=*.xz' \
'--exclude=ssl/server.*' \
'--exclude=config-*.pl' \
'--exclude=*.tpl' \
'--exclude=*.core' \
"$poe_daemon_dir/" \
rootfs/root/poe-daemon/


date +%s > rootfs/root/last-updated.txt


du -hAxs rootfs/
