#!/bin/sh
tce-load -wil squashfs-tools

rm -rf build
sudo rm -rf /tmp/https-mirrors

mkdir build

sudo mkdir -p /tmp/https-mirrors/usr/local/https-mirrors
sudo mkdir -p /tmp/https-mirrors/usr/local/tce.installed
sudo mkdir -p /tmp/https-mirrors/usr/local/share/doc/https-mirrors

sudo cp COPYING                 /tmp/https-mirrors/usr/local/share/doc/https-mirrors/COPYING
sudo cp https-mirrors.patch     /tmp/https-mirrors/usr/local/https-mirrors/https-mirrors.patch
sudo cp https-mirrors-pi.patch  /tmp/https-mirrors/usr/local/https-mirrors/https-mirrors-pi.patch
sudo cp https-mirrors.installed /tmp/https-mirrors/usr/local/tce.installed/https-mirrors

sudo chown root:staff \
	/tmp/https-mirrors/usr/local/tce.installed \
	/tmp/https-mirrors/usr/local/tce.installed/https-mirrors

sudo chmod g+w \
	/tmp/https-mirrors/usr/local/tce.installed \
	/tmp/https-mirrors/usr/local/tce.installed/https-mirrors

mksquashfs /tmp/https-mirrors build/https-mirrors.tcz
cp https-mirrors.tcz.info build/https-mirrors.tcz.info

echo ca-certificates.tcz  >build/https-mirrors.tcz.dep
echo mirrors.tcz         >>build/https-mirrors.tcz.dep

tar czf build/https-mirrors-source.tgz -X build -X .gitignore *

if [ "$1" == "install" ]; then
	rm -f /etc/sysconfig/tcedir/optional/https-mirrors.tcz
	cp build/https-mirrors.tcz* -t /etc/sysconfig/tcedir/optional
	echo https-mirrors.tcz >>/etc/sysconfig/tcedir/onboot.lst
	tce-load -i https-mirrors.tcz
fi
