#!/bin/bash
#Vars

#Goto ProjectRoot
cd "$( cd "$( dirname "$0" )" &> /dev/null && pwd )/.."

echo "The script will now install required dependencies"
read -r -s -p $'Press enter to continue, or ctrl+c to quit'
#Install software dependencies
apt install ca-certificates golang git make debootstrap curl gcc libc-dev nano gnupg2 rsync xz-utils --no-install-recommends -y


#Get Source for DistroBuilder
git clone https://github.com/lxc/distrobuilder distrobuilderSRC
cd distrobuilderSRC
#Run Build for Build
gofmt -s -w .
go build -o ../ -v ./...
#Cleanup
cd ../
rm -rf distrobuilderSRC

#Disable use of cache Server
bash Scripts/UpdateIMGBuildFile-CacheAndRepo.sh -d
#Allow XZ to use all cores
export XZ_DEFAULTS="-T 0"
#Function for building LXC images
BuildImage () {
    local Distro=$1
    local Variant=$2
    ./distrobuilder build-lxc CT-Build/$Distro.yaml  -o image.variant=$Variant
    rm -f meta.tar.xz
    mv ./rootfs.tar.xz "$Distro-$Variant".tar.xz
}

#Build Jenkins Image
BuildImage Debian jenkins
#Build JenkinsNode (imgbuilder) Image
BuildImage Debian imgbuilder

#Get resources 
bash Scripts/GetExternalResources.sh