#!/bin/sh -
#
# @(#)$Id: 28e668551f00a3d15d44e552e96e547ba9a19f59 $
#
# The author of this file, J.A. Neitzel <jan (at) etsh (dot) io>,
# hereby grants it to the public domain.
#

#
# Ready .etsh* and etsh* for installation.
# ../Makefile runs this script via the install-exp target.
#
# usage: $(SHELL) ready_rc_files "$(EBN)" "$(PREFIX)" "$(BINDIR)" "$(SYSCONFDIR)"
#
# Exit w/ a status of 0 on success.
# Exit w/ a status of 1 on error.
# --
# Jeffrey Allen Neitzel
#

if test ! \( $# -eq 4 \) -o \
	! \( -n "$1" -a -n "$2" -a -n "$3" -a -n "$4" \) ; then
   echo 'usage: $(SHELL) ready_rc_files "$(EBN)" "$(PREFIX)" "$(BINDIR)" "$(SYSCONFDIR)"' >&2
   exit 1
fi

ebn=$1
prefix=$2
bindir=$3
sysconfdir=$4

rm -rf ../examples.install
mkdir  ../examples.install || exit 1

for rc_file in .etsh* etsh* ; do
    if test $rc_file = .etsh.prompt ; then
	cat < .etsh.prompt > ../examples.install/.etsh.prompt ; continue ; fi
    sed -e "s|@EBN@|$ebn|g" \
	-e "s|@PREFIX@|$prefix|" \
	-e "s|@BINDIR@|$bindir|" \
	-e "s|@SYSCONFDIR@|$sysconfdir|" < $rc_file \
    > ../examples.install/$rc_file || { echo "$rc_file: error" >&2 ; exit 1 ; }
done

if test "$ebn" != "etsh" ; then
   cd ../examples.install
   echo -n "Moving .etsh* and etsh* to .${ebn}* and ${ebn}* . . . " >&2
   mv .etsh.login  .${ebn}.login
   mv .etsh.logout .${ebn}.logout
   # note: .etsh.prompt will not function with a changed name.
   mv .etshrc      .${ebn}rc
   mv etsh.etshrc   ${ebn}.${ebn}rc
   mv etsh.login    ${ebn}.login
   mv etsh.logout   ${ebn}.logout
   echo "Done ." >&2
fi

exit 0
