#!/bin/bash
# 
# chenv
#   change the environment of a running process
#
# Copyright (c) 2009 Stuart Prescott
# 
#   Distributable under the terms of the GPL v2.
#

if [ $# -ne 3 ]
then
  echo "Usage: $0 process variable value" 1>&2
  exit 1
fi

if [[ $1 =~ ^[0-9]+$ ]]
then
  PID=$1
else
  PID=$(pgrep $1)
  if [[ $PID =~ ' ' ]]
  then
    echo "E: more than one PID found" 1>&2
    exit 1
  fi
fi

echo "Modifying $PID to set $2=$3"
TMPFILE=$(mktemp)
cat - > $TMPFILE <<EOF
attach $PID
call putenv ("$2=$3")
detach
EOF
gdb -batch-silent -x $TMPFILE > /dev/null

rm $TMPFILE


