#!/bin/bash

# generate filename without directory:
FILENAME=`echo "$0"|sed 's!.*/!!'`
CAT=$(which cat)

print_help()
{
##########################################################################
$CAT <<EOF

  $0

  author:      Karl Voit, shellscript@Karl-Voit.at

  date:        2002-02-26

  version:     v0.1

  copyright:   GPL

  usage:       $FILENAME

  description: This script copies CDs (audio/data) onto CDR(W)s.

               It detects, if an old CD-image is found and asks,
               wether this one should be used or a new one should 
               be generated.

               Some personalisation has to be done within this script:
               filenames, CD-ROM/burner device, cddb-options

               Optionally, you'll be asked if you'd like to generate
               a CD-cover via the tool disc-cover (if installed).

               Only root can execute this script.

               Please email me suggestions for improvements and errors!
               I am still learning how to write good shell-scripts *g*

EOF
##########################################################################
}

## 2do:
##      * detection, if _audio_-CD-ROM is in drive (and don't ask for cover then)
##      * 


########################################################

## PERSONALISATION:


# filenames to use for the current CD-image and TOC-file:

CDCPY_TEMPFILEDIR="/home/root"
CDCPY_TEMPFILE="$CDCPY_TEMPFILEDIR/$FILENAME.tempfile.bin"
CDCPY_TOCFILE="$CDCPY_TEMPFILEDIR/$FILENAME.tempfile.toc"


# device-options for cdrdao:

CDCPY_CDROMDEVICE="0,3,0"
CDCPY_BURNERDEVICE="0,5,0"


# cddboptions for cdrdao (-> man cdrao) or empty, if no cddb is wanted:
# example (from cdrdap-manpage): freedb.freedb.org:/~cddb/cddb.cgi

CDCPY_CDDBOPTIONS=" --with-cddb --cddb-servers freedb.freedb.org:/cgi-bin/cddb.cgi "


# for the next two options: if disc-cover is installed:

# change this to USERNAME.USERGROUP for the cover-file:

CDCPY_COVERNAMEGROUP="vk.users"

# change this to the target-directory, where you keep your CD-covers as user:

CDCPY_COVERTARGETDIR="/home/vk/daten/musik/covers"

# change to "done", if you changed all options above to _your_ needs!

CDCPY_CONFIGURED="done"

########################################################


# --- normally you DON'T have to change anything below this line! --- #


########################################################
## FUNCTIONS:


myexit()
{
    echo "$FILENAME done."

    exit $1
}


## check, if some files needed are not found
testiffound()
{
  if [ -z "$2" ]; then
    print_help
    echo
    echo "$FILENAME: ERROR: a tool is missing: $1"
    echo "$FILENAME: aborting."
    myexit 2
  fi
}


## check, if caller = root-user
if [ $(id -u) != "0" ]; then
  print_help
  echo
  echo "$FILENAME: ERROR: You must be the superuser to run this script" >&2
  echo
  myexit 1
fi


## check, if everything was configured by the user
if [ ! "$CDCPY_CONFIGURED" = "done" ]; then
  print_help
  $CAT <<EOF

  $FILENAME: ERROR: Please make sure, that you checked/modified all options
  to meet your requirements!
  (see section "PERSONALISATION" in file $0)

EOF
  myexit 3
fi




CDCPY_mvcover()
{
    ## this function moves the generated cover-file (*.ps)
    ## to a user-directory and changes the file-owner:

    chown $CDCPY_COVERNAMEGROUP *.ps && \
    mv -b --suffix="_backupped_by_$FILENAME" --target-directory=$CDCPY_COVERTARGETDIR *.ps

}


## call-function for disc-cover
CDCPY_docover()
{

    ## disc-cover should be installed by now: (checked before)
    DISCCOVER=$(which disc-cover)

  $CAT <<EOF
===============================================================
                                                      $FILENAME

  Do you want to add a string to CD-cover?
  (e.g. year)

    Enter a string now

  or

    press <ENTER> without entering something.
  

EOF

  read ANSWER

  echo  

  if [ -z "$ANSWER" ]
  then
    $DISCCOVER -e
  else
    $DISCCOVER -e -a "$ANSWER"
  fi

  $CAT <<EOF
===============================================================
                                                      $FILENAME

  changing file-owner and copying to cover-directory ..."

EOF

  CDCPY_mvcover

  $CAT <<EOF

  ... copying done.

===============================================================
                                                      $FILENAME
  Back to $FILENAME:

===============================================================

EOF

}




