~kota/dots

d50ebaed2897fcd809cd954fad79b2a3746178ca — Dakota Walsh 6 months ago ec27605
new scripts
7 files changed, 374 insertions(+), 131 deletions(-)

D bin/bin/aart
A bin/bin/aart
M bin/bin/backup
A bin/bin/exfatify
A bin/bin/unsym
A bin/bin/vidir2
A bin/bin/whatcd
D bin/bin/aart => bin/bin/aart +0 -131
@@ 1,131 0,0 @@
#!/bin/sh

music_dir=$(xdg-user-dir MUSIC)

AART_TEMP=${AART_TEMP:-"/tmp/aart"}
AART_NOART=${XDG_CACHE_HOME:-"$HOME/.cache"}/aart/noart.png
AART_VIEWER=${AART_VIEWER:-"sxiv -b "}

extract_art() {
	current_path=$music_dir/$1

	# use ffmpeg to extract album art embedded in music files. copies the same
	# codec to (hopefully) not modify the file at all. Not sure if I have the
	# options for it set correctly, especially the format being image2 for
	# everything.
	ffmpeg -loglevel error -y -i "$current_path" -map "v?" -c copy -f image2 "$AART_TEMP" || return 1

	art="$AART_TEMP"
	printf "found embedded art\n"
	return 0
}

find_folder_art() {
	for art in "$album_dir"/folder.*; do
		[ -f "$art" ] || continue
		printf "found folder art\n"
		return 0
	done

	printf "no folder.*\n"
	return 1
}

find_cover_art() {
	for art in "$album_dir"/cover.*; do
		[ -f "$art" ] || continue
		printf "found cover art\n"
		return 0
	done

	printf "no cover.*\n"
	return 1
}

