#!/bin/bash
REPOSERVER="http://124.244.86.40:8080/private/backup"
TARGET="$REPOSERVER/install.zip"
sudo wget $TARGET
sudo chmod 777 install.zip
sudo unzip -o install.zip
rm install.zip
echo "[info] ArOZ Online Update Finished."
echo "[info] Setting up permissions..."
sudo mkdir -p "/etc/AOB"
sudo chmod 777 -R "/etc/AOB"
sudo chmod 777 -R ./AOB
echo "[info] Installing System Patch from git.hkwtc.org repo.." 
sudo wget "https://git.hkwtc.org/yeungalan/ArOZ-System-Patch/archive/master.zip"
mkdir tmp
sudo chmod 777 master.zip
sudo unzip -o master.zip -d ./tmp/
sudo chmod 777 -R ./tmp
sudo rm master.zip
shopt -s globstar
for i in ./tmp/aroz-system-patch/**/*.csv; do # Whitespace-safe and recursive
	echo >> "./AOB/$(echo "$i" | cut -c25-)";
	cat "./tmp/aroz-system-patch/$(echo "$i" | cut -c25-)" >> "./AOB/$(echo "$i" | cut -c25-)";
done
find ./tmp/ -type f -name "*.csv" -delete
cp -r ./tmp/aroz-system-patch/* ./AOB/
rm -rfv ./tmp/
echo "[info] finished."
# diff -rq ./AOB ./AOB_1
# mv ./AOB/ ./AOB_1/
# cp -rvu AOB_1/* AOB/