#!/bin/sh
# Copy an existing text file to a temporary location. Then
# Edit the file.
# Attempt to then transfer the temporary file back to the original
# location if the temprary file has been altered.
# Conclude with a little clean-up.
# Try to avoid deleting any changes.

if [ $# -lt 1 ]
then
   echo "usage: $0 text-file"
   exit 1
fi

if [ ! -f "$1" ]
then
   echo "File $1 does not exist or is a special file/link."
   exit 2
fi

if [ -L "$1" ]
then
   echo "File is a symbolic link. Refusing to edit."
   exit 2
fi

if [ ! -r "$1" ]
then
   echo "This user is unable to read the specified file."
   exit 3
fi

temp_file=$(mktemp --tmpdir doasedit.XXXXXXXX)
if [ ! -r "$temp_file" ]
then
   echo "Was unable to create temporary file."
   exit 4
fi

mydir=$(dirname -- "$1")
myfile=$(basename -- "$1")
cp "$mydir/$myfile" "$temp_file"
status=$?
if [ $status -ne 0 ]
then
   echo "Unable to copy file $1"
   exit 5
fi

# If $VISUAL fails, run $EDITOR.
# $EDITOR should be a line editor functional without advanced terminal features.
# $VISUAL is a more advanced editor such as vi.
"${VISUAL:-vi}" "$temp_file"
status=$?
if [ $status -ne 0 ]
then
    echo "Could not run visual editor $VISUAL"
    "${EDITOR:-ex}" "$temp_file"
    status=$?
    if [ $status -ne 0 ]
    then
      echo "Could not run editor $EDITOR"
      echo "Please make sure the VISUAL and/or EDITOR variables are set."
      rm -f "$temp_file"
      exit 6
    fi
fi

# Check to see if the file has been changed.
cmp -s "$mydir/$myfile" "$temp_file"
status=$?
if [ $status -eq 0 ]
then
   echo "File unchanged. Not writing back to original location."
   rm -f "$temp_file"
   exit 0
fi

# At this point the file has been changed. Make sure it still exists.
if [ -f "$temp_file" ]
then
    doas cp "$temp_file" "$mydir/$myfile"
    cmp -s "$temp_file" "$mydir/$myfile"
    status=$?
    # If file fails to copy, do not do clean-up
    while [ $status -ne 0 ]
    do
       echo "Copying file back to $1 failed. Press Ctrl-C to abort or Enter to try again."
       read abc
       doas cp "$temp_file" "$mydir/$myfile"
       cmp -s "$temp_file" "$mydir/$myfile"
       status=$?
    done
fi

# Clean up
rm -f "$temp_file"
exit 0

