#!/usr/local/bin/curseperl
#
# EasyView -- Unix File Viewer/Editor Interface
#             (a "practical" demo for menu.pl)
#
# Note:    Requires curseperl
#
# Author:  Steven L. Kunz
#          Networking & Communications
#          Iowa State University Computation Center
#          Ames, IA  50011
#          Email: skunz@iastate.edu
#
# Date:    September, 1993
#

require "./menu.pl";

  $SIG{'INT'} = 'cleanup';
  $| = 1;		# Command buffering on stdout

  $last_arrow = 0;
  $last_top = 0;
#
#  MAIN_MENU -- Main (top level) menu
#
  while (1) {
    &menu_init(1,"EasyView Version 2.1");
    &menu_item("Exit","%UP%");
    &menu_item("List files in current directory","dir_list");
    &menu_item("Page through a text file","page_file");
    &menu_item("Edit a text file","edit_file");

    $subr = &menu_display("",$last_arrow,$last_top);
    if ($subr eq "%UP%") { 
      &cleanup;
    }
    if ($subr ne "") { &$subr; }	# Call subroutine selected
  }

#**********
#  DIR_LIST -- Provide directory list
#**********
sub dir_list {
   &dir_select(0,".","Directory Contents");
}

#***********
#  PAGE_FILE -- Page through a file in the current directory
#
#  Arguments:  None
#
#  Returns:    Nothing
#
#  Note: Uses file as an unnumbered menu
#***********
sub page_file {
  local($filename,$last_arrow,$last_top);

# Call utility function to select file
  $title = "Select file to page through";
  $filename = &dir_select(1,".","Select file to page through");
  if ($filename eq "%UP%") {
    return;
  }

# Load file as an unnumbered menu - let menu_display do the paging
#
# Special thanks: The tab expansion used here was lifted from the
# "pager" program distributed with perl.4.19 in the "usub" directory.
# Don't know who wrote it but it fit the bill.  SLK
#
  &menu_init(0,"File: $filename");
  open(TEMP,$filename);
  while (<TEMP>) {
    s/^(\t+)/'        ' x length($1)/e;
    &expand($_) if /\t/;
    &menu_item($_,"");
  }
  &menu_display("",$last_arrow,$last_top);
  close(TEMP);
}

sub expand {
    while (($off = index($_[0],"\t")) >= 0) {
        substr($_[0], $off, 1) = ' ' x (8 - $off % 8);
    }
}


#***********
#  EDIT_FILE -- Edit a file in the current directory
#***********
sub edit_file {
  &clear;
  $title = "Select file to edit";
  $filename = &dir_select(1,".","Select file to edit");
  if ($filename eq "%UP%") {
    return;
  }
  system("vi $filename");
}

#************
#  DIR_SELECT -- Load a formatted directory list into a menu.
#
#  Arguments:  Boolean flag indicating numbered menu (1=yes), directory 
#              name string and top-title string for menu
#
#  Returns:    File name (or "%UP%")
#************
sub dir_select {
  local($numbered,$directory,$title) = @_;
  local($last_arrow,$last_top) = 0;
  opendir(DIR,$directory);
  &menu_init($numbered,$title);
dir_entry:
  while ($filename = readdir(DIR)) {
    next dir_entry if ($filename eq "."); 
    next dir_entry if ($filename eq ".."); 
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
     $blksize,$blocks) = stat($filename);
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
    $mon++;
    $sel_action = $filename;
    $filename=$filename.substr("                    ",0,20-length($filename));
    $sel_text = sprintf("%s%s%02d/%02d/%02d %02d:%02d  %s\n",
                $filename,$filler,$mon,$mday,$year,$hour,$sec,getpwuid($uid));
    &menu_item($sel_text,$sel_action);
  }
  $fn = &menu_display("",$last_arrow,$last_top);
  $fn;
}

sub cleanup {
  &clear;
  &refresh;
  &endwin;
  exit;
}
