#!/bin/sh #---------------------------------------------------------------------------- # /var/install/bin/apache2-config-module-sqlitemanger-database # # Creation: 2010-01-30 hb # Last Update: $Id$ # # Copyright (c) 2010-@@YEAR@@ Holger Bruenjes # # This program 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 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib ASK='/var/install/bin/ask' MKTEMP='/usr/bin/mktemp' AWK='/usr/bin/gawk' #exec 2>/tmp/sqlite-orga-trace$$.log #set -x # -------------------------------------------------------------------------- # do sqlite check # --------------------------------------------------------------------------- do_sqlite_check() { # check for sqlite and version if [ -x "$(which sqlite 2>/dev/null)" ] then sqlite_version=2 sqlite_exe="sqlite" config="/srv/www/sqlitemanager/db/config.db" fi if [ -x "$(which sqlite3 2>/dev/null)" ] then sqlite_version=$((${sqlite_version} + 3)) sqlite_exe="sqlite3" config="/srv/www/sqlitemanager/db/config3.db" fi } # --------------------------------------------------------------------------- # list database # --------------------------------------------------------------------------- show_database() { clrhome cd /srv/sqliteDB # get databases from folder sqliteDB _database=$(ls -F 2>/dev/null) echo mecho --info -n "Show database in" mecho " '/srv/sqliteDB'" echo techo --begin '3 3r 1 4r 1 32' techo --row "" --info No "" --info Lnk "" --info Database echo idx=1 echo "${_database}" | while read line do if [ $(echo ${line} | grep "@$") ] then line=$(echo "${line}" | sed 's#@$##') # techo --row "" "${idx}" "" --link '* ' "" "${line}" techo --row "" "${idx}." "" '* ' "" "${line}" else techo --row "" "${idx}." "" "" "" "${line}" fi idx=$((${idx} + 1)) done echo techo --end # get number of action_entry to select the max number entry_lines=$(echo "${_database}" | wc -l) echo _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "Select" "" "1-${entry_lines}" "^$=Return" "0=Exit" >${_ask_tmpfile} rc=${?} read action_to_do <${_ask_tmpfile} rm -f ${_ask_tmpfile} if [ ${rc} = 255 ] then action_to_do=0 fi case ${action_to_do} in '') select_action ;; 0) exit 127 ;; *) # get action line action_line="$(echo "${_database}" | sed -n "${action_to_do}p" | sed 's#^ *##; s#@$##')" rm ${action_line} ${sqlite_exe} ${config} "DELETE FROM database WHERE location = \"${action_line}\"" show_database ;; esac } # --------------------------------------------------------------------------- # select action # --------------------------------------------------------------------------- select_action() { clrhome work_to_do="Add a new database to SQLiteManager config Remove database from SQLiteManager and folder '/srv/sqliteDB'" echo mecho --info "Please select your work" echo techo --begin '3 3r 1 68*' techo --row "" --info No "" --info "Job to do" echo idx=1 echo "${work_to_do}" | while read line do techo --row "" "${idx}." "" "${line}" idx=$((${idx} + 1)) done echo techo --end # get number of action_entry to select the max number entry_lines=$(echo "${work_to_do}" | wc -l) echo _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "Select" "" "1-${entry_lines}" "^$=Return" "0=Exit" >${_ask_tmpfile} rc=${?} read action_to_do <${_ask_tmpfile} rm -f ${_ask_tmpfile} if [ ${rc} = 255 ] then action_to_do=0 fi case ${action_to_do} in '') exit 0 ;; 0) exit 127 ;; 1) add_database ;; 2) show_database ;; esac } # --------------------------------------------------------------------------- # add database # --------------------------------------------------------------------------- add_database() { clrhome echo mecho --info "Add database" echo mecho --info "Enter the full path and name from the new database" echo echo echo "Example:" echo " /srv/sqliteDB/data.db without extra name" echo " /srv/sqliteDB/data.db source.db with extra name" echo echo echo _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "Path: (ENTER=Return, 0=Exit)" "" "*" >${_ask_tmpfile} rc=${?} read action_to_do <${_ask_tmpfile} rm -f ${_ask_tmpfile} if [ ${rc} = 255 ] then action_to_do=0 fi case ${action_to_do} in '') select_action ;; 0) exit 127 ;; *) cd /srv/sqliteDB echo echo "Database: ${action_to_do}" echo echo _ask_tmpfile=$(${MKTEMP} -t .XXXXXXXXXXXXX) ${ASK} "Database OK:" >${_ask_tmpfile} rc=${?} read ok_to_do < ${_ask_tmpfile} rm -f ${_ask_tmpfile} if [ ${rc} = 255 ] then ok_to_do=no fi if [ "${ok_to_do}" = "no" ] then add_database else # set -- ${action_to_do} full_db=$(echo "${action_to_do}" | ${AWK} '{print $2}') new_db=$(echo "${action_to_do}" | ${AWK} '{print $2}' | sed 's#\..*$##') if [ -z "${new_db}" ] then full_db=$(basename ${action_to_do}) new_db=$(basename ${action_to_do} | sed 's#\..*$##') fi strip_db=$(echo "${action_to_do}" | sed 's# .*$##') if [ -f ${strip_db} ] then ln -sf ${action_to_do} chown wwwrun:nogroup ${strip_db} chmod 0600 ${strip_db} db_exist=true else echo mecho --error "Database '${strip_db}' not found" mecho --info "Please do again" echo anykey add_database fi if ${db_exist:-false} then _db_first=$(echo ${new_db} | cut -c1 | tr [:lower:] [:upper:]) _db_secon=$(echo ${new_db} | cut -c2-) ${sqlite_exe} ${config} "INSERT INTO database (name, location) VALUES (\"${_db_first}${_db_secon}\", \"${full_db}\")" add_database fi fi ;; esac } # --------------------------------------------------------------------------- # main # --------------------------------------------------------------------------- do_sqlite_check select_action exit 0 # --------------------------------------------------------------------------- # end # ---------------------------------------------------------------------------