# home to all my small misc functions.

use strict;
use warnings;

sub ci_sort
{
	my @sortme2 = sort { lc($a) cmp lc($b) } @_;
	return @sortme2;
}

#--------------------------------------------------------------------------------------------------------------
# nf_error
#--------------------------------------------------------------------------------------------------------------

sub nf_error {
	my $msg = shift;

	print "ERROR: $msg\n";
}

#--------------------------------------------------------------------------------------------------------------
# readdir, also removes . and .. from listing which nf needs
#--------------------------------------------------------------------------------------------------------------

sub readdir {
	my $d = shift;
        my @dl_1 = ();
        my @dl_2 = ();

	opendir(DIR, "$d") or nf_error("sub readdir cant open directory $d, $!");
	@dl_1 = readdir(DIR);
       	closedir DIR;

        for(@dl_1) 
        {
        	if($_ eq ".") 
        	{
                	next;
                }
                push @dl_2, $_;
        }

        return @dl_2;
}

sub br_readdir 
{
	my $d = shift;
        my @dl_1 = ();
        my @dl_2 = ();

	opendir(DIR, "$d") or nf_error("sub readdir cant open directory $d, $!");
	@dl_1 = CORE::readdir(DIR);
       	closedir DIR;

        for(@dl_1) 
        {
        	s/^\s+|\s+$//g;
        	if($_ eq "." || $_ eq ".." || $_ eq "") 
        	{
                	next;
                }

                if($main::proc_dirs && !-d $_) 
                {
                	next;
                }
                
                if($main::ig_type == 0 && $_ !~ /\.($main::file_ext_2_proc)$/i)
                {
                	next;
                }
                push @dl_2, $_;
        }
        
        return @dl_2;
}
#--------------------------------------------------------------------------------------------------------------
# save_file
#--------------------------------------------------------------------------------------------------------------

sub save_file 
{
        my $file = shift;
        my $t = shift;
        print "file = $file\n";
        $t =~ s/^\n//g;		# no blank line @ start of file
        $t =~ s/\n\n+/\n/g;	# no blank lines in file
        open(FILE, ">$file") or die "ERROR: sub save_file, Couldnt open $file to write to. $!";
        print FILE $t;
        close(FILE);
}


sub file_append 
{
	my $file = shift;
	my $string = shift;

	open(FILE, ">>$file") or die "ERROR: Couldnt open $file to append to. $!";
        print FILE $string;
        close(FILE);

}

#--------------------------------------------------------------------------------------------------------------
# read file
#--------------------------------------------------------------------------------------------------------------

sub readf 
{
        my $file = $_[0];

        open(FILE, "$file") or die("ERROR: Couldnt open $file to read.\n");
        my @file = <FILE>;
        close(FILE);

        # clean file of empty lines
        $file =~ s/^\n//g;
        $file =~ s/\n\n+/\n/g;

        return @file;
}

#--------------------------------------------------------------------------------------------------------------
# read and sort file
#--------------------------------------------------------------------------------------------------------------

sub readsf 
{
        my $file = $_[0];

        open(FILE, "$file") or die("ERROR: Couldnt open $file to read.\n");
        my @file = <FILE>;
        close(FILE);

        # clean file of empty lines
        $file = join('', sort @file);
        $file =~ s/^\n//g;
        $file =~ s/\n\n+/\n/g;
        @file = split('\n+', $file);

        return @file;
}

#--------------------------------------------------------------------------------------------------------------
# read, sort and join file
#--------------------------------------------------------------------------------------------------------------

sub readsjf 
{
        my $file = $_[0];

        open(FILE, "$file") or die("ERROR: Couldnt open $file to read.\n");
        my @file = <FILE>;
        close(FILE);
        $file = join('', sort @file);
        $file =~ s/^\n//g;
        $file =~ s/\n\n+/\n/g;

        return $file;
}

#--------------------------------------------------------------------------------------------------------------
# read and join file
#--------------------------------------------------------------------------------------------------------------

sub readjf 
{
        my $file = $_[0];

        open(FILE, "$file") or die("ERROR: Couldnt open $file to read.\n");
        my @file = <FILE>;
        close(FILE);
        $file = join('', @file);
        $file =~ s/^\n//g;
        $file =~ s/\n\n+/\n/g;

        return $file;
}

#--------------------------------------------------------------------------------------------------------------
# multiscroll Y axis
#--------------------------------------------------------------------------------------------------------------

sub multiscrolly {
        my ($sb,$wigs,@args) = @ARG;
        my $w;
        foreach $w (@$wigs) {
                $w->yview(@args);
        }
}

#--------------------------------------------------------------------------------------------------------------
# clear options
#--------------------------------------------------------------------------------------------------------------

