#!/bin/sh
export AR="arm-xilinx-linux-gnueabi-ar"
export AS="arm-xilinx-linux-gnueabi-as "
export BUILD_AR="ar"
export BUILD_AS="as "
export BUILD_CC="gcc "
export BUILD_CCLD="gcc "
export BUILD_CFLAGS="-isystemFIXMESTAGINGDIRTARGET-native/usr/include -O2 -pipe"
export BUILD_CPP="gcc  -E"
export BUILD_CPPFLAGS="-isystemFIXMESTAGINGDIRTARGET-native/usr/include"
export BUILD_CXX="g++ "
export BUILD_CXXFLAGS="-isystemFIXMESTAGINGDIRTARGET-native/usr/include -O2 -pipe"
export BUILD_FC="gfortran "
export BUILD_LD="ld "
export BUILD_LDFLAGS="-LFIXMESTAGINGDIRTARGET-native/usr/lib -LFIXMESTAGINGDIRTARGET-native/lib -Wl,-rpath-link,FIXMESTAGINGDIRTARGET-native/usr/lib -Wl,-rpath-link,FIXMESTAGINGDIRTARGET-native/lib -Wl,-rpath,FIXMESTAGINGDIRTARGET-native/usr/lib -Wl,-rpath,FIXMESTAGINGDIRTARGET-native/lib -Wl,-O1 -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/sysroots-uninative/x86_64-linux/lib/ld-linux-x86-64.so.2"
export BUILD_NM="nm"
export BUILD_RANLIB="ranlib"
export BUILD_STRIP="strip"
export CC="arm-xilinx-linux-gnueabi-gcc  -march=armv7-a -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=FIXMESTAGINGDIRTARGET"
export CCACHE_DIR="/home/ssw-dev/.ccache"
export CCLD="arm-xilinx-linux-gnueabi-gcc  -march=armv7-a -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=FIXMESTAGINGDIRTARGET"
export CC_FOR_BUILD="gcc "
export CFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/openssh/7.5p1-r0=/usr/src/debug/openssh/7.5p1-r0 -fdebug-prefix-map=FIXMESTAGINGDIRTARGET-native= -fdebug-prefix-map=FIXMESTAGINGDIRTARGET=  -D__FILE_OFFSET_BITS=64"
export CFLAGS_FOR_BUILD="-isystemFIXMESTAGINGDIRTARGET-native/usr/include -O2 -pipe"
export CONFIG_SITE="/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/meta/site/endian-little /scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/meta/site/arm-common /scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/meta/site/arm-32 /scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/meta/site/common-linux /scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/meta/site/common-glibc /scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/meta/site/arm-linux /scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/meta/site/common /scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/../meta-openembedded/meta-oe/site/endian-little "
export CPP="arm-xilinx-linux-gnueabi-gcc -E --sysroot=FIXMESTAGINGDIRTARGET  -march=armv7-a -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9"
export CPPFLAGS=""
export CPPFLAGS_FOR_BUILD="-isystemFIXMESTAGINGDIRTARGET-native/usr/include"
export CPP_FOR_BUILD="gcc  -E"
export CXX="arm-xilinx-linux-gnueabi-g++  -march=armv7-a -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=FIXMESTAGINGDIRTARGET"
export CXXFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/openssh/7.5p1-r0=/usr/src/debug/openssh/7.5p1-r0 -fdebug-prefix-map=FIXMESTAGINGDIRTARGET-native= -fdebug-prefix-map=FIXMESTAGINGDIRTARGET=  -fvisibility-inlines-hidden"
export CXXFLAGS_FOR_BUILD="-isystemFIXMESTAGINGDIRTARGET-native/usr/include -O2 -pipe"
export CXX_FOR_BUILD="g++ "
unset DISTRO
export FC="arm-xilinx-linux-gnueabi-gfortran  -march=armv7-a -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 --sysroot=FIXMESTAGINGDIRTARGET"
export HOME="/home/ssw-dev"
export LC_ALL="en_US.UTF-8"
export LD="arm-xilinx-linux-gnueabi-ld --sysroot=FIXMESTAGINGDIRTARGET "
export LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed"
export LDFLAGS_FOR_BUILD="-LFIXMESTAGINGDIRTARGET-native/usr/lib -LFIXMESTAGINGDIRTARGET-native/lib -Wl,-rpath-link,FIXMESTAGINGDIRTARGET-native/usr/lib -Wl,-rpath-link,FIXMESTAGINGDIRTARGET-native/lib -Wl,-rpath,FIXMESTAGINGDIRTARGET-native/usr/lib -Wl,-rpath,FIXMESTAGINGDIRTARGET-native/lib -Wl,-O1 -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/sysroots-uninative/x86_64-linux/lib/ld-linux-x86-64.so.2"
export LD_FOR_BUILD="ld "
unset MACHINE
export MAKE="make"
export NM="arm-xilinx-linux-gnueabi-nm"
export OBJCOPY="arm-xilinx-linux-gnueabi-objcopy"
export OBJDUMP="arm-xilinx-linux-gnueabi-objdump"
export PATH="/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/sysroots-uninative/x86_64-linux/usr/bin:/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/scripts:FIXMESTAGINGDIRTARGET-native/usr/bin/arm-xilinx-linux-gnueabi:FIXMESTAGINGDIRTARGET/usr/bin/crossscripts:FIXMESTAGINGDIRTARGET-native/usr/sbin:FIXMESTAGINGDIRTARGET-native/usr/bin:FIXMESTAGINGDIRTARGET-native/sbin:FIXMESTAGINGDIRTARGET-native/bin:/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/sysroots-xsct/SDK/2018.3/bin:/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/sysroots-xsct/SDK/2018.3/gnu/microblaze/lin/bin:/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/sysroots-xsct/SDK/2018.3/gnu/aarch32/lin/gcc-arm-none-eabi/bin:/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/sysroots-xsct/SDK/2018.3/gnu/armr5/lin/gcc-arm-none-eabi/bin:/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/sysroots-xsct/SDK/2018.3/gnu/aarch64/lin/aarch64-none/bin:/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/sources/core/bitbake/bin:FIXME_HOSTTOOLS_DIR"
export PKG_CONFIG_DIR="FIXMESTAGINGDIRTARGET/usr/lib/pkgconfig"
export PKG_CONFIG_DISABLE_UNINSTALLED="yes"
export PKG_CONFIG_LIBDIR="FIXMESTAGINGDIRTARGET/usr/lib/pkgconfig"
export PKG_CONFIG_PATH="FIXMESTAGINGDIRTARGET/usr/lib/pkgconfig:FIXMESTAGINGDIRTARGET/usr/share/pkgconfig"
export PKG_CONFIG_SYSROOT_DIR="FIXMESTAGINGDIRTARGET"
export PKG_CONFIG_SYSTEM_INCLUDE_PATH="/usr/include"
export PKG_CONFIG_SYSTEM_LIBRARY_PATH="/lib:/usr/lib"
export PSEUDO_DISABLED="1"
export PSEUDO_UNLOAD="1"
export RANLIB="arm-xilinx-linux-gnueabi-ranlib"
export READELF="arm-xilinx-linux-gnueabi-readelf"
unset SHELL
export STRINGS="arm-xilinx-linux-gnueabi-strings"
export STRIP="arm-xilinx-linux-gnueabi-strip"
unset TARGET_ARCH
export TARGET_CFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/openssh/7.5p1-r0=/usr/src/debug/openssh/7.5p1-r0 -fdebug-prefix-map=FIXMESTAGINGDIRTARGET-native= -fdebug-prefix-map=FIXMESTAGINGDIRTARGET= "
export TARGET_CPPFLAGS=""
export TARGET_CXXFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/scratch/petalinux-yocto/yocto_downloads_2018.3_zynq-generic/build_zynq-generic/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/openssh/7.5p1-r0=/usr/src/debug/openssh/7.5p1-r0 -fdebug-prefix-map=FIXMESTAGINGDIRTARGET-native= -fdebug-prefix-map=FIXMESTAGINGDIRTARGET= "
export TARGET_LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed"
export TZ="UTC"
export base_bindir="/bin"
export base_libdir="/lib"
export base_prefix=""
export base_sbindir="/sbin"
export bindir="/usr/bin"
export datadir="/usr/share"
export docdir="/usr/share/doc"
export exec_prefix="/usr"
export includedir="/usr/include"
export infodir="/usr/share/info"
export libdir="/usr/lib"
export libexecdir="/usr/libexec"
export localstatedir="/var"
export lt_cv_sys_lib_dlsearch_path_spec="/usr/lib /lib"
export mandir="/usr/share/man"
export nonarch_base_libdir="/lib"
export nonarch_libdir="/usr/lib"
export oldincludedir="/usr/include"
export prefix="/usr"
export sbindir="/usr/sbin"
export servicedir="/srv"
export sharedstatedir="/com"
export sysconfdir="/etc"
export systemd_system_unitdir="/lib/systemd/system"
export systemd_unitdir="/lib/systemd"
export systemd_user_unitdir="/usr/lib/systemd/user"

