#!/bin/bash

# Placed in the public domain "as-is".
#   Bob Newell, Honolulu, Hawai`i
#   bobnewell@bobnewell.net
#   2021-July-12 and later

#######################################################################
# "cheebu" is a very cheesy text only database that is inspired by an
# old DOS program called 'say'. A database is simply a plain text
# file. Each line of the database is a record. No record can be longer
# than a single line. No keys, no table relations, nothing but text
# records. Cheesy!

# But surprisingly useful.  "cheebu" has been tested with files as
# large as 6 MB in length containing 26,000 records, some over 10,000
# characters long, and works with excellent speed, even on a
# relatively slow tablet computer running Termux.  However, ultra-long
# records do become unwieldy and best results will be had with records
# of under about 128 characters.

# The only functions are add, find, and delete, as shown below.  To
# list the entire database use 'find' with no argument.

# Be sure to edit the location of 'cheefile' just below.
#######################################################################

echo
echo "Cheebu the Cheesy Database V1.0"
echo

# Location of database. Change to suit. 

cheefile="$HOME/data/cheesy.db"

# Check for args.
if [[ $1 == "" ]] ; then
    echo "Need command: a,d,f for add, delete, find"
    echo "a (text to add)"
    echo "d (regexp to match for record deletion)"
    echo "f (regexp to find or blank for db dump)"
    echo
  exit 1
fi

# This will create the file if it doesn't exist,
# including any missing directories.

if ! [[ -f "$cheefile" ]]
then
 echo "Creating new database $cheefile"
 install -D /dev/null "$cheefile"
fi

# What to do?
case $1 in

# Add record.
a) shift
   if [[ $* == "" ]] ; then
       echo "Add what?"
       echo
       exit 1
   fi
# Append new info to database file.
   echo "$*" >>"$cheefile"
   echo "Added." ;;

# Delete record. 
d) shift
   delit="$*"
   if [[ "$delit" == "" ]] ; then
       echo "Delete what?"
       echo
       exit 1
   fi
# Eliminate any leftover temp file.   
   if [[ -f cheefile.tmp ]]
   then
      rm cheefile.tmp
   fi
# Nothing found to delete yet.   
   found=0
# Read through the database.   
   while read -u 3 line
   do
     tline="$line"
     if [[ ! $line =~ $delit ]]
# Not a record to delete. Write to temp file.	  
     then 
        echo "$tline" >>cheefile.tmp
# A delete candidate. Query for action.	 
     else
        echo
        read -p "Delete $tline ? " -n 1 -r
# Yes, delete it, which we would do by simply failing to copy to the
# temp file.
        if [[ $REPLY =~ ^[Yy]$ ]]
        then
# But make a backup first, if we haven't already. Note there is only
# one level of backups.
            if [[ $found -eq 0 ]]
	    then
               cp "$cheefile" "$cheefile".bak
               echo "(Backup made to $cheefile.bak)"
	       found=1
	    fi
# No, don't delete it, so save it to the temp file.
	else    
	    echo "$tline" >>cheefile.tmp
	fi
     fi
   done 3< "$cheefile"
# If we have a temp file, then copy back over the database file.
   if [[ -f cheefile.tmp ]]
   then
      mv cheefile.tmp "$cheefile"
   fi
;;

# Find records. A null target will list everything.
f) shift
   grep -i "$*" "$cheefile"
;;

# Unknown option.
*) echo "Unknown option. Type only 'cheebu' for help." ;;
 
esac

echo
exit 0
