dotfiles

<-- duh.
git clone https://hhvn.uk/dotfiles
git clone git://hhvn.uk/dotfiles
Log | Files | Refs | Submodules | LICENSE

set (2370B)


      1 #!/bin/rc
      2 
      3 usage = (
      4 'usage: tag/set [-n] [-k] [--artist artist] [--date date]'
      5 '               [--title title] [--album album]'
      6 '               [--track track] [--comment comment]'
      7 '               files...'
      8 '       tag/set [-n] [-d] files...'
      9 ''
     10 'tag/set removes and replaces metadata from audio/video files.'
     11 ''
     12 '-n Dry run. Only print commands to be executed.'
     13 '-d Remove all data without replacing. Cannot be used with other options'
     14 '   except -n. This option was added to prevent accidental destruction.'
     15 '-k Preserve all metadata that is not specified'
     16 ''
     17 '--artist, --date, --title, --album, --track, and --comment'
     18 '  Specify replacement metadata.'
     19 )
     20 
     21 clear = (-map_metadata -1)
     22 files = ()
     23 tmp = ()
     24 margs = ()
     25 
     26 fn sigexit {
     27 	for (f in $tmp) {
     28 		rm -f $f >[2]/dev/null >[1=2]
     29 	}
     30 }
     31 
     32 fn usage {
     33 	for (line in $usage) {
     34 		printf '%s\n' $line >[1=2]
     35 	}
     36 	exit 2
     37 }
     38 
     39 fn tmp {
     40 	if (~ $dry 1) {
     41 		echo tmpfile
     42 	} else {
     43 		location = /tmp/$pid.`$nl{echo $1 | tr / _} {
     44 			echo $location
     45 			tmp = ($tmp $location)
     46 		}
     47 	}
     48 }
     49 
     50 fn addmd {
     51 	if (~ $2 ()) {
     52 		usage
     53 	}
     54 	margs = ($margs -metadata `$nl{echo $1 | sed 's/^--//'} ^ '=' ^ $2)
     55 }
     56 
     57 fn printcmd {
     58 	whatis $1 >[2=1] | sed 's/^[^=]*=(//;s/)$//'
     59 }
     60 
     61 while (!~ $#* 0) {
     62 	switch ($1) {
     63 	case --artist
     64 		addmd $*
     65 		shift
     66 	case --date
     67 		addmd $*
     68 		shift
     69 	case --title
     70 		addmd $*
     71 		shift
     72 	case --album
     73 		addmd $*
     74 		shift
     75 	case --track
     76 		addmd $*
     77 		shift
     78 	case --comment
     79 		addmd $*
     80 		shift
     81 	case -k
     82 		clear = ()
     83 	case -d
     84 		delete = 1
     85 	case -n
     86 		dry = 1
     87 	case -*
     88 		printf 'No such option: %s\n' $1 >[1=2]
     89 		usage
     90 	case *
     91 		files = ($files $1)
     92 	}
     93 	shift
     94 }
     95 
     96 if (~ $files ()) {
     97 	printf 'No files specified\n' >[1=2]
     98 	exit 1
     99 }
    100 
    101 # --delete cannot be specified along with other args
    102 if (~ $delete 1 && {!~ $margs () || ~ $clear ()}) {
    103 	printf '--delete cannot be specified along with other args\n'
    104 	exit 1
    105 }
    106 
    107 if (~ $margs () && !~ $delete 1) {
    108 	printf 'Nothing to do\n' >[1=2]
    109 	exit 1
    110 }
    111 
    112 for (f in $files) {
    113 	out = `$nl{tmp $f}
    114 	fcmd = (ffmpeg -y -nostdin -i $f $clear -c copy $margs $out(1))
    115 	mcmd = (mv $out(1) $f)
    116 	if (~ $dry 1) {
    117 		printcmd fcmd
    118 		printcmd mcmd
    119 	} else if (!test -f $f) {
    120 		printf 'No such file: %s\n' $f >[1=2]
    121 	} else {
    122 		if (~ $delete 1) {
    123 			echo Removing metadata...
    124 		} else if (~ $clear ()) {
    125 			echo Adding metadata...
    126 		} else {
    127 			echo Replacing metadata...
    128 		}
    129 		$fcmd
    130 		echo Replacing file...
    131 		$mcmd
    132 	}
    133 }