#!/bin/sh

# sam_edit
# Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010, 2014, 2019 Joseph
# Rosevear

# This file is part of an application of SAM for GNU/Linux Slackware
# known as SAM-GLS.

# SAM-GLS 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-GLS 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/>.

# (Slackware is a registered trademark of Patrick Volkerding and
# Slackware Linux, Inc.)



# 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.



# 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

$env_handy/texed "$2 $1"
