#!/usr/bin/perl

#--------------------------------------------------------#
$description =
"Syntax: sscan [options] [file...]

Sscan produces a metrical analysis of a Sanskrit text
in Unicode Roman (ISO 15919). Heavy syllables are
represented in the output by \"-\", light syllables by \"u\". The
program understands the conventions regarding comments,
prose passages, \"X uvāca\"-type lines and non-anuṣṭubh
lines employed in electronic texts of the Mahābhārata
and Rāmāyaṇa; it should produce good results with other
texts too, provided that they are laid out in a fairly normal way.

-h option prints this help.
";
#--------------------------------------------------------#

require 'getopts.pl';
Getopts(':h');
if ($opt_h) {
    print STDERR $description;
    exit 1;
}

while (<>) {
    next if (/^\d{8}[ A-Z] /);				# MBh uvaaca, prose
    if (($num, $line) = /^(\d+[^ ]* +)(.*)$/s) {	# Line numbers
	$_ = $line;
    }
    else {
	print "\n" if /^$/;
	next;
    }

    tr/’//d;							 	# Remove apostrophes
    
    s/ā/L/g;							# Long vowels -> L
    s/ī/L/g;
    s/ū/L/g;
    s/r̥̄/L/g;
    s/a[iu]/L/g;
    s/[eo]/L/g;

    s/[aiu]/S/g;							# Short vowels -> S
    s/r̥/S/g;
    s/l̥/S/g;
    
    s/[kgcjtdpb]h?/C/g;					# Consonants -> C
    s/ṭh?/C/g;
    s/ḍh?/C/g;
    s/ḷh?/C/g;
    s/m̐/C/g;
    s/[nmyrlvsh]/C/g;
    s/ṁ/C/g;
    s/ḥ/C/g;
    s/ṅ/C/g;
    s/ñ/C/g;
    s/ṇ/C/g;
    s/ś/C/g;
    s/ṣ/C/g;

    tr/ ://d;								# Remove spaces and colons

    s/^C*//;							# Initial consonants
    s/LC*(;?)C*/- $1/g;					# V
    s/SC{2,};C*/- ;/g;						# vCC;
    s/SC;C+/- ;/g;						# vC;C
    s/S(;?)C{2,}/- $1/g;					# vCC (includes v;CC)
    s/SC*(;?)C*/u $1/g;					# vC  (includes v;C, vC;)
    s/ $//;								# Tidy up
    s/;/; /g;
    
    print $num, $_;    
}
