#!/usr/bin/bash
# preplocal - Used for dev mode
#             materializes etc/trisul-geo/domain0 from share templates and
#             rewrites /usr/local paths to $INSTALL_PREFIX
#
source env.eff

BINDIR=$INSTALL_PREFIX/bin
SHARE=$INSTALL_PREFIX/share/trisul-geo
ETC_DOMAIN0=$INSTALL_PREFIX/etc/trisul-geo/domain0

# The probe development prefix - this is where the GEO will be injected 
PROBE_PREFIX=/home/vivek/bldart/8p6/trisul_probe/install.debug

echo "Creating the local /etc area under $INSTALL_PREFIX"
mkdir -p $ETC_DOMAIN0
cp -f $SHARE/domain0.xml         $ETC_DOMAIN0/domain0.xml
# Local development uses router endpoints from devmode.cert.  Keep the
# runtime filename expected by the domain configuration.
cp -f $SHARE/devmode.cert        $ETC_DOMAIN0/domain0.cert
cp -f $SHARE/geo0.cert           $ETC_DOMAIN0/geo0.cert
cp -f $SHARE/geo0.cert_secret    $ETC_DOMAIN0/geo0.cert_secret
cp -f $SHARE/trisulGeoConfig.xml $ETC_DOMAIN0/trisulGeoConfig.xml
chmod 600 $ETC_DOMAIN0/geo0.cert_secret

echo "Rewriting /usr/local -> $INSTALL_PREFIX in domain0.xml and trisulGeoConfig.xml"
sed -i "s|/usr/local|$INSTALL_PREFIX|g" $ETC_DOMAIN0/domain0.xml
sed -i "s|/usr/local|$INSTALL_PREFIX|g" $ETC_DOMAIN0/trisulGeoConfig.xml

mkdir -p $INSTALL_PREFIX/var/run/trisul-geo/domain0/geo0
mkdir -p $INSTALL_PREFIX/var/lib/trisul-geo/domain0/run
mkdir -p $INSTALL_PREFIX/var/lib/trisul-geo/domain0/geo0/run
mkdir -p $INSTALL_PREFIX/var/log/trisul-geo/domain0
mkdir -p $INSTALL_PREFIX/var/log/trisul-geo/domain0/geo0

echo "Local trisul-geo domain0 ready at $ETC_DOMAIN0"

# incoming_0_<filename> links for regular plugin files only.
# Skip if the link already exists, and never use a symlink as the source
# (re-running would otherwise chain incoming_0_incoming_0_... and overflow PATH_MAX).
for file in "$SHARE/plugins"/*.csv; do
    [ -f "$file" ] && [ ! -L "$file" ] || continue
    name=$(basename "$file")
    link="$SHARE/plugins/incoming_0_${name}"
    if [ ! -e "$link" ]; then
        ln -s "$file" "$link"
    fi
done

GEO_ID=geo0
DOMAIN_ROOT=$(sed -n 's:.*<DomainRoot>\(.*\)</DomainRoot>.*:\1:p' "$ETC_DOMAIN0/domain0.xml")
CP_PIDFILE="$DOMAIN_ROOT/$GEO_ID/run/trisul_cp_geo.pid"

if [ -f "$CP_PIDFILE" ]; then
	OLD_PID=$(tr -d '[:space:]' < "$CP_PIDFILE")
	if [ -n "$OLD_PID" ] && kill -0 "$OLD_PID" 2>/dev/null; then
		echo "Stopping trisul_cp_geo pid=$OLD_PID (pidfile=$CP_PIDFILE)"
		kill "$OLD_PID" || true
		for i in 1 2 3 4 5; do
			kill -0 "$OLD_PID" 2>/dev/null || break
			sleep 1
		done
		kill -9 "$OLD_PID" 2>/dev/null || true
	fi
	rm -f "$CP_PIDFILE"
fi

# Inject the data path into the probe
if [ ! -d $PROBE_PREFIX/share/trisul-probe/plugins/trisul-geo ]; then
    echo "Injecting the data path into the probe"
    ln -sf $SHARE/plugins $PROBE_PREFIX/share/trisul-probe/plugins/trisul-geo
fi

# Inject the plugin library and lua scripts into the probe
GEO_LIB_PLUGINS=$INSTALL_PREFIX/lib/trisul-geo/plugins
PROBE_LIB_PLUGINS=$PROBE_PREFIX/lib/trisul-probe/plugins

if [ -e "$GEO_LIB_PLUGINS/libgeometer.so" ]; then
    echo "Linking libgeometer.so into probe plugins"
    ln -sf "$GEO_LIB_PLUGINS/libgeometer.so" "$PROBE_LIB_PLUGINS/libgeometer.so"
fi

if [ -d "$GEO_LIB_PLUGINS/lua" ]; then
    echo "Linking lua plugin directory into probe"
	rm -f "$PROBE_LIB_PLUGINS/lua/trisul-geo"
	ln -sf "$GEO_LIB_PLUGINS/lua" "$PROBE_LIB_PLUGINS/lua/trisul-geo"
fi


echo "Starting trisul_cp_geo"
$BINDIR/trisul_cp_geo -demon $ETC_DOMAIN0/domain0.xml $GEO_ID
