#!/bin/sh

# Note: makefiles could be used.

cp p1/comsci/sh/chel.sh p1/comsci/sh/chel.sh.txt;

source chel.sh;


# Find and call bash update.sh.
function updateworkspace
{
  local loc_files=`find -name update.sh`;
  local loc; # Hold the location/directory.
  local t;   #tempory.
  for i in ${loc_files}; do
    t=${i%*/update.sh}; 
    #echo $t;
    if [ "$t" != "." ]; then 
      loc="$t $loc";
    fi;
    #pushd ${i};
    #echo update.sh;
    #bash update.sh;
    #popd;
  done;

  for k in ${loc}; do
    pushd ${k};
    bash update.sh;
    popd;
  done;

  #echo "testing";
  #echo "";
  #echo $loc;
}

updateworkspace;


#
# Functions can not return strings.

# The previous code suggests a general function would be written
#   to operate on each element in the spirit of grep, but
#   where a function is supplied by the user to do the processing.

# Unfortunateld Bash doesn't support string functions, but passes
# data through global variables. 

# I suggest building/implementing a general data stack in Bash
#   which keeps track of elements. Function results could be 
#   passed back on the data stack. Input is through the usual means,
#   but output is through the data stack.

# This is an involved task. 


# Tempory/non-essential code.
function strip_update
{
  local i=$1;
  local res="";
  local t=${i%*/update.sh};

  if [ "$t" != "." ]; then
    res="$t";
  fi;
}

# Tempory/non-essential code.
function test01
{
  local t1="pi/update.sh";
  
  local c1=`strip_update $t1`;

  echo $c1;
}

#test01;




