2022-02-01: Gimme gimme gimme

Gimme is one of those absurdly useful little programs that I write once and then forget about. I should make it more permanent.

The idea behind it is simple. Say you have a folder full of things. Say a big bunch of .dot files you want to turn into pretty .png charts, or a bunch of .zip files you want to rename into .cbz's so you can stick 'em on your comic reader. You could use a simple loop like:

for f in *.dot; do dot -Tpng "$f" ${f%%.*}.png; done
for f in *.zip; do mv "$f" "$(basename "$f" .zip).cbz"; done

But who seriously has the time to remember the commands. What I *really* want is to be able to yell:

gimme png
gimme cbz

And have something figure out how to transform all the files for me.

I present: gimme.

  #! /usr/bin/env bash
  make='/usr/bin/env gmake' 
  config="${HOME}/.config/gimme/Makefile"

  makeflags="--keep-going" 

  for extension in $@; do
      for f in *; do
          target="${f%%.*}.${extension}"
          ${make} -f "${config}" ${makeflags} "${target}" 2>/dev/null
      done
  done

Gimme works by having a Makefile hidden in the config directory (~/.config/gimme/Makefile) that contains all the rules for converting files that gimme needs to know about. The gimme script itself just switches out the extension for the one you want for every file it can see and sees if make can find a way through the makefile for it. My gimmefile looks like:

  %.cbz: %	; zip -r $@ $<

  %.jpeg: %.jpg	; cp $< $@
  %.jpeg: %.png	; convert $< $@
  %.jpeg: %.pdf	; convert "$<[0]" $@

  %.mp4: %.avi	; ffmpeg -i "$<" -c:v copy -c:a copy -y "$@"
  %.mp4: %.gif	; ffmpeg -i "$<" -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" "$@"
  %.mp4: %.mov	; ffmpeg -i "$<" -vcodec h264 -acodec mp2  -qscale 0 "$@"
  %.mp4: %.mkv	; ffmpeg -i "$<" -vcodec h264 -acodec mp2  -qscale 0 "$@"

  %.pdf: %.dot	; dot -Tpdf $< -o $@
  %.pdf: %.tex	; latexmk -pdf $<
  %.png: %.pdf	; convert "$<[0]" $@

  %.png: %.dot	; dot -Tpng $< -o $@
  %.png: %.jpeg	; convert $< >$@

  %.tar.gz: %	; tar czf $@ $<

  %.zip: %	; zip -r $@ $<

It is surprisingly powerful. It is also prone to breaking and going wrong but who cares? Its a tool for doing bulk conversion and I love it.