#!/bin/sh
#open a terminal inside folder 'dunfell' and run this script.
#it will create folder '../oe-quirky' and populate it.
#200921 $BUILDSPATH absolute variable target path.

BUILDSPATH='/mnt/sda1/nvme/oe-builds' ###SET THIS###
[ ! -d $BUILDSPATH ] && exit

if [ -d ${BUILDSPATH}/oe-quirky ];then
 echo "Error, 'oe-quirky' already exists."
 exit
fi
if [ ! -d downloads-oe ];then #sanity check.
 echo "Error, folder downloads-oe does not exist."
 exit
fi
PROJECTPATH="$(pwd)"

#need these to fetch from https sites...
if which update-ca-certificates;then
 mkdir -p /etc/ssl/certs
 [ "$(find /etc/ssl/certs -mindepth 1 -maxdepth 1)" == "" ] && update-ca-certificates > /dev/null 2>&1 #populates /etc/ssl
else
 echo "Error, 'ca-certificates' package is not installed."
 exit
fi

mkdir ${BUILDSPATH}/oe-quirky
echo 'Populating ${BUILDSPATH}/oe-quirky...'
tar -xf downloads-oe/openembedded-core.tar.gz --directory=${BUILDSPATH}/oe-quirky --strip=1
tar -xf downloads-oe/meta-openembedded.tar.gz --directory=${BUILDSPATH}/oe-quirky --strip=1
tar -xf downloads-oe/bitbake.tar.gz --directory=${BUILDSPATH}/oe-quirky
tar -xf downloads-oe/meta-yocto.tar.gz --directory=${BUILDSPATH}/oe-quirky --strip=1
tar -xf downloads-oe/meta-rust.tar.gz --directory=${BUILDSPATH}/oe-quirky
#tar -xf downloads-oe/meta-clang.tar.gz --directory=${BUILDSPATH}/oe-quirky
#tar -xf downloads-oe/meta-browser.tar.gz --directory=${BUILDSPATH}/oe-quirky
#tar -xf downloads-oe/meta-office.tar.gz --directory=${BUILDSPATH}/oe-quirky
sync

#customization for quirky/easyos...
cp -a -f quirky/* ${BUILDSPATH}/oe-quirky/
sync

# copy the docs also...
cp -a docs ${BUILDSPATH}/oe-quirky/
#cp -a -f readme.htm ${BUILDSPATH}/oe-quirky/

# hacks...
#allow run as root...
sed -i -e 's%if 0 == os\.getuid%if 9999 == os.getuid%' ${BUILDSPATH}/oe-quirky/meta/classes/sanity.bbclass

#'setterm' is out of util-linux pkg...
if [ ! -e /usr/bin/setterm ];then
 cp -a -f setterm /usr/bin/
 chmod 755 /usr/bin/setterm
fi
[ ! -e /usr/bin/python2 ] && [ -e /usr/bin/python2.7 ] && ln -s python2.7 /usr/bin/python2
FNDPY3="$(find /usr/bin -maxdepth 1 -type f -name 'python3.[0-9]' | head -n 1)"
if [ ! "$FNDPY3" ];then
 echo "Error, Python >= 3.4 is required."
 exit
fi
BASEPY3="$(basename $FNDPY3)"
[ ! -e /usr/bin/python3 ] && ln -s $BASEPY3 /usr/bin/python3

#download pkgs folder...
mkdir -p ${BUILDSPATH}/downloads
ln -sr ${BUILDSPATH}/downloads ${BUILDSPATH}/oe-quirky/build-amd64/downloads

#fix absolute paths in bblayers.conf...
PTN1="s%ABSPATH%${BUILDSPATH}%"
sed -i -e "$PTN1" ${BUILDSPATH}/oe-quirky/build-amd64/conf/bblayers.conf

#181202 to compile ghostscript with libgs, need this...
#ref: https://patchwork.openembedded.org/patch/137913/
echo '
SECURITY_CFLAGS_pn-ghostscript = "${SECURITY_NO_PIE_CFLAGS}"' >> ${BUILDSPATH}/oe-quirky/meta/conf/distro/include/security_flags.inc
#see also ${BUILDSPATH}/oe-quirky/meta-quirky/recipes-extended/ghostscript/ghostscript_9.20.bbappend

#20200921 kill the msg "... has not been validated with this version ..."
Wstr="bb.warn('Host distribution \"%s\""
sed -i -e "s/${Wstr}/#${Wstr}/" ${BUILDSPATH}/oe-quirky/meta/classes/sanity.bbclass

#200921
echo "$PROJECTPATH" > ${BUILDSPATH}/oe-quirky/PROJECTPATH

echo "Folder '${BUILDSPATH}/oe-quirky' populated.
Now what you have to do, is open a terminal in 'oe-quirky'
and run this, for pc x86_64 build:
# source oe-init-build-env build-amd64
# bitbake core-image-minimal
"