useradd_sysroot() {
	# Pseudo may (do_prepare_recipe_sysroot) or may not (do_populate_sysroot_setscene) be running
	# at this point so we're explicit about the environment so pseudo can load if
	# not already present.
	export PSEUDO="PSEUDO_PREFIX=FIXME_COMPONENTS_DIR/x86_64/pseudo-native/usr PSEUDO_LOCALSTATEDIR=FIXME_PSEUDO_LOCALSTATEDIR PSEUDO_PASSWD=FIXMESTAGINGDIRTARGET:FIXME_COMPONENTS_DIR/x86_64/pseudo-native PSEUDO_NOSYMLINKEXP=1 PSEUDO_DISABLED=0 FIXME_COMPONENTS_DIR/x86_64/pseudo-native/usr/bin/pseudo"

	# Explicitly set $D since it isn't set to anything
	# before do_prepare_recipe_sysroot
	D=FIXMESTAGINGDIRTARGET

	# base-passwd's postinst may not have run yet in which case we'll get called later, just exit.
	# Beware that in some cases we might see the fake pseudo passwd here, in which case we also must
	# exit.
	if [ ! -f $D/etc/passwd ] ||
			grep -q this-is-the-pseudo-passwd $D/etc/passwd; then
		exit 0
	fi

	# It is also possible we may be in a recipe which doesn't have useradd dependencies and hence the
	# useradd/groupadd tools are unavailable. If there is no dependency, we assume we don't want to
	# create users in the sysroot
	if ! command -v useradd; then
		bbwarn "command useradd not found!"
		exit 0
	fi

	# Add groups and users defined for all recipe packages
	GROUPADD_PARAM=""
	USERADD_PARAM="--system --no-create-home --home-dir /var/run/sshd --shell /bin/false --user-group sshd"
	GROUPMEMS_PARAM=""

	# Tell the system to use the environment vars
	UA_SYSROOT=1

	useradd_preinst
}

