#!/bin/sh
#
# A build system for libgraph.

path() {
  system=`uname -s`

  if test -z $prefix
    then
      if test $system = NetBSD
        then prefix=/usr/pkg
      else
        prefix=/usr
      fi
  fi
}

machine() {
  if test -z $ARCH
    then
      ARCH=`uname -m | sed -e "s/i[4-9]86/i386/" \
      -e "s/armv[3-6].*/arm/" \
      -e "s/armv7.*/armv7/" \
      -e "s/evbarm/arm/"`
  fi
  if test $ARCH = arm
    then ASFLAGS="-I arch/arm --warn -march=armv3"
  elif test $ARCH = armv7
    then
      ASFLAGS="--defsym ARM_V7=1 -I arch/arm -march=armv4t"
      ARCH=arm
  elif test $ARCH = i386
    then ASFLAGS="--warn -march=i386"
  else
    echo Error: unknown architecture. Please fix redo.sh >&2
    echo Abort.
    exit 0
  fi
}

hash_file() {
  if test "$system" = NetBSD
    then
      cksum -a md5 "$1" | cut -f 4 -d " "
      return
  fi
  md5sum "$1" | cut -f 1 -d " "
}

file_state() {
  meta_file=.redo/`echo "$1" | tr "/" "+"`

  if test "$system" = NetBSD
    then timestamp=`stat -f %m "$1"`
  else
    timestamp=`stat -c %Y "$1"`
  fi

  if test -f $meta_file
    then
      old_timestamp=`head -n 1 $meta_file`

      if test $timestamp -eq $old_timestamp
        then return 0
      fi
      old_hash=`tail -n 1 $meta_file`

      hash=`hash_file "$1"`
      if test $hash = $old_hash
        then return 0
      fi
  else
    hash=`hash_file "$1"`
  fi
  printf "%s\n%s\n" $timestamp $hash > $meta_file
  return 1
}

run() {
  echo "$*"
  "$@" > /dev/null
}

build() {
  system=`uname -s`
  mkdir -p .redo
  if test $system = NetBSD
    then ASFLAGS="--defsym NetBSD=1 $ASFLAGS"
  fi

  for source in *.c
  do
    if ! file_state $source
      then
        out=`echo $source | sed -e "s/\.c/\.o/"`
        run "$CC" $CFLAGS -c -I include -I $PREFIX/include/libfndn -o $out $source
    fi
  done
  "$AR" -crs libgraph.a *.o
}

clean() {
run rm -fr \
*.o \
libgraph.a \
.redo
}

distclean() {
  clean
  rm -f .config
}

install() {
  path
  test -f libgraph.a || all
  run rm -fr ${PREFIX}/include/libgraph
  run mkdir -p ${PREFIX}/lib ${PREFIX}/share/doc/libgraph ${PREFIX}/include/libgraph
  run cp -f README ${PREFIX}/share/doc/libgraph/
  if test $system != NetBSD
    then run cp -r include/linux ${PREFIX}/include/libgraph/
  fi
  run cp -r include/*.h ${PREFIX}/include/libgraph/
  run cp -f libgraph.a ${PREFIX}/lib/
}

uninstall() {
  path
  run rm -fr \
${PREFIX}/include/libgraph \
${PREFIX}/lib/libgraph.a \
${PREFIX}/share/doc/libgraph
}

defconfig() {
  machine
  path
  echo \# Automatically generated configuration for redo.sh > .config
  echo AR=\"ar\" >> .config
  echo CC=\"cc\" >> .config
  echo ARCH=\"${ARCH}\" >> .config
  echo CFLAGS=\"-nostdinc -fno-builtin -fno-stack-protector -fno-pic -std=c89 -Wall -Wno-main -Wpedantic\" >> .config
  echo PREFIX=\"${PREFIX}\" >> .config
}

if test ! -z $1 && test $1 = defconfig
  then
    defconfig
    exit $?
fi

if test ! -f .config
  then
    echo You have not yet configured libgraph! >&2
    echo Please run some configurator \(e.g. \"./redo.sh defconfig\"\) >&2
    exit 0
fi
while read line
do
  eval $line
done < .config

if test $# -gt 0
  then
    if ! `type $1 | grep -q function\$`
      then
        echo Error: unknown verb: $1 >&2
        exit 1
    fi
    eval $@
else
  all
fi
