#!/bin/sh

# bree
# 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 110116 I changed this script so that it doesn't use
# sudo.  I did this because the new sudo (Slackware 13.1, sooner?) is
# very strict about inheriting environment variables.  It looked like I
# couldn't make it work.  This is actually better, anyway.

# I also put something back I had taken out in making the above change. 
# I provided "/sbin:/usr/sbin:$sam_add" as $2 for bound.  This worked
# well before, so I'll use it again.  Otherwise sometimes root can't
# find commands that it needs.

# Joseph Rosevear 110327 I added some things to this script to allow it
# to work in Slackware 13.1 (transitioning from 12.0):

# Joseph Rosevear 110401 I simplified and improved the fix below by
# using "push; pop" and then "source $sam_temp_dir/sam_set".  This
# captures the whole environment, so explicit handling of .profile,
# $sam_batch, $sh_flag, and $LS_OPTIONS is unneeded.

   # before su -c invocation
   
   #    place="`pwd`"
   
   # in su -c invocation
   
   #    cd \"$place\"
   #    source /root/.profile
   #    export sam_batch=$sam_batch
   #    export sh_flag=$sh_flag
   #    export LS_OPTIONS=\"$LS_OPTIONS\"
   
# Joseph Rosevear 110818 I replaced "source" with "." for improved
# POSIX compliance.

# Joseph Rosevear 111231 I replaced #!/bin/sh with #!/bin/bash.

# Joseph Rosevear 120808 I changed the prgama back to #!/bin/sh,
# because due to changes in SAM the sh shell will work fine.

# Joseph Rosevear 170130 I made this tailored copy.

# Joseph Rosevear 170130 I made two changes below to enable this to
# work as part of my new portable SAM.

# Joseph Rosevear 171219 I added use of bplace.

# Joseph Rosevear 180321 I added re-definition of HOME.

# Joseph Rosevear 180329 I changed bplace to sam_root.

# Joseph Rosevear 180329 I cleaned up by removing some in-line comments.

# Joseph Rosevear 180407 I removed :$sam_add from the end of bound's second
# argument.  It seems the use of ":$sam_add" was an error.  It was causing
# extra instances of the $sam_add string in PATH.  By "extra" I mean in
# addition to the already present duplication that is a natural part of how
# bree (and breeze) work.

# Joseph Rosevear 200425 I added a sourced invocation of
# $sam_root/bclean to the code below.  This to improve a "problem" I've
# seen--bree otherwise gives pold and PATH multiple instances of the
# same dir.

# Joseph Rosevear 200514 I added a semicolon (";") after the invocation
# of beclean.  Leaving it out was an error that didn't surface until
# now.

# And I edited the way the code is continued over several lines making
# it more readable.

# Joseph Rosevear 210215 I put $sam_add back into the invocation.  This
# time I put it at the beginning of the string which I think is a more
# logical place.  This way the sam_add string is assembed in order from
# left to right.

# Joseph Rosevear 210801 I removed the bclean invocation.  It is not
# needed since I wrote function lean.

# Joseph Rosevear 231030 pass18.  I made a small change by moving an
# escaped quotation mark.

# This:

#    $sam_root/begin bound $level $sam_add:/sbin:/usr/sbin \
#    \"final; export sam_mark_num=2; $*\"" - root

# Became:

#    $sam_root/begin \"bound $level $sam_add:/sbin:/usr/sbin \
#    final; export sam_mark_num=2; $*\"" - root

# Joseph Rosevear 231101 I removed the "\" for continuing a line.  This
# was in su -c"..." at the end of the next to last line.  I don't think
# it was needed, but I don't actually know the consequences of taking
# it out vs.  leaving it in.  It is my understanding that "\" for
# continuing a line is not needed inside of "...".

# Joseph Rosevear 231102 I made pass20.



place="`pwd`"

push; pop

# Joseph Rosevear 180321 I changed the code below in one place.
echo login as root
su -c "cd \"$place\";
       . $sam_temp_dir/sam_set;
       export sam_temp_base=/tmp/root;
       export HOME=/root;
       $sam_root/begin \"bound $level $sam_add:/sbin:/usr/sbin
       final; export sam_mark_num=2; $*\"" - root
