#!/bin/sh

# -*- mode: sh; mode: fold -*-
#Check for dialog package
which dialog &> /dev/null
if [ $? -ne 0 ] ; then
  echo "Need dialog package v0.5 and higher."
  echo "You can download it from ftp://sunsite.unc.edu in dir"
  echo "/pub/Linux/utils/shell file dialog-0.5.tar.gz"
  echo "or from http://www.redhat.com"
 
  exit 1
fi


dialog=dialog
dkeeper=dkeeper

keep2() #{{{
{
  #Ask for label name
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --title "Keep mode" \
    --inputbox "Type the label name." 11 70 \
    2> /tmp/dkeeper.tmp.$$
    
  if [ $? -ne 0 ] ; then
    rm -f /tmp/dkeeper.tmp.$$
    exit 1
  fi
  
  label=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$

  #Ask for kpr file
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --title "Keep mode" \
    --inputbox "Enter the file name where will be files information stored (kpr file)." 11 70 \
    2> /tmp/dkeeper.tmp.$$
    
  if [ $? -ne 0 ] ; then
    rm -f /tmp/dkeeper.tmp.$$
    exit 1
  fi
  
  kpr_file=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  #Ask for path
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --clear --title "Keep mode" \
    --inputbox "Type the path to files (usually under /mnt/...)." 11 70 \
    2> /tmp/dkeeper.tmp.$$
    
  if [ $? -ne 0 ] ; then
    rm -f /tmp/dkeeper.tmp.$$
    exit 1
  fi
  
  data_path=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  clear
  eval $dkeeper $options \"$label\" $data_path $kpr_file
  exit 0
}
#}}}

keep1() #{{{
{
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --title "Keep mode" \
    --checklist "" 10 80 2 \
      "Links" "Follow symbolic links" off \
      "Verbose" "Print each file name" on 2> /tmp/dkeeper.tmp.$$
      
  ret=$?
  
  choice=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  case $ret in
    0)
      if echo $choice | grep -q Links ; then
        options="$options"l
      fi
      
      if echo $choice | grep -q Verbose ; then
        options="$options"v
      fi
      
      keep2
      ;;
    *)
      exit 1;;
  esac
}
#}}}

keep() #{{{
{
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --title "Keep mode" \
    --radiolist "Use space to choose." 10 60 2 \
      "Create" "Store files in new kpr file" off \
      "Append" "Add files to exists kpr file" on 2> /tmp/dkeeper.tmp.$$
      
  ret=$?
  
  choice=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  case $ret in
    0)
      case $choice in
        Create) options=-kn;;
	Append) options=-ka;;
      esac
      keep1;;
    *)
      exit 1;;
  esac
}
#}}}

search_info() #{{{
{
  #Ask for info pattern
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --inputbox "Enter the regexpr for information data" 10 60 2> /tmp/dkeeper.tmp.$$
  
  ret=$?
  
  info_pattern=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  if [ $? -ne 0 ] ; then
    exit 1
  fi
  
  #Ask for case sensitive
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --yesno "Case sensitive when comparing the information pattern against the \
      information data?" 10 60
      
  case $? in
    0) options=${options}c;;
    1) ;;
    *) exit 1;;
  esac
  
  #Ask for ignoring the information data of the links
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --yesno "Ignore the information data of links? It contains the file name it points to. \
      I recommend to ingore." 10 60
  
  case $? in
    1) ;;
    0) options=${options}l;;
    *) exit 1;;
  esac
}
#}}}

search() #{{{
{
  options=-s
  
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --inputbox "Enter the kpr file name" 11 60 2> /tmp/dkeeper.tmp.$$
    
  ret=$?
  
  kpr_file=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  if [ $ret -ne 0 ] ; then
    exit 1
  fi
  
  #Ask for file pattern
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --inputbox "Enter the file for which you want to search. You can use standart file \
      patterns." 11 60 2> /tmp/dkeeper.tmp.$$
      
  ret=$?
  
  file_pattern=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  #Ask for case sensitive
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --yesno "Case sensitive when comparing a file name against the file pattern?" 10 60
 
  case $? in
    0) options=${options}d;;
    1) ;;
    *) exit 1;;
  esac
  
  #Ask if search in info data
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --yesno "Do you want to search in information data of the file?" 10 60
  
  case $? in
    0) search_info;;
    1) ;;
    *) exit 1;;
  esac
  
  #Ask for print style
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --radiolist "Print style 0:\nFor each matched file prints the label name where \
is\nthe file and to the new line file name\n\nPrint style 1:\nFor each matched file \
prints as in style 0 and then\nprints lines of information data and to the new \
line\ninformation data" 15 60 2 "Style 0" "Print style 0" on "Style 1" \
"Print style 1" off 2> /tmp/dkeeper.tmp.$$

  ret=$?
  
  choice=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  case $ret in
    0) if [ "$choice" = "Style 0" ] ; then
         options=${options}0 ;
       else
         options=${options}1
       fi
    
       if [ -z $info_pattern ] ; then
         eval $dkeeper $options \"$file_pattern\" $kpr_file ;
       else
         eval $dkeeper ${options}i \"$info_pattern\" \"$file_pattern\" $kpr_file
       fi;;
    *) exit 11;
  esac
}
#}}}

