#!/bin/sh

# begin
# Copyright (C) 2025 Joseph Rosevear, San Diego CA, USA.

# This file is part of a distribution SAM.

# SAM is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your
# option) any later version.

# SAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



# Joseph Rosevear 120813 I rewrote this and made it a simple invocation of a
# new script called bstart.

# Joseph Rosevear 170128 I changed the line below to point to
# /mnt/150605aa2/bstart instead of ~/bstart

# Joseph Rosevear 170128 I added a lines to run brc and source bprofile.  I
# made brc from the original /etc/rc.d/rc.SAM and bprofile from the original
# /opt/SAM/tips/bash_profile.  I edited them.  I'm doing this to prepare
# sam_temp_base and scratch and to get suitable defintions of sam_temp_base
# and perhaps some other variables and perhaps some other preparations.

# Joseph Rosevear 171215 I changed this script to know where it lives
# (place=`dirname $0`).  Then I used $place in the invocations of brc,
# bprofile, and bstart.

# Joseph Rosevear 180330 I reversed the order of invocations of
# bprofile and brc.  I did that because I also changed brc, and now it
# needs bprofile to run first.

# Joseph Rosevear 191103 I changed the invocation of bstart:

# Was:

#    $place/bstart "love; exhand; export sam_bye_command=exit; $*"

# Now:

#    $place/bstart "love; exhand; sam_bye_command=exit; $*"

# Joseph Rosevear 200826 I made this from script begin by copying it.

# Joseph Rosevear 200826 I removed "exhand;" from the invocation of
# bstart.

# Joseph Rosevear 230513 I used $sam_root instead of $place below the
# sourcing of bprofile, as it defines sam_root.

# I added exported definitions of init and lib for when they don't
# exist.

# I changed the comments and added exported definitions for init and
# lib when they are "".

# Joseph Rosevear 230601 I commented-out the exported definition of
# sam_distro.

# Joseph Rosevear 230622 I added a section which defines and exports
# bprofile if it is "".

# Joseph Rosevear 230622 I removed a commented-out line.

# Joseph Rosevear 230719  I changed the order of definitions.

# Joseph Rosevear 240820 I added a definition and use of sam_hosts.



# Definitions
export sam_root=`dirname $0`

if [ "$init" = "" ]; then

   export init=$sam_root/init
fi

if [ "$lib" = "" ]; then

   export lib=$sam_root/lib
fi

if [ "$bprofile" = "" ]; then

   export bprofile=$sam_root/bprofile
fi

if [ "$sam_hosts" = "" ]; then

   export sam_hosts=24
fi

export sam_mark_num=1
export sam_temp_base=/tmp/`whoami`
export env_scratch=$sam_temp_base/scratch
export sam_set_version=2

if [ "$bprofile" != "" ]; then

   . $bprofile
fi



# Beginning of main program.
echo "in $BASH_SOURCE"

mkdir --parents $sam_temp_base/scratch
umask `cat $HOME/.SAM.umask`


$sam_root/go/sam1 "$sam_hosts" `${init}` "; $*"