sub clr_no_save {
	# clear options that are never saved

        $main::replace		= 0;
	$main::rpwold         	= "";
        $main::rpwnew         	= "";
        $main::front_a		= 0;
        $main::faw            	= "";
        $main::end_a		= 0;
        $main::eaw            	= "";

	$main::id3_art_str	= "";
	$main::id3_alb_str	= "";
	$main::id3_com_str	= "";
	$main::id3_gen_str 	= "Metal";
	$main::id3_year_str 	= "";

	$main::id3_art_set	= 0;
	$main::id3_alb_set	= 0;
	$main::id3_com_set	= 0;
	$main::id3_gen_set 	= 0;
        $main::id3_year_set 	= 0;

	$main::id3v1_rm		= 0;
	$main::id3v2_rm		= 0;
}

#--------------------------------------------------------------------------------------------------------------
# Escape strings for use in regexp - wrote my own cos uri is fucked.
#--------------------------------------------------------------------------------------------------------------


sub escape_string {
	my $s = shift;

	$s =~ s/(\~|\`|\!|@|\#|\$|\%|\^|\&|\(|\)|\=|\+|\{|\[|\]|\}|\:|\;|\"|\'|\<|\,|\.|\>|\?)/"\\".$1/eg;

	# pulled out -
	return $s;
}

#--------------------------------------------------------------------------------------------------------------
# namefix print
#--------------------------------------------------------------------------------------------------------------

sub nf_print 
{
	my $s1 = shift;
	my $s2 = shift;
        my $fi = "";

	chomp $s1;
        if(!$s2) 
        {
        	$s2 = "";
        }
        chomp $s2;

        my $hlist_file = $s1;
        if(!$main::LISTING && !$main::testmode) 
        {
        	$hlist_file = $s2;
        }

	$main::hlist2->add
	(
		$main::hl_counter,
		-data=>[$hlist_file, $main::hlist_cwd]
	);

        if($s1 =~ /^\s+$/) 
        {
                $main::hlist2->itemCreate
                (
                        $main::hl_counter,
                        0,
                        -itemtype => "text",
                        -text => " "
                );
        }
        elsif
        (
        	(($main::LISTING || $main::testmode) && -d $s1) ||	# listing check if s1 is file
                (!$main::LISTING && -d $s2)		# file renamed, check if s2 is file :)
        ) 
        {
                $main::hlist2->itemCreate
                (
                        $main::hl_counter,
                        0,
                        -itemtype=>'imagetext',
                        -image=>$main::folderimage
                );
        }
        elsif
        (
	        (($main::LISTING || $main::testmode) && -f $s1) ||
	        (!$main::LISTING && -f $s2)
	) 
	{
                $main::hlist2->itemCreate
                (
                        $main::hl_counter,
                        0,
                        -itemtype=>'imagetext',
                        -image=>$main::fileimage
                );
        }
        else 
        {
                $main::hlist2->itemCreate
                (
                        $main::hl_counter,
                        0,
                        -itemtype => "text",
                        -text => " "
                );
        }

	if($main::id3_mode == 1) 
	{
		my $art = shift;
		my $tit = shift;
		my $tra = shift;
		my $alb = shift;
		my $com = shift;
                my $gen = shift;
                my $year = shift;

		my $newart = shift;
		my $newtit = shift;
		my $newtra = shift;
		my $newalb = shift;
		my $newcom = shift;
                my $newgen = shift;
                my $newyear = shift;

		if(!$s2) 
		{
			$fi = $s1;
		}
		else
		{
			chomp $s2;
			$fi = $s2;
		}

		$main::hlist2->itemCreate($main::hl_counter, 1, -text => "$s1");

		if($art) 
		{
			$main::hlist2->itemCreate($main::hl_counter, 2, -text => "$art");
		}
		if($tra) 
		{
			$main::hlist2->itemCreate($main::hl_counter, 3, -text => "$tra");
		}
		if($tit) 
		{
			$main::hlist2->itemCreate($main::hl_counter, 4, -text => "$tit");
		}
		if($alb) 
		{
			$main::hlist2->itemCreate($main::hl_counter, 5, -text => "$alb");
		}

		if($gen) 
		{
			$main::hlist2->itemCreate($main::hl_counter, 6, -text => "$gen");
		}

		if($year) 
		{
			$main::hlist2->itemCreate($main::hl_counter, 7, -text => "$year");
		}

		if($com) 
		{
			$main::hlist2->itemCreate($main::hl_counter, 8, -text => "$com");
		}


                if($main::LISTING == 0) 
                {
			$main::hlist2->itemCreate($main::hl_counter, 9, -text => " -> ");
              	  	$main::hlist2->itemCreate($main::hl_counter, 10, -text => "$fi");

	                if($newart) 
	                {
	                        $main::hlist2->itemCreate($main::hl_counter, 11, -text => "$newart");
	                }
	                if($newtra) 
	                {
	                        $main::hlist2->itemCreate($main::hl_counter, 12, -text => "$newtra");
	                }
	                if($newtit) 
	                {
	                        $main::hlist2->itemCreate($main::hl_counter, 13, -text => "$newtit");
	                }
	                if($newalb) 
	                {
	                        $main::hlist2->itemCreate($main::hl_counter, 14, -text => "$newalb");
	                }
	                if($newgen) 
	                {
	                        $main::hlist2->itemCreate($main::hl_counter, 15, -text => "$newgen");
	                }
	                if($newyear) 
	                {
	                        $main::hlist2->itemCreate($main::hl_counter, 16, -text => "$newyear");
	                }
	                if($newcom) 
	                {
	                        $main::hlist2->itemCreate($main::hl_counter, 17, -text => "$newcom");
	                }
                }
	}
	else 
	{
		if(!$s2) 
		{
			$s2 = $s1;
		}
		$main::hlist2->itemCreate($main::hl_counter, 1, -text => "$s1");

                if($main::LISTING == 0) 
                {
	                $main::hlist2->itemCreate($main::hl_counter, 2, -text => " -> ");
	                $main::hlist2->itemCreate($main::hl_counter, 3, -text => "$s2");
                }
	}
	$main::hl_counter++;
	$main::mw->update();
}

#--------------------------------------------------------------------------------------------------------------
# draw_list
#--------------------------------------------------------------------------------------------------------------

sub draw_list 
{
        my $columns = 4;
        if($main::id3_mode == 1) 
        {
        	$columns = 18;
        }

	if($main::hlist2) 
	{
		$main::hlist2->destroy;
	}

        our $hlist2 = $main::frm_right2 -> Scrolled
        (
		"HList",
		-scrollbars=>"osoe",
		-header => 1,
		-columns=>$columns,
		-selectbackground => 'Cyan',
		-browsecmd => sub 
		{
                	# when user clicks on an entry update global variables
               		$main::hlist_selection = shift;
               		($main::hlist_file, $main::hlist_cwd) = $main::hlist2->info("data", $main::hlist_selection);
               	},
		-command=> sub 
		{
                	# user has double clicked
			&hlist_cd($main::hlist_file, $main::hlist_cwd);
		}

	)
	->pack
	(
        	-side=>'bottom',
		-expand=>1,
		-fill=>'both'
	);
	$main::hlist2->header('create', 0, -text =>' ');		# these 2 columns are the same
	$main::hlist2->header('create', 1, -text =>'Filename');      # for norm & id3 mode

	if($main::id3_mode == 1) 
	{
		$main::hlist2->header('create', 2, -text => 'Artist');
		$main::hlist2->header('create', 3, -text => 'Track');
		$main::hlist2->header('create', 4, -text => 'Title');
		$main::hlist2->header('create', 5, -text => 'Album');
		$main::hlist2->header('create', 6, -text => 'Genre');
		$main::hlist2->header('create', 7, -text => 'Year');
		$main::hlist2->header('create', 8, -text => 'Comment');

		$main::hlist2->header('create', 9, -text => '#');
		$main::hlist2->header('create', 10, -text => 'New Filename');
		$main::hlist2->header('create', 11, -text => 'New Artist');
		$main::hlist2->header('create', 12, -text => 'New Track');
		$main::hlist2->header('create', 13, -text => 'New Title');
		$main::hlist2->header('create', 14, -text => 'New Album');
		$main::hlist2->header('create', 15, -text => 'New Genre');
		$main::hlist2->header('create', 16, -text => 'New Year');
		$main::hlist2->header('create', 17, -text => 'New Comment');
	}
	else 
	{
		$main::hlist2->header('create', 2, -text => '#');
		$main::hlist2->header('create', 3, -text => 'New Filename');
	}

        our $rc_menu = $main::hlist2->Menu(-tearoff=>0);

        $rc_menu -> command
        (
		-label=>"Manual Rename",
		-underline=> 1,
		-command=> sub 
		{
			&manual_edit($main::hlist_file, $main::hlist_cwd);
       		}
	);

        $main::hlist2->bind('<Any-ButtonPress-3>', \&show_rc_menu);
        $main::hlist2->bind('<Any-ButtonPress-1>',[\&hide_rc_menu, $rc_menu]);
        $main::hlist2->bind('<Any-ButtonPress-2>',[\&hide_rc_menu, $rc_menu]);

	&ls_dir;
}

sub show_rc_menu 
{
        my ($x, $y) = $main::mw->pointerxy;

        my $s = $main::hlist2->nearest($y - $main::hlist2->rooty);
        $main::hlist2->selectionClear();
        $main::hlist2->selectionSet($s);
        $main::hlist_selection = $s;
	$main::rc_menu->post($x,$y);
}

sub hide_rc_menu 
{
	my ($l,$m)=@_;
	$m->unpost();
}

sub hlist_cd 
{
	my $file = shift;
        my $wd = shift;

        my $path = $wd . "/" . "$file";

        if(-d $path) {
        	$main::dir = $path;
                chdir $main::dir or die "couldnt chdir to $main::dir\n";
	        &ls_dir;
	        return;
        }
        # not a valid path, ignore
        return;
}

1;

syntax highlighted by Code2HTML, v. 0.9.1