#! /bin/bash
set -e
home=$(pwd)
rm -vf $home/sysroot.tar.bz2
apt-get update --allow-releaseinfo-change && apt-get install -y rsync
mkdir /raspbian
rsync -a $home/raspbian /

rm /raspbian/etc/ld.so.preload
cp -vf /etc/resolv.conf /raspbian/etc/resolv.conf
chroot /raspbian /bin/bash -c "date && cat /etc/resolv.conf && ping 8.8.8.8 -c 4 && ping archive.raspberrypi.org -c 4"
chroot /raspbian /bin/bash -c "apt-get update --allow-releaseinfo-change && apt-get upgrade -y && apt-get install -y libicu-dev zlib1g-dev libglew-dev libffi-dev libssl-dev libmpc-dev libmpfr-dev libgmp-dev lzma-dev liblzma-dev libxml2-dev libbz2-dev zip \"^libxcb.*\" libx11-xcb-dev libxkbcommon-x11-dev libglu1-mesa-dev libxrender-dev mariadb-client mariadb-common mysql-common libmariadb-dev-compat libmariadbclient-dev libpq-dev ninja-build"

cd /raspbian
bash $home/fix_links.sh
tar -cjf /soft/rpi_image/sysroot.tar.bz2 lib/arm-linux-gnueabihf usr/lib usr/include usr/share var/lib/dpkg/ etc/*-release
chmod 777 /soft/rpi_image/sysroot.tar.bz2