bbwarn() {
	if [ -p FIXME_LOGFIFO ] ; then
		printf "%b\0" "bbwarn $*" > FIXME_LOGFIFO
	else
		echo "WARNING: $*"
	fi
}

useradd_preinst() {
OPT=""
SYSROOT=""

if test "x$D" != "x"; then
	# Installing into a sysroot
	SYSROOT="$D"
	OPT="--root $D"

	# Make sure login.defs is there, this is to make debian package backend work
	# correctly while doing rootfs.
	# The problem here is that if /etc/login.defs is treated as a config file for
	# shadow package, then while performing preinsts for packages that depend on
	# shadow, there might only be /etc/login.def.dpkg-new there in root filesystem.
	if [ ! -e $D/etc/login.defs -a -e $D/etc/login.defs.dpkg-new ]; then
	    cp $D/etc/login.defs.dpkg-new $D/etc/login.defs
	fi

	# user/group lookups should match useradd/groupadd --root
	export PSEUDO_PASSWD="$SYSROOT"
fi

# If we're not doing a special SSTATE/SYSROOT install
# then set the values, otherwise use the environment
if test "x$UA_SYSROOT" = "x"; then
	# Installing onto a target
	# Add groups and users defined only for this package
	GROUPADD_PARAM="${GROUPADD_PARAM}"
	USERADD_PARAM="${USERADD_PARAM}"
	GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
fi

# Perform group additions first, since user additions may depend
# on these groups existing
if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then
	echo "Running groupadd commands..."
	# Invoke multiple instances of groupadd for parameter lists
	# separated by ';'
	opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
	remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
	while test "x$opts" != "x"; do
		perform_groupadd "$SYSROOT" "$OPT $opts"
		if test "x$opts" = "x$remaining"; then
			break
		fi
		opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
		remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
	done
fi

if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then
	echo "Running useradd commands..."
	# Invoke multiple instances of useradd for parameter lists
	# separated by ';'
	opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
	remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
	while test "x$opts" != "x"; do
		perform_useradd "$SYSROOT" "$OPT $opts"
		if test "x$opts" = "x$remaining"; then
			break
		fi
		opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
		remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
	done
fi

if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then
	echo "Running groupmems commands..."
	# Invoke multiple instances of groupmems for parameter lists
	# separated by ';'
	opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
	remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
	while test "x$opts" != "x"; do
		perform_groupmems "$SYSROOT" "$OPT $opts"
		if test "x$opts" = "x$remaining"; then
			break
		fi
		opts=`echo "$remaining" | cut -d ';' -f 1 | sed -e 's#[ \t]*$##'`
		remaining=`echo "$remaining" | cut -d ';' -f 2- | sed -e 's#[ \t]*$##'`
	done
fi
}

