# -*- sh -*-
# Support functions for the startscript and some other scripts.

rotate () {
  b=5;
  for a in 4 3 2 1 ; do mv -f $1.$a $1.$b 2> /dev/null;  b=$a; done
}

setup_environment() {
  # If environment file doesn't exist, try to create it.
  if test ! -f "$LOCALDIR"/environment && test -f bin/buildenv.pike;  then
    if [ $verbose -gt 0 ]; then
      dp "Building environment: $pike bin/buildenv.pike"
    fi
    $pike bin/buildenv.pike
    touch "$LOCALDIR"/environment
  fi

  # Set up environment
  if test -f "$LOCALDIR"/environment; then
    . "$LOCALDIR"/environment
  fi

  # Also source environment2, which will not be modified by updates
  if test -f "$LOCALDIR"/environment2; then
    . "$LOCALDIR"/environment2
  fi

  # Make sure $CLASSPATH contains the servlet stuff
  CLASSPATH="java/classes${CLASSPATH:+:}$CLASSPATH"
  for jar in java/classes/*.jar; do
    CLASSPATH="$jar:$CLASSPATH"
  done
  export CLASSPATH
}

dp() {
  if [ "x$SILENT_START" = "xy" ] ; then 
     :
  elif [ "x$self_test" = "xy" ]; then
    echo "$@" >&2
  else
    echo "          :" "$@" >&2
  fi
}

dp_no_nl() {
  if [ "x$SILENT_START" = "xy" ] ; then 
     :
  elif [ "x$self_test" = "xy" ]; then
    echo "$@" | tr -d '
' >&2
  else
    echo "          :" "$@" | tr -d '
' >&2
  fi
}

roxen_version() {
   VERSION="`sed <etc/include/version.h -e '/__roxen_version__/s/[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\)[^0-9]*/\1/p' -n | head -1`"
   BUILD="`sed <etc/include/version.h -e '/__roxen_build__/s/[^0-9]*\([0-9][0-9]*\)[^0-9]*/\1/p' -n | head -1`"
   if [ -f "RELEASE" ] ; then
     RELEASE="`head -1 <RELEASE`"
   else
     RELEASE="-cvs"
   fi
   echo "$VERSION.$BUILD$RELEASE"
}

fullpath() {
  (
    IFS=:
    for path in $PATH ; do
      if [ -x "$path/$1" ] ; then
	full="$path/$1"
	break
      fi
    done
    ( test "x$full" != "x" && echo "$full" ) || echo "$1"
  )
}

change_owner( ) {
 uid="$1";  shift
 gid="$1";  shift

 for a in "$@" ; do 
    ok=`ls -lgd $a 2>/dev/null | grep "$uid" | grep "$gid" | wc -l`
    ok=`echo $ok`
    if [ x$ok = x1 ] ; then 
      dp "$a already has the correct ownership"
    else
      if [ x"$gid" = x ] ; then
        gid=root
      fi
      dp "Changing ownership of $a to $uid:$gid"
      if chown -R "$uid" "$a" 2>/dev/null ; then
        if  chgrp -R "$gid" "$a" 2>/dev/null ; then
	  :
        else
          dp "Group change failed"
        fi
       else
        dp "Ownership change failed"
       fi
     fi
 done
}


find_pike() {
 pike="`fullpath pike`"
 if [ -x bin/pike ] ; then pike="$roxendir/bin/pike"; fi
 if [ -x bin/roxen ] ; then  pike="$roxendir/bin/roxen"; fi
 if [ -x "$LOCALDIR/bin/pike" ] ; then pike="$LOCALDIR/bin/pike"; fi
 if [ -x "$LOCALDIR/bin/roxen" ] ; then pike="$LOCALDIR/bin/roxen"; fi
 if [ "x$PIKE" = "x" ]; then :; else
   if [ -x "$PIKE" ]; then 
     pike="$PIKE"
   else
     pikepath="`fullpath \"$PIKE\"`"
     if [ -x "$pikepath" ]; then 
       pike="$pikepath"
     else 
       dp "$PIKE is not executable - ignored."
     fi
   fi
 fi

 if [ x"$pike" = "x" ] ; then
   dp "No pike binary found. Aborting."
   exit 1
 fi

 if [ ! -f "$pike" ] ; then
   dp "No pike binary found. Aborting."
   exit 1
 fi

 # Check version of Pike.
 if [ x`"$pike" -e 'if (__VERSION__ > 7.4) write("new");'` = "xnew" ]; then
   # Currently we're targetting for Pike 7.4.
   DEFINES="-V7.4 $DEFINES"
   export DEFINES
 fi

 PIKE="$pike"
 export PIKE
}

raise_limit() {
  if ulimit -n unlimited 2>/dev/null ; then 
   :
  else
    nfds=1048576
    while [ $nfds -gt "`ulimit -n`" ] ; do 
      nfds=`expr $nfds / 4`
      ulimit -n $nfds 2>/dev/null
    done
  fi
  if [ "x$1" = "x" ] ; then
    nn="`ulimit -n`"
    if [ $verbose -gt 0 ]; then
      if [ "x$nn" = xunlimited ] ; then
        dp "Maximum number of open FDs: unlimited (2147483648)"
      else
        dp "Maximum number of open FDs: $nn"
        if [ "$nn" -lt 1024 ] 2>/dev/null; then
          dp "Warning: This is dangerously low"
        fi
      fi
    fi
  fi
}
