#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - edit       *
#  ###  ##        ####### ####### ##     **      $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
# NoSQL v2 trivial table editor.
#
##########################################################################

Exit ()
{
  rm -f "${traplist}"
  exit $1
}

# Include main NoSQL front-end.
. ${NSQLIB}/sh/nosqlmain

while [ $# -ge 1 ]
do
  case $1 in
	-R*) 			;;
	-K|--key)  		shift; key_field=$1 ;;
	-u|--unique)    key_unique=1 ;;
	*)     	   		tbl_file=$1; break ;;
  esac
  shift
done

out_dir=$(dirname ${tbl_file:-.})

if cd ${out_dir}
then
  :
else
  echo "Could not access directory ${out_dir}" >&2
  Exit 2
fi

tbl_file=$(basename ${tbl_file})

# Table name is mandatory.
if test -z "${tbl_file}"
then
  echo "Usage: nosql edit tablename" >&2
  Exit 1
fi

# Check whether a final 'ci -u' may be necessary.
if test -f ${tbl_file}
then
  check_in="ci -u"
else
  check_in="ci"
fi

# If no writeable table exists then try and check it out from RCS.
if test ! -w ${tbl_file}
then
  co -q -l ${tbl_file}
  test $? -eq 0 && use_rcs=1
fi

edit_tmp=$(${NSQTEMPF})
traplist=${edit_tmp}
trap "rm -f ${traplist}" 0 1 2 15

if nosql istable < ${tbl_file}
then
  if nosql lock ${tbl_file}
  then
	if test -z "${key_field}"
	then
	  # The key column defaults to the leftmost one.
	  key_field=$(nosql field 1 < ${tbl_file} | nosql headline)
	fi
	traplist="${traplist} ${tbl_file}.LCK"
	trap "rm -f ${traplist}" 0 1 2 15
    nosql tabletolist < ${tbl_file} > ${edit_tmp}
	offset=1
	editor=${EDITOR:-vi}
	while :
	do
	  if test "${editor}" = "vi"
	  then
		${editor} +${offset} ${edit_tmp}
	  else
        ${editor} ${edit_tmp}
	  fi
	  set -- ; set - $(nosql islist -v -e < ${edit_tmp})
	  if test -z "$3"
	  then
		break
	  else
		offset=$3
		echo -e "\nWhat shall I do ?\n"
		echo "1) Re-edit the table"
		echo "2) Quit without saving the changes"
		echo "3) Commit the changes anyway"
		# ash(1) echo builtin does not handle this well.
		/bin/echo -ne "\n=> "

		read edit_action edit_other

		if test ${edit_action:=2} -eq 1
		then
		  :
		elif test ${edit_action} -eq 2
		then
		  Exit 3
		else
		  break
		fi
	  fi
	done

	# Keep the table sorted on the key column.
	if test -z "${key_unique}"
	then
	  nosql listtotable < ${edit_tmp} |nosql sort ${key_field} > ${tbl_file}
	else
	  nosql listtotable < ${edit_tmp} |nosql sort -u ${key_field} > ${tbl_file}
	fi

    # Handle RCS versioning on output table.
    if test "${use_rcs}"
	then
      echo "." | ${check_in} -q ${tbl_file}
    fi

    nosql unlock ${tbl_file}
  else
	echo "Table ${tbl_file} already locked" >&2
  fi
else
  echo "File ${tbl_file} is not a valid table" >&2
fi