perform_useradd() {
	local rootdir="$1"
	local opts="$2"
	bbnote "openssh: Performing useradd with [$opts]"
	local username=`echo "$opts" | awk '{ print $NF }'`
	local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
	if test "x$user_exists" = "x"; then
		eval flock -x $rootdir/etc -c  \"$PSEUDO useradd \$opts\" || true
		user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
		if test "x$user_exists" = "x"; then
			bbfatal "openssh: useradd command did not succeed."
		fi
	else
		bbnote "openssh: user $username already exists, not re-creating it"
	fi
}

perform_groupadd() {
	local rootdir="$1"
	local opts="$2"
	bbnote "openssh: Performing groupadd with [$opts]"
	local groupname=`echo "$opts" | awk '{ print $NF }'`
	local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
	if test "x$group_exists" = "x"; then
		eval flock -x $rootdir/etc -c \"$PSEUDO groupadd \$opts\" || true
		group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
		if test "x$group_exists" = "x"; then
			bbfatal "openssh: groupadd command did not succeed."
		fi
	else
		bbnote "openssh: group $groupname already exists, not re-creating it"
	fi
}

perform_groupmems() {
	local rootdir="$1"
	local opts="$2"
	bbnote "openssh: Performing groupmems with [$opts]"
	local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'`
	local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
	bbnote "openssh: Running groupmems command with group $groupname and user $username"
	local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
	if test "x$mem_exists" = "x"; then
		eval flock -x $rootdir/etc -c \"$PSEUDO groupmems \$opts\" || true
		mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
		if test "x$mem_exists" = "x"; then
			bbfatal "openssh: groupmems command did not succeed."
		fi
	else
		bbnote "openssh: group $groupname already contains $username, not re-adding it"
	fi
}

bbfatal() {
	if [ -p FIXME_LOGFIFO ] ; then
		printf "%b\0" "bbfatal $*" > FIXME_LOGFIFO
	else
		echo "ERROR: $*"
	fi
	exit 1
}

bbnote() {
	if [ -p FIXME_LOGFIFO ] ; then
		printf "%b\0" "bbnote $*" > FIXME_LOGFIFO
	else
		echo "NOTE: $*"
	fi
}

useradd_sysroot
