#!/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
#tar -xf downloads-oe/meta-qt5.tar.gz --directory=${BUILDSPATH}/oe-quirky
#tar -xf downloads-oe/meta-qt5-extra.tar.gz --directory=${BUILDSPATH}/oe-quirky

#2020106 ***NO***
##20201004...
#tar -xf downloads-oe/meta-debian.tar.gz --directory=${BUILDSPATH}/oe-quirky
#sed -i -e 's%^LAYERSERIES_COMPAT_debian = .*%LAYERSERIES_COMPAT_debian = "dunfell"%' ${BUILDSPATH}/oe-quirky/meta-debian/conf/layer.conf
#sed -i -e 's%^BBFILE_PRIORITY_debian = .*%BBFILE_PRIORITY_debian = "9"%' ${BUILDSPATH}/oe-quirky/meta-debian/conf/layer.conf
#rm -rf ${BUILDSPATH}/oe-quirky/meta-debian/recipes-kernel
#rm -f ${BUILDSPATH}/oe-quirky/meta-debian/classes/linux-src.bbclass #only reqd by recipes-kernel
##cut it down...
#for aAPP in bc busybox cmake dtc gawk git grep kbd kmod lsb meson mtools pam pam-pkcs11 pam-shield paramiko perl pkgconfig ply python 'python-*' 'py*' qemu rpm sed selinux shadow shared-mime-info socat sudo tar tzdata u-boot util-linux xz zip
#do
# rm -rf ${BUILDSPATH}/oe-quirky/meta-debian/recipes-debian/${aAPP}
#done
##touch every file...
#for aFILE in `find ${BUILDSPATH}/oe-quirky/meta-debian -mindepth 1 -type f -o -type l`
#do
# touch $aFILE
#done
##need patches etc from 'warrior' release of oe...
#echo "Copying warrior recipe files to meta-warrior..."
#for aPKG in `find ${BUILDSPATH}/oe-quirky/meta-debian/recipes-debian -mindepth 1 -maxdepth 1 -type d`
#do
# aNAME="$(basename $aPKG)"
# fndPKG="$(find downloads-oe/warrior/openembedded-core/meta/recipes-* -maxdepth 1 -type d -name ${aNAME} | head -n 1)"
# if [ "$fndPKG" ];then
#  echo " ${fndPKG}"
#  fndPATH="$(dirname $fndPKG)"
#  fndRECIPE="$(basename $fndPATH)"
#  mkdir -p ${BUILDSPATH}/oe-quirky/meta-warrior/${fndRECIPE}
#  cp -a ${fndPKG} ${BUILDSPATH}/oe-quirky/meta-warrior/${fndRECIPE}/
# fi
#done
#mkdir ${BUILDSPATH}/oe-quirky/meta-warrior/conf
#echo 'BBPATH .= ":${LAYERDIR}"
#BBFILES += "${LAYERDIR}/recipes-*/*/*.bb ${LAYERDIR}/recipes-*/*/*.bbappend"
#BBFILE_COLLECTIONS += "warrior"
#BBFILE_PATTERN_warrior := "^${LAYERDIR}/"
#BBFILE_PRIORITY_warrior = "8"
#LAYERVERSION_warrior = "1"
#LAYERDEPENDS_warrior = "core"
#LAYERSERIES_COMPAT_warrior = "dunfell"
#' > ${BUILDSPATH}/oe-quirky/meta-warrior/conf/layer.conf
##libpcap wants this...
#cp -a -f downloads-oe/warrior/openembedded-core/meta/classes/bluetooth.bbclass ${BUILDSPATH}/oe-quirky/meta-debian/classes/
##glibc wants this...
#cp -a -f downloads-oe/warrior/openembedded-core/meta/classes/libc-common.bbclass ${BUILDSPATH}/oe-quirky/meta-debian/classes/

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 src-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

#oe gatesgarth pre-release, kernel 5.8.13, it wanted /usr/bin/awk in host
#no harm in doing it now...
ln -s ../../bin/awk /usr/bin/awk 2>/dev/null

#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
"
