#!/bin/sh

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

# Joseph Rosevear 240409 I added an if block which removes write
# permission after the block which rotates.

# Joseph Rosevear 240525 I changed the ls command to use:

#    sort -k9 -V | less -R

# Joseph Rosevear 240709 I replace /tmp with $env_scratch, and I
# edited a comment.

# I added code which checks the invocation.

# Joseph Rosevear 241124 I removed the code which removes write
# permission.



# Definitions.
file="$1"
place="$env_scratch"
self=`basename $0`
problem=false

# Define present.
present=false
if [ -a "$file" ]; then

   present=true
fi

# Check invocation.
if [ "$file" = "" ]; then

   cat << DONE
   
$self: Error. \$1 must be provided.
DONE

   exit
fi   

# Test to see if ${file}* exists.
ls ${file}* > /dev/null
result=$?

# If $result is 0, then find next.
if [ "$result" = "0" ]; then

   last=`ls ${file}* -d | sort -V | tail -n-1`
   last=${last#"$file"}
   
   #if $last is "", then give it the value "-1".
   if [ "$last" = "" ]; then
   
      last="-1"
   fi
   
   next=`echo "$last + 1" | bc`
fi

id=`id -u -n`:`id -g -n`

# Tell what's happening.
cat << DONE

file:    $file
place:   $place
present: $present
last:    $last
next:    $next
id:      $id
DONE

# Test dir place.
if ! [[ (-d "$place") && (-r "$place") ]]; then

   echo
   echo "$self: $place must be a readable directory."
   problem=true
fi

# Does ${place}/${file} exist?
if ! [ -a "${place}/${file}" ]; then

   echo
   echo "$self: ${place}/${file} must exist."
   problem=true
fi

# Rotate the versions by giving the next version number to the default version, if it
# exists. Fail if it exists, but is not writeable.
if [ -a "$file" ]; then

   if ! [ -w "$file" ]; then
   
      echo
      echo "$self: $file is not writeable."
      problem=true
   fi
   
   # Rotate if $problem is false.
   if [ "$problem" = "false" ]; then
   
      mv -i $file ${file}${next}
      result=$?
      
      if [ "$result" != "0" ]; then
      
         echo
         echo "$self: mv -i $file ${file}${next}: failed"
         problem=false
      fi
   fi
fi

# If problem is false, then, as root: Add a new default version from
# $place, then change the ownership to $id.
if [ "$problem" = "false" ]; then
   
   sam_batch=batch $sam_root/bree "
   
      mv -i ${place}/${file} .;
      chown $id $file;
   "
fi

# Wait until file is moved.
while ! [ -a "$file" ]; do

   sleep 1
done

# List all the versions.
echo
ls -ltr ${file}* | sort -k9 -V | less -R
