#!/usr/bin/env osh
:
: osh - " Force sh(1), csh(1), and other shells to exit w/ error! " <'' ;;;
:
: "  @(#)$Id: 12bc3231bb952e422059dab9d296f8850c3eb788 $  "
:
: "  The author of this file, J.A. Neitzel <jan (at) v6shell (dot) org>,  "
: "  hereby grants it to the public domain.                               "
:

:
: "  Prepare system-wide rc files (etc.*) & user rc files (dot.*)  "
: "  for installation on current machine so interactive & login    "
: "  instances of osh can execute them on user's behalf.           "
:
: "  Print message, and exit w/ zero status on success.            "
: "  Print diagnostic, and exit w/ non-zero status on error.       "
:
: "  usage: osh FILEPREP /path/to/nonexistent_directory            "
:

: " Check for correct usage. "

if X$0 != XFILEPREP -a X$0 != X./FILEPREP\
	if { exit } fd2 -e echo FILEPREP: $0: Invalid FILEPREP pathname ; false
if $n != 1 -o X$1 = X\
	if { exit } fd2 -e echo usage: osh $0 /path/to/nonexistent_directory ;\
	false
if -e $1'' -o -h $1''\
	if { exit } fd2 -e echo FILEPREP: $1: File exists ; false
if { fd2 which . >/dev/null }\
	if { exit } fd2 -e echo $0: which: Invalid results ; false
if ! { which chmod cp head mkdir pwd rm strings tee tr >/dev/null }\
	if { exit } false

: " Do needed setup. "

	sigign + 2 3 ; umask 0022
	if { mkdir -p $1'' } cp *osh* $1/
	if $s != 0 if { exit } false
	cd $1'' ; chmod 0644 *
	if ! { mkdir tmp-$$ } if { exit } false
	setenv OSHDIR tmp-$$

: " Prepare files for installation. "

	echo -n "<$1 ( rm -f $1 ; sed 's,@SYSCONFDIR@," >$d/sysconfdir
	which osh | sed 's,.*,strings &,' | osh |\
		sed -n 's,\(.*\)/osh.login,\1,p' >$d/sysconfdir_path
	<$d/sysconfdir_path tr -d '\n' >>$d/sysconfdir
	echo ",' >$1 )" >>$d/sysconfdir
	osh - etc.osh.login etc.osh.oshrc etc.osh.logout ; goto EXECSHELL
	: Loop
		osh $d/sysconfdir $1 ; shift
		if $n = 0 exit
		goto Loop

: EXECSHELL

	(\
	 echo -n "<$1 ( rm -f $1 ; sed 's,@EXECSHELL@," ;\
	 which osh | tr -d '\n' ; echo ",' >$1 )"       ;\
	) >$d/execshell
	osh $d/execshell dot.osh.login
	: fallthrough

: OSHDIR

if ! { fd2 which oshdir >/dev/null } goto NotFound
	(\
	 echo -n "<$1 ( rm -f $1 ; sed 's,@SOURCE_OSHDIR@,source " ;\
	 which oshdir | tr -d '\n' ; echo " $$,' >$1 )"            ;\
	) >$d/oshdir
	goto Done

: NotFound

	(\
	 echo -n "<$1 ( rm -f $1 ; sed 's,@SOURCE_OSHDIR@,"             ;\
	 echo ": See http://v6shell.org/v6scripts/oshdir.osh .,' >$1 )" ;\
	) >$d/oshdir
	: fallthrough

: Done - " Finish, clean up, and exit w/ zero status. "

	osh $d/oshdir dot.osh.login
	: fallthrough

	(echo Successful File Prep! From the:;echo;echo -n '	';pwd;echo;\
	 echo ...;echo directory, please manually copy:;echo)|tee fileprep.log

	(osh - *osh*;echo;echo ...;echo after modifying each file to taste.)|\
		tee -a fileprep.log;rm -rf $d;: zero status;exit
	: Loop1
		echo -n '	'$1'	to  '
		head -1 <$1 | sed 's,^: \(.*\) - .*$,echo \1,' | osh ; shift
		if $n = 0 exit
		goto Loop1
