#!/usr/bin/perl
#-----------------------------------------------------------------------#
# @(#) hgsbook v1.01 - 12/02/07 (c) 2007 Handsworth Grammar School	#
# Author: Martin Woolley (Handsworth Grammar School)			#
# Reason: Global book marks, that is allow staff to provide a list of	#
#	  websites that each user is to have bookmarked.		#
#-----------------------------------------------------------------------#
# @(#) hgsbook v1.00 - 17/07/05 (c) 2005 Handsworth Grammar School	#
# 1.01 - include check for brower type & path for Tk			#
#-----------------------------------------------------------------------#
#

use lib "/usr/lib/perl5/site_perl/5.8.1";

use Tk;
use Getopt::Std;

# Some useful things
$PROG="hgsbook";
$VERS="$PROG: 1.01 - 12/02/07";				# change if you update
$BMARKS="/usr/local/lib/hgsbookmarks";			# global bookmarks file
$DEBUG=0;						# display 'useful' stuff
$HDR="HGS Bookmarks - $VERS";				# window header
$BROWSER="firefox";					# web brower
$CMD="type " . $BROWSER . " >/dev/null 2>&1";
$RC=system($CMD);
if ($RC != 0) {
  $BROWSER="/opt/firefox/firefox";
  $CMD="type " . $BROWSER . " >/dev/null 2>&1";
  $RC=system($CMD);
  if ($RC != 0) {
    $BROWSER="opera";
    $CMD="type " . $BROWSER . " >/dev/null 2>&1";
    $RC=system($CMD);
    if ($RC != 0) {
      $BROWSER="konqueror";
    }
  }
}

$MW=MainWindow->new();					# open main window
##$MW =tkinit;						# centre a new window
##$MW->withdraw;
##$MW->Popup;
$MW->minsize(30,15);					# minimum size
$MW->title($HDR);
$T1=$MW->Label(
	-text => "Double click on a description \
to access the website")
	->pack();
$MW_FR=$MW->Frame()					# define main frame
	->pack(-side => "left");
$MW_L_FR=$MW_FR->Frame()				# define listbox frame
	->pack(-side => "left", -pady => 5, -padx =>5);
&get_bmarks;						# load listbox
$MW_R_FR=$MW_FR->Frame()				# define buttons frame
	->pack(-side => "right");
$MW_B=$MW_R_FR->Button(
	-text => "Exit",
	-command => sub {exit;} )
	->pack(-side => "bottom", -fill => "x", -anchor => "se");

MainLoop;


sub get_bmarks {
# read in the bookmarks file and file display window
# blank lines and entries starting with '#' are ignored
#
my ($KNT)=0;
$LB->destroy if Tk::Exists($LB);	 		# drop old listbox if present
$LB=$MW_L_FR->Scrolled("Listbox",			# listbox for bookmarks
	-scrollbars => "osoe",
	-width => 40,
	-background => "white",
	-selectmode => "single",
	-setgrid => 1,
	-takefocus => 0,
	-relief => "groove")
	->pack(-side => "left",-anchor => "w",-fill => "y",-expand => "yes");
$LB->bind('<Double-Button-1>', sub{&proc_sel});		# access url
##$LB->focus;
$LB->delete(0,'end');					# clear list box
@LI=();							# delete list index
open(BMARKS,"<",$BMARKS)
   or die "$PROG: couldn't open '$BMARKS' file\n";
while(chomp($BLINE = <BMARKS>)) {
  next if ($BLINE =~ /^#/);				# drop comments
  next if ($BLINE =~ /^$/);				# drop blank lines
  @TMP=split(/\|/,$BLINE);
  $LB->insert('end',$TMP[0]);				# put desc into listbox
  $KNT++;
  $LI[$KNT]=$TMP[1];					# put url into index
}
close(BMARKS);
if ($DEBUG) {printf "get_bmarks: >%s<\n", $KNT}
}

sub proc_sel {
# process each of the selected items
#
@SEL=$LB->curselection;
for (@SEL) {
  $ITB=$LB->get($_);					# get URL
  $ITM_NO=$LB->index("active");
  $URL=$LI[$ITM_NO+1];
  if ($DEBUG) {printf "proc_sel: >%s< >%s< >%s<\n", $ITM_NO, $ITM, $URL}
  $CMD=$BROWSER . " " . $URL;
  if ($DEBUG) {printf "proc_sel: >%s<\n", $CMD}
  system ($CMD);
}
printf "\n";
}

