#!/bin/bash

#Here are the variables
NORMAL="backup was sucessfull."
ERREUR="###backup failed.###"
ERROR_CODE=0
DATE=$( date +%j )
DATEFILE=$( cat /home/charlie/.datefile )

#The first function. It backup the files that have been modified since the last backup
#If generate an error code if something goes wrong

function backup {
     cp -Ru /windows/3dsmax/scenes/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /mnt/charlie/publications/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /mnt/charlie/img/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /mnt/charlie/mp3/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /home/charlie/Mail/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /home/charlie/.mozilla/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi
}
#The second function. It removes all the files in the backup directory and create a new backup. If I delete some files in source directory, they will be deleted in backup directory too.

function new_backup {
     rm -r /mnt/charlie/backup
     mkdir /mnt/charlie/backup
     
     cp -Ru /windows/3dsmax/scenes/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi
     
     cp -Ru /mnt/charlie/publications/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /mnt/charlie/img/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /mnt/charlie/mp3/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /home/charlie/Mail/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi

     cp -Ru /home/charlie/.mozilla/ /mnt/charlie/backup/
       if [ $? != 0 ]; then
           ERROR_CODE=1
       fi
#It stores the actual date (number of the day in the year) in a file  
     date +%j > .datefile
}

#Third function. It list all the files in all subdirectories and put it in a file. It's helpfull to know what's on that directory

function list {
    ls -Rsh /mnt/charlie/backup > /mnt/charlie/backup/LIST.TXT
}

#It launch backup function. Once a month it launch new_backup function.
#It compare the actual date and the date stored in the .datefile.
#If there are some error it returns $ERREUR, else $NORMAL
 
if [ $DATE -gt $(($DATEFILE+30)) -o $DATE -lt $(($DATEFILE)) ]; then
     new_backup
     if [ $ERROR_CODE != 0 ]; then
       echo $ERREUR
     else
       echo $NORMAL
     fi
else
     backup
     if [ $ERROR_CODE != 0 ]; then
       echo $ERREUR
     else
       echo $NORMAL
     fi
fi

#New variables. If size of backup directory is more than 500M, then it told to burn a CD and launch list function.

for x in /mnt/charlie/backup /mnt/charlie/backup/scenes; do
BURN="Il est temps de graver $x sur un CD"

let Y=$(du -ls $x | awk '{print $1/1024}' | sed 's/\..*//')
     if [ $Y -gt 500 ]; then
       SIZE=$"("$Y"M)"
       echo $BURN $SIZE
       list
     fi
done

#The backup directory is ready to be burn.






