#!/bin/bash

function selectline() {
    RANDOM=$RANDOM   #its dodgy as is, try to make is ever so slightly better
    count=`echo "$1" | wc -l`
    rnum=`echo $RANDOM  '*' $count / 32768 + 1| bc`
    string=`echo "$1" | head -n "$rnum" | tail -n 1`
    echo "$string"
}

line=`cat $HOME/.backgroundrc`
line=`selectline "$line"`

dir=`echo "$line" | sed 's!\(.*\) *|.*|.*!\1!'`
command=`echo "$line" | sed 's!.*| *\(.*\) *|.*!\1!'`
pattern=`echo "$line" | sed 's!.*|.*| *\(.*\)!\1!'`


cd $dir

files=`eval ls "$pattern"`
file=`selectline "$files"`


$command "$file" &
