#!/bin/bash #
# For every CD in the local cddb-spool generate a file with
# Tex-macros which includes scanned cover-images.
# This files are generated in ~/cdinhalt, for every
# cddb-genre there is a directory with the corresponding name.
JPGDIR=$HOME/public_html/cd-cover;
cd $HOME/cdinhalt
rm -rf $HOME/cdinhalt/*;
for dir in /opt/kde2/share/apps/kscd/cddb/*; do
  [ ! -d $(basename $dir) ] && mkdir $(basename $dir);
  (
    cd $(basename $dir); 
    pwd;
    for file in $dir/*; do
      cddbid=$(basename $file);
      if [ -f $file ] 
      then
	# generate the TeX-file and get the filename from the
	# disc-cover output.
        name=$(disc-cover -f $file -t mytex 2>&1 |\
	       grep "Output goes to" | sed -e 's/Output goes to //' |\
	       sed -e 's/"//g');
	[ ! -f $name ] && continue;
	echo  $cddbid : $name;

	jpegs="no";
	# Now we are looking, if there are jpg-Scans for this album.
	# These Scans are located in $JPGDIR, for every Album there
	# must be directory with the cddb-id as name.
	# In $JPGDIR/thumbs there must be also a directory with this
	# name, which contains a thumb for every jpg-file.
	# The files front.jpg und back.jpg are included first.

	# First we get the some macros of the Tex-File
	# and put them in a temporary file
	egrep 'currentartist|currenttitle' $name > $name.tmp;
	echo " " >> $name.tmp;

	# front.jpg und back.jpg are included
	if [ -f $JPGDIR/$cddbid/front.jpg ]
	then
	  echo \
	  "\\htmladdnormallink{\htmladdimg{../cd-cover/thumbs/$cddbid/front.jpg}}{../cd-cover/$cddbid/front.jpg}"\
	  >> $name.tmp;
	  jpegs="yes";
	fi
	if [ -f $JPGDIR/$cddbid/back.jpg ]
	then
	  echo \
	  "\\htmladdnormallink{\htmladdimg{../cd-cover/thumbs/$cddbid/back.jpg}}{../cd-cover/$cddbid/back.jpg}"\
	  >> $name.tmp;
	  jpegs="yes";
	fi

	# Now all the other images, if there are any
	for jpgfile in $JPGDIR/$cddbid/*.jpg
	do
	  [ ! -f $jpgfile ] && continue;
	  basejpgname=$(basename $jpgfile .jpg);
	  if [ $basejpgname = "front" ] || [ $basejpgname = "back" ]
	  then
	    continue;
	  else
	  echo \
	  "\\htmladdnormallink{\htmladdimg{../cd-cover/thumbs/$cddbid/$basejpgname.jpg}}{../cd-cover/$cddbid/$basejpgname.jpg}"\
	  >> $name.tmp;
	  jpegs="yes";
	  fi
	done
	# The rest of the tex-file is put in the temp-file and the
	# temp file is renamed.
	if [ "$jpegs" = "yes" ]
	then
	  echo " " >> $name.tmp;
	  egrep -v 'currenttitle|currentartist' $name >> $name.tmp;
	  mv $name.tmp $name;
	fi
	[ -f $name.tmp ] && rm -f $name.tmp;
      fi
    done
  );
done

# Generate a tex-file (.tex) for every subdirectory (if it
# contains any tex-file). The tex-file consist of a
# \cddbsection{genre}, aferwards all tex-files of this directory are
# \inputted.
# At the same time inhalt.tex is built. It consists of a preamble in
# the form of \input{vorspann.tex}, inputs all the genry-files and
# finally a file called nachspann.tex.
# All that is left is to write the two files vorspann.tex and
# nachspann.tex to generate a tex-file.
# In this files  the user must define \cddbsection and all the
# macros which are generated by disc-cover.
# For \htmladdnormallink you have to do a \usepackage{html}
echo "\\input{vorspann.tex}" > inhalt.tex;
for dir in * ; do
  if (test ! -d $dir )
  then
    continue;
  fi
  inhalt="nein";
  for file in $dir/*; do
    if [ -f $file ]; then
      if [ $inhalt = "nein" ]
      then
        echo "\\cddbsection{$dir}" > $dir.tex;
        echo "\\input{$dir.tex}" >> inhalt.tex;
        inhalt="ja";
      fi
      echo "\\input{/home/thomas/cdinhalt/$file}" >> $dir.tex;
    fi
  done;
  if [ "$inhalt" = "nein" ]
  then
    rmdir $dir
  fi
done
echo "\\input{nachspann.tex}" >> inhalt.tex;
#