#!/usr/bin/perl

# Generated by sh2p.pl on Wed Nov 14 16:18:39 2012

use warnings;
use strict;
use integer;

#< if [[ -z $1 ]]
#< then
#< echo "supply \'YYYY-MM-DD\ HH:MM\' \'quoted list of peeps\'"
#< exit 1
#< fi
if (  ! $ARGV[0] ) {
    print "supply \'YYYY-MM-DD\ HH:MM\' \'quoted list of peeps\'\n";
    exit (1);
}

#< script=./.ls
my $script = "./.ls";
#< if [[ ! -f $script ]]
#< then
#< echo ./.ls not found--aborting
#< exit 2
#< fi
if ( ! -f ("$script") ) {
    print "./.ls not found--aborting\n";
    exit (2);
}

#< date="$1"
my $date = "$ARGV[0]";
#< shift
shift;
#< time="$1"
my $time = "$ARGV[0]";
#< shift
shift;
#< peeps="$*"
my $peeps = join(" ",@ARGV)."";
#< if [[ -z $peeps ]]
#< then
#< echo supply quoted list of peeps
#< exit 3
#< fi
if ( !  ! $peeps ) {
    print "supply quoted list of peeps\n";
    exit (3);
}
#< echo ~/bin/toucher.pl "$date $time"print "",(glob("~/bin/toucher.pl")),"$date $time 7\n";
#< ~/bin/toucher.pl "$date $time" 7
(glob("~/bin/toucher.pl"$date $time"7"))


#< script=./.tt
$script = "./.tt";
#< [[ -f $script ]] && /bin/rm -f $script
( -f ("$script") ) && system ("/bin/rm -f $script"); 

#< [[ -f .number ]] || number.sh
( -f (".number") ) || system ("number.sh"); 
# **** INSPECT: ls should be replaced by something like glob or opendir/readdir/closedir or stat/lstat

#< ls -l *.mp3 |cut -d' ' -f6-7,8 |sed 's/\(.*:..\) /touch -d "\1" /' >.toucher
ls ("-l",(glob("*.mp3")),"|cut","-d' '","-f6-7,8","|sed",'s/\(.*:..\) /touch -d "\1" /',">",".toucher"); 
#< year=`/bin/date +%Y`
my $year =  `/bin/date +%Y` ;
# **** INSPECT: ls should be replaced by something like glob or opendir/readdir/closedir or stat/lstat
#< ls -latr *.mp3 |sed "s/.*$year/$year/" |sed 's/.*:../touch -d "&" /' >.toucher
ls ("-latr",(glob("*.mp3")),"|sed","s/.*$year/$year/","|sed",'s/.*:../touch -d "&" /',">",".toucher"); 
#< Track=0
my $Track = 0;
#< Artist='No Pangolins'
my $Artist = 'No Pangolins';
#< Year=`/bin/date +%Y`
my $Year =  `/bin/date +%Y` ;

#< for f in *.mp3
#< do
#< timestamp="`ls -l $f |cut -d' ' -f6-7 |sed 's/ /\\\\ /g'`"
#< echo $timestamp $f
#< Title=`echo $f |sed 's/.mp3//'`
#< Track=`expr $Track + 1`
#< Album=`pwd |xargs basename |sed 's/.*\([0-9]\{4\}\).*/\1/'`
#< Year=`echo $Album |sed 's/.*\([0-9]\{4\}\).*/\1/'`
#< Comment=`echo $* |sed 's/ /\\\\ /g'` #bogus test
#< if [[ -z $Comment ]]
#< then
#< echo "supply quoted list of peeps"
#< exit 0
#< fi
#< Genre=Cult
#< mp3gain $f
#< echo id3 -t "$Title" -T $Track -a \"$Artist\" -A "$Album" -y "$Year" -c "$Comment" -g "$Genre" $f
#< echo id3 -t "$Title" -T $Track -a \"$Artist\" -A "$Album" -y "$Year" -c "$Comment" -g "$Genre" $f >> $script
# **** INSPECT: Suspious conversion from expr
#< done
for my $f ((glob("*.mp3"))){
    my $timestamp = `ls -l $f |cut -d' ' -f6-7 |sed 's/ /\\\\ /g'`;
    print $timestamp,$f,"\n";
    my $Title = `echo $f |sed 's/.mp3//'`;
    $Track = $Track + 1;
    my $Album = `pwd |xargs basename |sed 's/.*\([0-9]\{4\}\).*/\1/'`;
    $Year = `echo $Album |sed 's/.*\([0-9]\{4\}\).*/\1/'`;
    my $Comment = `echo $* |sed 's/ /\\\\ /g'`;
 #bogus test
    if (  ! $Comment )     {
        print "supply quoted list of peeps\n";
        exit (0);
    }
    my $Genre = "Cult";
    system ("mp3gain $f"); 
    print "id3 -t ",$Title," -T ",$Track," -a \"$Artist\" -A ",$Album," -y ",$Year," -c ",$Comment," -g ",$Genre," ",$f,"\n";

    my $sh2p_handle;
    open($sh2p_handle,'>',"$script") or
         die "Unable to open $script: $!";
    print $sh2p_handle "id3 -t ",$Title," -T ",$Track," -a \"$Artist\" -A ",$Album," -y ",$Year," -c ",$Comment," -g ",$Genre," ",$f,"\n";
    close($sh2p_handle);
    undef $sh2p_handle;


}
# **** INSPECT: cat should be replaced by something like ExtUtils::Command::cat
#< cat $script
cat ("$script"); 
# **** INSPECT: ls should be replaced by something like glob or opendir/readdir/closedir or stat/lstat
#< ls -l $script
ls ("-l","$script"); 

# **** INSPECT: sourced file should also be converted
#< . $script
do $script;#< if [[ -f .toucher ]]
#< then
#< . .toucher
#< else
#< echo .toucher not found

# **** INSPECT: sourced file should also be converted
#< fi
if ( -f (".toucher") ) {
    do ".toucher";
}
else {
    print ".toucher not found\n";
}
#< makepl.sh $Album
system ("makepl.sh $Album"); 
#< ssh-add
system ("ssh-add"); 
#< bash ~/bin/uploader.sh
system ("bash ~/bin/uploader.sh"); 
