#!/bin/bash
################################################################################
# Fichier : mp3rand.sh
# Auteur  : Sebastien Beaugrand
# Date    : 01/07/2008
# Url     : http://www.chez.com/beaugrand/
################################################################################
#
# Les albums doivent etre ranges dans des repertoires commencants par
# part01, part02, etc. Par exemple :
#
# part01 - Metal/
# part02 - Metal/
# part03 - Metal/
# part04 - Metal/
# part05 - Metal/
# part06 - Metal/
# part07 - Metal/
# part08 - Indus/
# part09 - Indus/
# part10 - Indus/
# part11 - Autre/
# part12 - Autre/
# part13 - Autre/
# part14 - Autre/
#
# A chaque repertoire doit etre associe' un poids :
# part : 1 2 3 4 5 6 7       1 2 3 4
weights="8 7 6 5 4 3 2 6 4 2 3 2 1 0"
#
# Les albums doivent etre des repertoires. Par Exemple :
#
# Abigor - 1995 - Nachthymnen (From The Twilight Kingdom)/
# Absu - 1997 - The Third Storm Of Cythraul/
# Accept - 1985 - Metal Masters/
# Accept - 1989 - Eat The Heat/
# Agathodaimon - 1999 - Higher Art Of Rebellion/
# Akhenaton - 1995 - Divine Symphonies/
# Alice Cooper - 1989 - The Beast Of Alice Cooper/
# Alice Cooper - 1989 - Trash/
# ...
#
# Dans chaque album il doit y avoir une liste 00.m3u. Par exemple :
#
# 00.m3u
# 01 - Unleashed Axe-age.mp3
# 02 - Scars In The Landscape Of God.mp3
# 03 - Reborn Through The Gates Of Three Moons.mp3
# 04 - Dornen.mp3
# 05 - As Astral Images Darken Reality.mp3
# 06 - The Dark Kiss.mp3
# 07 - I Face The Eternal Winter.mp3
# 08 - Revealed Secrets Of The Whispering Moon.mp3
# 09 - A Frozen Soul In A Wintershadow.mp3
#

if [ -n "$MP3DIR" ]; then
  dir="$MP3DIR"
else
  dir=/mnt/mp3/mp3
fi
list=mp3.list
log=~/.mp3log
tmpfile=/tmp/mp3list.tmp
lastdate=~/.mp3date

# Usage
if [ "$1" = "-h" ]; then
  cat <<EOF

Usage: ${0##*/} [options]
Options:
-i [artiste]    informations sur les albmus de l'artiste ou sur le dernier album
-r              generation de la liste
EOF
  exit
fi

if [ ! -d "$dir" ]; then
  echo "$dir n'est pas un repertoire"
  exit -1
fi

# Reset
if [ "$1" = "-r" ] || [ ! -r "$dir/$list" ]; then
  echo "generation du fichier $dir/$list ..."
  cd "$dir"
  find . -name 00.m3u -print | sort | cut -c3- > "$dir/$list"
  exit
fi

# Info
if [ "$1" = "-i" ]; then
  cd "$dir"
  if [ -n "$2" ]; then
    album=`grep "/$2 -" $log | tail -n 1 | cut -d "/" -f 2`
    artist="$2"
  else
    album=`tail -n 1 $log | cut -d "/" -f 2`
    artist=`echo "$album"  | cut -d "-" -f 1 | sed 's/ $//'`
    grep "/$album" $log | tail -n 2 | head -n 1
    echo
  fi

  grep "/$artist - " $list | sed 's/\/00\.m3u//' | sed 's/\ /\\\ /g' |\
  sed 's/(/\\(/g' | sed 's/)/\\)/g' | sed "s/'/\\\'/g" |\
  gawk '{ print "grep "$0" mp3.list | sed \"s/^/           /\" |\
    sed \"s/\\/00\\.m3u//\" | cat - ~/.mp3log | grep "$0"$ | tail -n 1" }' |\
    gawk '{ system($0) }'
  exit
fi

# XMMS
status=`xmms-shell -e status 2>&1 | head -n 1 | cut -c-19`
if [ "$status" = "XMMS is not running" ]; then
  echo "starting XMMS..."
  soundwrapper xmms&
  exit
fi

updatelog()
{
   jour=`date +%F`
  heure=`date +%R`
  last=`echo "$jour $heure" | sed 's/-/ /g' | sed 's/:/ /'`
  prev=`cat $lastdate 2> /dev/null | sed 's/-/ /g' | sed 's/:/ /'`
  if [ -n "$prev" ]; then
    i=0
    for a in $prev; do
      prevtab[$i]=`echo $a | gawk 'END { print $0+0 }'`
      i=$(($i+1))
    done
    i=0
    for a in $last; do
      lasttab[$i]=`echo $a | gawk 'END { print $0+0 }'`
      i=$(($i+1))
    done
    min=$(((${lasttab[0]}-${prevtab[0]})*525600+
           (${lasttab[1]}-${prevtab[1]})*44640+
           (${lasttab[2]}-${prevtab[2]})*1440+
           (${lasttab[3]}-${prevtab[3]})*60+
           (${lasttab[4]}-${prevtab[4]})))
    if [ -n "$min" ]; then
      if (($min < 2)); then
        echo "derniere selection il y a $min minute"
      else
        echo "derniere selection il y a $min minutes"
      fi
      if (($min < 5)); then
        echo "effacement de la derniere selection"
        nl=`wc -l $log | cut -d ' ' -f 1`
        if (($nl > 0)); then
          nl=$(($nl-1))
          head -n $nl $log > $log.tmp
          mv $log.tmp $log
        fi
      fi
    fi
  fi
  echo $jour" $m3u" | sed 's/\/00\.m3u//' >> $log
  echo "$last" > $lastdate
}

# Main
max=0
m=`ls "$dir" | grep part | tail -n 1`
m=`echo ${m:4:2} | gawk 'END { print $0+0 }'`
n=0
for i in $weights; do
  max=$(($max+$i))
  n=$(($n+1))
done
if (($n != $m)); then
  echo "les nombres de poids ne correspondent pas"
  exit
fi
rand=$(($RANDOM*$max/32768+1))
part=1
max=0
for i in $weights; do
  max=$(($max+$i))
  if (($rand <= $max)); then
    break;
  fi
  part=$(($part+1))
done

part="part"`printf %02d $part`
grep $part "$dir/$list" > $tmpfile
max=`wc -l $tmpfile | cut -d ' ' -f 1`
num=$(($RANDOM*$max/32768+1))
m3u=`head -n $num $tmpfile | tail -n 1`
rm $tmpfile

updatelog

album=`tail -n 1 $log | cut -d "/" -f 2`
grep "/$album" $log | tail -n 2 | head -n 1
m3u=`echo "$dir/$m3u" | sed 's/ /\\\ /g' | sed "s/'/\\\\\'/g"`
xmms-shell -e clear
xmms-shell -e "load $m3u" > /dev/null
xmms-shell -e play > /dev/null

# Fin du fichier mp3rand.sh