#! /bin/ksh

#
# Name	      : ${HOME}/bin/SQLite.ksh
#
# Version     : 1.0
#
# Author      : Gerard van Wageningen
#
# Description : SQLite query (http://www.sqlite.org).
#
# 1.0	First Version
#


\whence set_debug 1> /dev/null 2>&1 && eval $( set_debug ${0##*/} )


if (( $# != 1 ))
then
  print -u2 "Usage:\n\t$0 <SQLite database>"
  exit 1
fi

if ! \whence sqlite3 1>/dev/null 2>&1
then
  print -u2 'Could not find executable sqlite3.'
  exit 1
fi

if [[ ! -r "$1" ]]
then
  print -u2 "Could not read SQLite database file \"$1\"."
  exit 1
fi

#case $( file "$1" ) in
#  *SQLite*database) ;;
#  *)
#    print -u2 "File \"$1\" is not an SQLite database."
#    exit 1
#    ;;
#esac
if [[ $( dd ibs=1 if="$1" count=7 2>/dev/null ) != 'SQLite ' ]]
then
  print -u2 "File \"$1\" is not an SQLite database file."
  exit 1
fi

sqlite3 -column "$1" 'SELECT * from sqlite_master;'


exit $?

############# EOF #############