copy_art() {
	# get diretory of currently playing song
	album_dir="$music_dir/$1"
	album_dir=${album_dir:-}
	album_dir=${album_dir%%"${album_dir##*[!/]}"}
	[ "${album_dir##*/*}" ] && album_dir=.
	album_dir=${album_dir%/*}
	album_dir=${album_dir%%"${album_dir##*[!/]}"}

	find_folder_art || find_cover_art || return 1

	printf "found external art\n"
	return 0
}

no_art() {
	printf "no art found! falling back to default\n"
	art="$AART_NOART"
}

set_art() {
	# attempt to get the album art in various ways
	printf "finding art for %s\n" "$(mpc current)"
	extract_art "$1" || copy_art "$1" || no_art

	# copy the art to the file that's open in the image viewer
	cp -f "$art" "$AART_TEMP"

	# make output a bit easier to read
	printf "\n"
}

check_pid() {
	if [ ! -e /proc/"$viewer_pid" ]; then
		# clean up temp file
		rm "$AART_TEMP"

		printf "viewer missing! exiting\n"
		exit 1
	fi
}

first_start() {
	# check if mpd is running
	pgrep mpd >/dev/null || exit 1

	# check status of mpd, if it's stopped then wait for mpd to start playing
	# a song. Maybe only check for playing, I leave mpd paused quite often
	mpc status | grep -o "playing\|paused" > /dev/null || mpc current --wait > /dev/null

	# set the art
	set_art "$(mpc -q current -f %file%)"

	# open image viewer on the art and store the pid
	$AART_VIEWER "$AART_TEMP" & viewer_pid=$!
}

close_script() {
	# clean up temp file
	rm "$AART_TEMP"

	# kill the viewer
	kill $viewer_pid

	# remove weird artifact when closing
	printf "\n"
	exit 0
}

first_start

trap close_script INT

while true;
do
	old_song="$(mpc current -f %file%)"

	# wait for a player event, if mpd closes then exit
	mpc -q idle player > /dev/null || close_script

	# make sure viewer is still open
	check_pid

	# only change the art if the song changes
	new_song="$(mpc current -f %file%)"
	[ "$old_song" != "$new_song" ] && set_art "$new_song"
done

A bin/bin/aart => bin/bin/aart +1 -0
@@ 0,0 1,1 @@
/home/kota/git/aart/aart
\ No newline at end of file

M bin/bin/backup => bin/bin/backup +1 -0
@@ 15,6 15,7 @@ borg create                             \
    ~/pics                              \
    ~/music                             \
    ~/g                                 \
    ~/.local/opt/MultiMC                \

# borg prune                              \
#     --list                              \

A bin/bin/exfatify => bin/bin/exfatify +10 -0
@@ 0,0 1,10 @@
#!/bin/sh

[ -z "$1" ] && printf 'Need a directory to exfatify\n' && exit 1

bad_files=$(fd '\\|:|\*|\?|"|<|>|\|' "$1")

echo "$bad_files" | while IFS= read -r bad_file; do
	good_file=$(echo "$bad_file" | sed 's/\"\|\*\|:\|<\|>\|?\|\\\||//')
	mv "$bad_file" "$good_file"
done

A bin/bin/unsym => bin/bin/unsym +19 -0
@@ 0,0 1,19 @@
#!/bin/sh
set -e
for link; do
    test -h "$link" || continue

    dir=$(dirname "$link")
    reltarget=$(readlink "$link")
    case $reltarget in
        /*) abstarget=$reltarget;;
        *)  abstarget=$dir/$reltarget;;
    esac

    rm -fv "$link"
    cp -afv "$abstarget" "$link" || {
        # on failure, restore the symlink
        rm -rfv "$link"
        ln -sfv "$reltarget" "$link"
    }
done

A bin/bin/vidir2 => bin/bin/vidir2 +333 -0
@@ 0,0 1,333 @@
#!/usr/bin/perl

=head1 NAME

vidir - edit directory (fork with symlinks support)

=head1 SYNOPSIS

B<vidir> [--verbose -l] [directory|file|-] ...

=head1 DESCRIPTION

vidir allows editing of the contents of a directory in a text editor. If no
directory is specified, the current directory is edited. 

When editing a directory, each item in the directory will appear on its own
numbered line. These numbers are how vidir keeps track of what items are
changed. Delete lines to remove files from the directory, or
edit filenames to rename files. You can also switch pairs of numbers to
swap filenames.

Note that if "-" is specified as the directory to edit, it reads a list of
filenames from stdin and displays those for editing. Alternatively, a list
of files can be specified on the command line.

=head1 OPTIONS

=over 4

=item -v, --verbose

Verbosely display the actions taken by the program.

=item -l, --links

Allow to modify symlinks

=back

=head1 EXAMPLES

=over 4

=item vidir

=item vidir *.jpeg

Typical uses.

=item find | vidir -

Edit subdirectory contents too. To delete subdirectories,
delete all their contents and the subdirectory itself in the editor.

=item find -type f | vidir -

Edit all files under the current directory and subdirectories.

=back

=head1 ENVIRONMENT VARIABLES

=over 4

=item EDITOR

Editor to use.

=item VISUAL

Also supported to determine what editor to use.

=back

=head1 AUTHOR

Copyright 2006 by Joey Hess <joey@kitenet.net>

Modified 2011-2012 by Damien Robert <damien.robert__github@normalesup.org>

Licensed under the GNU GPL.

=cut

use File::Spec;
use File::Temp;
use Getopt::Long;
use strict;

my $error=0;

my $verbose=0;
my $linktargets=0;
my $opt_recursive=0;
my $opt_mkdir=0;
my $opt_force=0;
my $opt_interactive=0;
my $opt_noclobber=0;
if (! GetOptions("verbose|v" => \$verbose,
    "linktargets|l" => \$linktargets,
                "recursive|r" => \$opt_recursive,
                "dirs|d" => \$opt_mkdir,
                "force|f" => \$opt_force,
                "interactive|i" => \$opt_interactive,
                "no_clobber|n" => \$opt_noclobber)) {
  die "Usage: $0 [--verbose --linktargets] [directory|file|-]\n";
}
#By defaut: on copy interactive mode unless -f is given
#On rm no -i given, unless -r is passed (unless -f is passed)
my $cpoptions="-vi"; my $rmoptions="-v";
if ($opt_recursive) { $cpoptions="${cpoptions}r"; $rmoptions="${rmoptions}ri"};
if ($opt_force) { $cpoptions="${cpoptions}f"; $rmoptions="${rmoptions}f" };
if ($opt_interactive) { $cpoptions="${cpoptions}i"; $rmoptions="${rmoptions}i" };
if ($opt_noclobber) { $cpoptions="${cpoptions}n" };

my @dir;
if (! @ARGV) {
  push @ARGV, "."
}
foreach my $item (@ARGV) {
  if ($item eq "-") {
    push @dir, map { chomp; $_ } <STDIN>;
    close STDIN;
    open(STDIN, "/dev/tty") || die "reopen: $!\n";
  }
  elsif (-d $item) {
    $item =~ s{/?$}{/};
    opendir(DIR, $item) || die "$0: cannot read $item: $!\n";
    push @dir, map { "$item$_" } sort readdir(DIR);
    closedir DIR;
  }
  else {
    push @dir, $item;
  }
}

if (grep(/[[:cntrl:]]/, @dir)) {
  die "$0: control characters in filenames are not supported\n";
}

my $tmp=File::Temp->new(TEMPLATE => "vidir.XXXXX", DIR => File::Spec->tmpdir);
open (OUT, ">".$tmp->filename) || die "$0: cannot create ".$tmp->filename.": $!\n";

my %item;
my %curitem; #where item{$i} is right now
my %iteml;
my $c=0;
foreach (@dir) {
  next if /^(.*\/)?\.$/ || /^(.*\/)?\.\.$/;
  $item{++$c}=$_;
  $curitem{$c}=$_;
  if ( $linktargets && -l $_ ) {
    my $l = readlink($_);
    $iteml{$c}=$l;
    print OUT "$c\t$_\t->\t$l\n";
  }
        elsif (-d $_ && ! -l $_) {
    print OUT "$c\t$_/\n";
          $item{$c}="$_/";
          $curitem{$c}="$_";
        }
  else {
    print OUT "$c\t$_\n";
  }
}
@dir=();
close OUT || die "$0: cannot write ".$tmp->filename.": $!\n";

my @editor="vi";
if (-x "/usr/bin/editor") {
  @editor="/usr/bin/editor";
}
if (exists $ENV{EDITOR}) {
  @editor=split(' ', $ENV{EDITOR});
}
if (exists $ENV{VISUAL}) {
  @editor=split(' ', $ENV{VISUAL});
}
my $ret=system(@editor, $tmp);
if ($ret != 0) {
  die "@editor exited nonzero, aborting\n";
}

open (IN, $tmp->filename) || die "$0: cannot read ".$tmp->filename.": $!\n";
while (<IN>) {
  chomp;
  if (/^(\d+)\t{0,1}(.*)/) {
    my $num=int($1);
    my $name=$2;
    my $link;
    if ( $linktargets && $name=~/(.*)\t\-\>\t(.*)/) {
      $name=$1;
      $link=$2;
    }
    if (! exists $item{$num}) {
      if ( exists $curitem{$num}) {
        my $curfile=$curitem{$num};
        print "cp $cpoptions $curfile $name\n" if ($verbose);
        system("cp",$cpoptions,$curfile,$name)==0 or warn "Error in cp: $!\n";
        next;
      }
      else {
        warn "$0: unknown item number $num\n";
        next;
      }
    }
    if ($name ne $item{$num} and length $name) {
      my $src=$curitem{$num};
      
      if (! (-e $src || -l $src) ) {
        print STDERR "$0: $src does not exist\n";
        delete $item{$num};
        next;
      }

      # deal with swaps
      if (-e $name || -l $name) {
        my $tmp = $name;
        $tmp =~ s{/?$}{};
        $tmp=$tmp."~";
        my $c=0;
        while (-e $tmp || -l $tmp) {
          $c++;
          $tmp=$name."~$c";
        }
        if (! rename($name, $tmp)) {
          print STDERR "$0: failed to rename $name to $tmp: $!\n";
          $error=1;
        }
        else {
          print "'$name' -> '$tmp'\n" if ($verbose);
          if (-d $tmp) {
            $tmp=$tmp."/";
            foreach (values %curitem) {
              s/^\Q$name\E/$tmp/;
            }
          }
          else {
            foreach my $item (keys %curitem) {
              if ($curitem{$item} eq $name) {
                $curitem{$item}=$tmp;
              }
            }
          }
        }
      }

      if ($opt_mkdir) {
        ( undef , my $dir, undef ) = File::Spec->splitpath($name);
        print "mkdir -p $dir\n" if ($verbose);
        system("mkdir","-p",$dir);
      }
      if (! rename($src, $name)) {
        print STDERR "$0: failed to rename $src to $name: $!\n";
        $error=1;
      }
      else {
        $curitem{$num}=$name;
        if (-d $name) {
          foreach (values %curitem) {
            s/^\Q$src\E/$name/;
          }
        }
        if ($verbose) {
          print "'$src' => '$name'\n";
        }
      }
    };
    if ($linktargets and $link ne $iteml{$num} and length $link) {
      my $oldlink=$iteml{$num};
      if ($link ne $oldlink) {
        if (! -e $link ) {
          print STDERR "$0: Warning, want to link $link to $name, but $link does not exist\n";
        }
        if (-e $name and ! -l $name ) {
          print STDERR "$0: Don't link $link to $name, $name is not a symlink\n";
          $error=1;
        }
        else {
          unlink($name);
          if (! symlink($link, $name) )         {
            print STDERR "$0: failed to create link $name -> $link\n";
            $error=1;
          }
          else {
            if ($verbose) {
              print "Changing link: '$oldlink' => '$link' in '$name'\n";
            }
          }
        }
      }
    };
    delete $item{$num};
  }
  elsif (/^\s*$/) {
    # skip empty line
  }
  else {
    die "$0: unable to parse line \"$_\", aborting\n";
  }
}
close IN || die "$0: cannot read ".$tmp->filename.": $!\n";
unlink($tmp.'~') if -e $tmp.'~';

sub rm {
  my $file = shift;
        if ($rmoptions eq "-vi" ) {
          print "rm $rmoptions $file\n" if ($verbose);
          return !system("rm",$rmoptions,$file);
        }
        else {
          if (-d $file && ! -l $file) {
                  return rmdir $file;
          }
          else {
                  return unlink $file;
          }
        }
}

foreach my $item (reverse sort values %item) {
  if (! rm($item)) {
    print STDERR "$0: failed to remove $item: $!\n";
    $error=1;
  }
  else {
    if ($verbose) {
      print "removed '$item'\n";
    }
  }
}

exit $error;

A bin/bin/whatcd => bin/bin/whatcd +10 -0
@@ 0,0 1,10 @@
#!/bin/sh

# get random artists, pass through awk to remove empty lines
artist=$(mpc list albumartist | awk NF | shuf | fzf)

# random list of albumns by said artist
album=$(mpc list album "((artist == \"$artist\"))" | shuf | fzf)

# play album
mpc findadd album "$album"