#!/bin/sh
# sceptre: el nombre corto. Sin argumentos propios es sc1; ademas:
#   sceptre --init-user [dirs...]   arma los archivos propios del usuario
#   sceptre --build-runtime         recompila el arbol (make install del
#                                   Makefile artesanal)
# Reubicable, como los demas launchers (paso 6 del PLAN).
set -eu
PRG=$0
case "$PRG" in
  */*) : ;;
  *)   PRG=`command -v "$PRG" 2>/dev/null || printf '%s' "$0"` ;;
esac
while [ -h "$PRG" ]; do
   DESTINO=`ls -ld "$PRG" | sed 's/^.*-> //'`
   case "$DESTINO" in
     /*) PRG=$DESTINO ;;
     *)  PRG=`dirname "$PRG"`/$DESTINO ;;
   esac
done
BINDIR=`CDPATH= cd -- "\`dirname -- "$PRG"\`" && pwd`
BASEDIR=${SCEPTRE_HOME:-`CDPATH= cd -- "$BINDIR/.." && pwd`}

case "${1:-}" in
  --init-user)
     shift
     for d in "$BINDIR" "$BASEDIR/bin"; do
        test -x "$d/sceptre-user-setup" && exec "$d/sceptre-user-setup" "$@"
     done
     echo "sceptre: no encuentro sceptre-user-setup" >&2; exit 1 ;;
  --build-runtime)
     shift
     test -f "$BASEDIR/Makefile" || { echo "sceptre: $BASEDIR no es un arbol de fuentes" >&2; exit 1; }
     exec make -C "$BASEDIR" install "$@" ;;
esac

for d in "$BINDIR" "$BASEDIR/bin"; do
   test -x "$d/sc1" && exec "$d/sc1" "$@"
done
echo "sceptre: no encuentro sc1" >&2; exit 1