CDCPY_askcover()
{
  $CAT <<EOF
===============================================================
                                                      $FILENAME

  Do you want to generate an audio-CD-cover via disc-cover?

    (y)es

  or

    (n)o (default)
  

EOF

  read ANSWER

  echo  
  
  case "$ANSWER" in
  
  y)
    CDCPY_docover ;;
  *)
    echo "  OK, maybe you're right." ;
    echo "  We don't need these fancy stuff anyway ..." ;
    echo ;;

  esac

}





CDCPY_readimage()
{


  if [ ! -z `which disc-cover` ]
  then
    CDCPY_askcover ;
  else
    $CAT <<EOF
===============================================================
                                                      $FILENAME

  NOTE: You may be interested in installing following cool
        program, that helps you on automatically generating 
        covers for audio-CDs: disc-cover

        And if disc-cover is installed, $FILENAME 
        will automatically generate a cover for you! ;-)

  HOMEPAGE: http://www.liacs.nl/~jvhemert/disc-cover/
  
===============================================================

EOF

  fi


  $CAT <<EOF
===============================================================
                                                      $FILENAME
  The CD-image will be generated from the given CD-ROM ...
  
===============================================================

EOF

  ## keepimage every time. I'll delete it afterwards - if necessary:
  #echo "$CDCPY_CDRDAO read-cd --device $CDCPY_CDROMDEVICE $CDCPY_CDDBOPTIONS --datafile $CDCPY_TEMPFILE"
  $CDCPY_CDRDAO read-cd --device $CDCPY_CDROMDEVICE $CDCPY_CDDBOPTIONS --datafile $CDCPY_TEMPFILE $CDCPY_TOCFILE

}




CDCPY_burnimage()
{

  $CAT <<EOF
===============================================================
                                                      $FILENAME
  The generated CD-image will be burned now ...
  
===============================================================

EOF

  ## keepimage every time. I'll delete it afterwards - if necessary:
  #echo "$CDCPY_CDRDAO write --device $CDCPY_BURNERDEVICE $CDCPY_TOCFILE"
  $CDCPY_CDRDAO write --device $CDCPY_BURNERDEVICE $CDCPY_TOCFILE

}





CDCPY_burnold()
{

  $CAT <<EOF
===============================================================
                                                      $FILENAME
  The found image-file will be burned now:
  
===============================================================

EOF

  ## keepimage every time. I'll delete it afterwards - if necessary:
  #echo "$CDCPY_CDRDAO write --device $CDCPY_BURNERDEVICE $CDCPY_TOCFILE"
  $CDCPY_CDRDAO write --device $CDCPY_BURNERDEVICE $CDCPY_TOCFILE

}





CDCPY_askdeleteimage()
{

  ## handle, what shall be done with generated image:

  $CAT <<EOF
===============================================================
                                                      $FILENAME

  Do you want to keep the image in order to burn another CDR(W)?
  
      (k)eep
      (d)elete (default)
  
EOF
  
  
  read ANSWER

  echo  
  
  case "$ANSWER" in
  
  k)
    echo "leaving $CDCPY_TEMPFILE and $CDCPY_TOCFILE.";
    myexit 0 ;;
  *)
    echo "removing $CDCPY_TEMPFILE and $CDCPY_TOCFILE ...";
    rm $CDCPY_TEMPFILE ;
    rm $CDCPY_TOCFILE ;
    myexit 0 ;;
  
  esac

  ## just 4 sure:  
  myexit 0

}





CDCPY_handlefoundimage()
{
  ## if an old image is found, handle, what shell be done!

  $CAT <<EOF
===============================================================
                                                      $FILENAME

  An old CD-image was found from a previous run.

  Do you want to

    (b)urn this one

    or

    (d)elete it and make a new CD

    or

    (c)ancel here and keep everything as it is?
  
EOF
  
  
  read ANSWER

  echo
  
  case "$ANSWER" in
  
  b)
    ## burn this found image
    CDCPY_burnold ;
    CDCPY_askdeleteimage ;;

  d)
    ## delete found image and make new one
    rm $CDCPY_TEMPFILE ;
    rm $CDCPY_TOCFILE ;
    CDCPY_readimage ;
    CDCPY_burnimage ;
    CDCPY_askdeleteimage ;;

  *)
    ## everything else: cancel
    myexit 0 ;;
  
  esac

}




########################################################
## SCRIPT:


## test for things that are needed:
CDCPY_CDRDAO=$(which cdrdao)
testiffound cdrdao $CDCPY_CDRDAO
CAT=$(which cat)
testiffound cat $CAT



if [ -f $CDCPY_TEMPFILE ]
then
## old image is found:

    CDCPY_handlefoundimage ;

## if no old CD-image was found, copy new cd:
else

    CDCPY_readimage ;
    CDCPY_burnimage ;
    CDCPY_askdeleteimage ;

fi

########################################################


myexit 0

#end











