#!/bin/bash


set -o allexport
## shellcheck disable=SC1090
[ -n "$PROVISIONING_ENV" ] && [ -r "$PROVISIONING_ENV" ] && source "$PROVISIONING_ENV"
[ -r "../env-provisioning" ] && source ../env-provisioning
[ -r "env-provisioning" ] && source ./env-provisioning
#[ -r ".env" ] && source .env set
set +o allexport

export PROVISIONING=${PROVISIONING:-/usr/local/provisioning}


TARGET_LIST=pack_list
pwd=$(pwd)
if [[ "$PROVISIONING"  == *provisioning* ]] ; then 
	while [ "$(basename $pwd)" != "provisioning" ]  && [ "$pwd" != "/"  ]
	do  
   		cd .. &&  pwd=$(pwd) 
	done
else 
	cd $PROVISIONING
  pwd=$(pwd)
fi
[ "$(basename "$pwd")" != "provisioning" ]  && echo "provisioning  path not found in $(pwd)"  && exit 1 
[ -r "distro/$TARGET_LIST" ] && mv distro/$TARGET_LIST distro/_$TARGET_LIST && echo "Previous distro list in 'distro/_$TARGET_LIST" 
find . -type f | grep -v "\.git" | grep -v "\.kclvm"  | grep -v "/wrks" | grep -v distro | grep -v "./no/" | grep -v "/_" | grep -v ".shellcheckrc" |\
   grep -v "/old_"  | grep -v "url.txt" > distro/$TARGET_LIST  &&
echo "provisioning pack list created in $TARGET_LIST"