export_html_one() #{{{
{
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --inputbox "Enter the name of html output file" 10 60 2> /tmp/dkeeper.tmp.$$
    
  ret=$?
  
  html=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  if [ $ret -ne 0 ] ; then
    exit 1
  fi
  
  eval $dkeeper $options $html $kpr_file
  exit
}
#}}}

export_html_more() #{{{
{
  #Html dir
  $dialog --backtitle "Menu interface of DKeeper v$version" \
  --inputbox "Enter the name of directory where will be stored all html files" 10 60 2> \
    /tmp/dkeeper.tmp.$$
  
  ret=$?
  
  html=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  if [ $ret -ne 0 ] ; then
    exit 1
  fi
 
  #Info count
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --inputbox "How much files with info data will be stored in one info html file" 10 60 2> \
      /tmp/dkeeper.tmp.$$
      
  ret=$?
  
  info_count=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  if [ $ret -ne 0 ] ; then
    exit 1
  fi
  
  #dir_ count
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --inputbox "How much dirs will be stored in one html file" 10 60 2> \
      /tmp/dkeeper.tmp.$$
      
  ret=$?
  
  dir_count=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  if [ $ret -ne 0 ] ; then
    exit 1
  fi
  
  eval $dkeeper $options $html -D $dir_count -I $info_count $kpr_file
  exit
}
#}}}

export_html() #{{{
{ 
  options=${options}h
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --radiolist "Export to a one or more html files?" 10 60 2 "One" "" off "More" "" on \
    2> /tmp/dkeeper.tmp.$$
  
  ret=$?
  
  choice=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  case $ret in
    0)
      case $choice in
        One)
	  export_html_one;;
	More)
	  export_html_more;;
      esac;;
    *)
      exit 1;;
  esac
}
#}}}

export_mysql() #{{{
{
  options=${options}m
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --inputbox "Enter the name of MySQL output directory" 10 60 2> /tmp/dkeeper.tmp.$$
    
  ret=$?
  
  mysql=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  if [ $ret -ne 0 ] ; then
    exit 1
  fi
  
  eval $dkeeper $options $mysql $kpr_file
  exit
}
#}}}

export() #{{{
{
  options=-e
  
  #Kpr name
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --inputbox "Enter the name of kpr file which to export" 10 60 2> /tmp/dkeeper.tmp.$$
  
  ret=$?
  
  kpr_file=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  if [ $ret -ne 0 ] ; then
    exit 1
  fi

  #Format
  $dialog --backtitle "Menu interface of DKeeper v$version" \
    --menu "Export to format:" 10 60 2 HTML "" MySQL "" 2> /tmp/dkeeper.tmp.$$
    
  ret=$?
  
  choice=`cat /tmp/dkeeper.tmp.$$`
  rm -f /tmp/dkeeper.tmp.$$
  
  case $ret in
    0) 
      case $choice in
        HTML)
	  export_html;;
	MySQL)
	  export_mysql;;
      esac;;
    *)
      exit 1;;
  esac
  
}
#}}}

version=0.2
$dialog --backtitle "Menu interface of DKeeper v$version" \
  --msgbox "This is the menu interface of DKeeper v$version. It is a good \
    choice if you don't like command line options." 9 40

$dialog --backtitle "Menu interface of DKeeper v$version" \
  --title "Choose a mode" \
  --menu "Please choose a mode you want to use." 11 60 3 \
    "Keep" "Keep the files from a data media" \
    "Search" "Search for a particular files" \
    "Export" "Export kpr file to other format (HTML)" 2>/tmp/dkeeper.tmp.$$
    
ret=$?

choice=`cat /tmp/dkeeper.tmp.$$`
rm -f /tmp/dkeeper.tmp.$$

case $ret in
  0) 
    case $choice in
      Keep) keep;;
      Search) search;;
      Export) export;;
    esac ;;
  *)
    exit 1;;
esac

exit 0
