#!/bin/sh

# sam_edit
# 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 I wrote this script.

# Joseph Rosevear 060424 I changed the invocation of exed to use "$2
# $1" instead of "$1" to allow passing parmameters like -overwrite as
# $2.  I made a complementary change to ment.f, so the scripts made by
# ment can accept parameters like -overwrite as $3.

# Joseph Rosevear 140829 I changed exed to texed.

# Joseph Rosevear 191110 I rearranged the if-block below which
# previously would not allow editing of a file (by non-root) which was
# both big and writable.

# Now it does allow editing of such files, but it gives an explanation
# regarding big files and makes you press a key to continue.

# It still gives the "You must be root..." message, but that comes
# first in the if-block, as the code was easier that way.

# Joseph Rosevear 230503 I removed the use of $env_handy from this
# script.  I think it is enough that handy has been added to the path,
# and having been added I will just invoke it.

# Therefore I changed the only use of handy in this way.  It was:

#    $env_handy/texed "$2 $1"

# Now:

#    texed "$2 $1"

# There is a story that goes with this.  I changed SAM so I can invoke
# it via B which does not run in the same way as begin, although it
# uses many of the same bits of code.  As a result this script which
# defined env_example was not being run:

#    /mnt/SAM_root/sam-start/exhand.sam

# Note that this script, after defining env_example, in turn, ran
# another script which defined env_handy.



# some definitions
BIG=16

du $1 > $sam_temp_dir/du.dat

read size balance < $sam_temp_dir/du.dat

#get perms:
perms="`ls -l $1 | grab 1`"

#get pu, pg and po
pu="`echo $perms | cut -c 3`"
pg="`echo $perms | cut -c 6`"
po="`echo $perms | cut -c 9`"
     
writable=false
#check if writable
if [ \( "$pu" = "w" \) -o \
     \( "$pg" = "w" \) -o \
     \( "$po" = "w" \) ]; then
        
   writable=true
fi

big=false
#Is the file big?
if [ "$size" -gt "$BIG" ]; then
   
   big=true;
fi

# Warn if writable even though no w was found in perms.  This happens
# when you are root.
if [ "$writable" != "true" ] && [ -w "$1" ]; then
            
   cat << done
      
You must be root.  That is OK, but be careful since you can write to
the file even though it has no write permission.  That could be bad, as
this is an archived file.

done

   pause

elif [ "$big" = "true" ]; then

   echo
   echo "File"
   echo "$1"
   echo "is big.  It's time to mv it to"
   echo "$1<n>,"
   echo "and perhaps make it read only, if you haven't done these"
   echo "things already."
   echo
   echo "I will edit the file, anyway, but be aware that a big file"
   echo "left as writable is either a mistake or a \"hybrid\"--"
   echo "an archived file which is still being updated."
   echo
   
   pause
fi

texed "$2 $1"
