#!/bin/bash##Definie all functions needed for BirthdayReminder##BASE_DIR="/home/${USER}/.birthday.d"VERSION="1.1"SOFT_NAME="BirthdayLauncher"name='' # Set $namebirth='' # Set $birthmois='' # Set $moismonth='' # Set $month######################### ################# WindowSet ###################Define WindowSet. Depending on the context, this function allow you to call a ##custom window##HowTo use WindowSet## WindowSet $var##Where $var is the Window name you want to setfunction WindowSet{## Getting WindowSet first argucase $1 in ## Set The Intro Window Intro) intro="`zenity --info --title $SOFT_NAME:$VERSION --text "bienvenue dans l'éditeur de dates d'anniversaires"`" ;; ## Set The Choice window and return $choice Choice) choice="`zenity --list --text "Quelle action souhaitez vous effectuer" --radiolist --column "" --column action /home/${USER}/Desktop/bin/BirthdayLauncher "1 Ajouter un anniversaire pour ${USER}" ${BASE_DIR}/bin/BirthdayLauncher "2 verifier les entrées pour ${USER}"`" ;; ## Set The Name Window and return $name Name) name="`zenity --entry --title $SOFT_NAME:$VERSION --text "Saisissez le nom de la personne dont vous voulez ajouter l'anniversaire"`" ;; ## Set The Birth Window and return $birth Birth) birth="`zenity --calendar --title "Entrée pour $name" --date-format %d%m`" ;; ## Set The Birthday Window and return $birthday Birthday) birthday="`zenity --notification --title $name:$jour --text "$MESSAGE"; zenity --info --title $name:$jour --text "$MESSAGE" `" ;; ## Set The Error Window and return $error Error) error="`zenity --warning --title Erreur --text "$MESSAGE"`" ;; ## Set The Warning Window and return $warning Warning) warning="`zenity --warning --title Attention --text "$MESSAGE"`" ;; ## Set The Info Window and return $info Info) info="`zenity --info --title Information --text "$MESSAGE"`" ;; ## Set other and return $error *) error="`zenity --warning --title "Erreur Inconnue"--text "bin la je sais pas trop"`" ;;esac}################## MakeChoice ###################ouvre une fenetre de choix pour lancer les differents applicationsfunction MakeChoice{WindowSet Choice}################## GroupCheck ##################function GroupCheck{# Check if $USER belong to cron groupgroup="`grep cron /etc/group |grep $USER`"group_name="`grep cron /etc/group | cut -d':' -f1`"# if notif [[ -z $group ]]thenMESSAGE="Vous devez appartenir au groupe $group_name pour \n pouvoir utiliser $SOFT_NAME \n vérifiez que le paquet cron et bien installé \n puis ajoutez l'utilisateur au groupe $group_name avec la commande \nsudo gpasswd -a $USER $group_name \n ou \n sudo usermod -a $USER $group_name"# Display a warning message with the command to type to fix itWindowSet Warning &# And exitexit 1fi}######################## ExistOrCreateDir ########################function ExistOrCreateDir{#Check if $BASE_DIR exist.if [ -e "$BASE_DIR" ]then # If TRUE check if $BASE_DIR is a regular File if [ -f "$BASE_DIR" ] then # If $BASE_DIR is a regular file ExistOrCreateDir will move $BASE_DIR file to $BASE_DIR_back MESSAGE="Déplacement du fichier $BASE_DIR .\n Le fichier sera renommé en ${BASE_DIR}_back" WindowSet Warning mv $BASE_DIR ${BASE_DIR}_back mkdir -p $BASE_DIR/bin cp BirthdayLauncher $BASE_DIR/bin cp functions_reminder $BASE_DIR/bin cp Reminder $BASE_DIR/bin fielse# if FALSE create dir $BASE_DIRmkdir -p $BASE_DIR/bincp BirthdayLauncher $BASE_DIR/bincp functions_reminder $BASE_DIR/bincp Reminder $BASE_DIR/binfi#move to $BASE_DIRcd $BASE_DIR}############################# ExistOrCreateBirthDir ## ###########################function ExistOrCreateBirthDir{Birth_DIR="$birth"#Check if $Birth_DIR exist.if [ -e "$Birth_DIR" ]then # If TRUE check if $Birth_DIR is a regular File if [ -f "$Birth_DIR" ] then # If $Birth_DIR is a regular file ExistOrCreateDir will move $Birth_DIR file to $Birth_DIR_back MESSAGE="Déplacement du fichier $Birth_DIR .\n Le fichier sera renommé en ${Birth_DIR}_back" WindowSet Warning mv $Birth_DIR ${Birth_DIR}_back mkdir $Birth_DIR fielse# if FALSE create dir $Birth_DIRmkdir $Birth_DIRfi#move to $Birth_DIRcd $Birth_DIR}################ GetMonth ################function GetMonth{case $mois in 1) month="janvier" ;; 2) month="février" ;; 3) month="mars" ;; 4) month="avril" ;; 5) month="mai" ;; 6) month="juin" ;; 7) month="juillet" ;; 8) month="aout" ;; 9) month="septembre" ;; 10) month="octobre" ;; 11) month="novembre" ;; 12) month="décembre" ;; *) MESSAGE="format de date incorrect" ;;esac}################## BirthCheck ##################function BirthCheck{jour="`echo $Birth_DIR | sed -e "s/\(^[0-9][0-9]\)/\1-/" | cut -d'-' -f1 | sed -e"s/\(^0\)/\1%/" |cut -d'%' -f2`"mois="`echo $Birth_DIR | sed -e "s/\(^[0-9][0-9]\)/\1-/" | cut -d'-' -f2 | sed -e"s/\(^0\)/\1%/" |cut -d'%' -f2`"GetMonth moiswhile [[ -e "$name" ]] do MESSAGE="Une date d'anniversaire est déjà définie pour cette personne ce meme jour \n\n Nom : $name \n Date de naissance : $jour $month" WindowSet Error WindowSet Name while [[ -z $name ]] do MESSAGE="Merci de renseigner le champ Nom" WindowSet Error WindowSet Name done done}#################### CrontabCheck ####################function CrontabCheck{# Set cron's valuecrons="30 10 $jour $mois * ${BASE_DIR}/bin/Reminder $birth"# Check if birthday.crons existif [[ ! -f ${BASE_DIR}/birthday.crons ]]then# if not then create ittouch ${BASE_DIR}/birthday.cronsfi# Set $EXIST_BIRTHEXIST_BIRTH="`grep $birth ${BASE_DIR}/birthday.crons`"# Dans tous les cas on ajoute une entrée unique pour $name dans ${BASE_DIR}/$monthMESSAGE="Creation de la nouvelle entrée pour \n Nom : $name \n Date de naissance : $jour $month"# On informe l'utilisateur de l'ajout de la nouvelle entréeWindowSet Info# On écrit l'entrée unique pour $name dans ${BASE_DIR}/$monthecho "##La nouvelle entrée pour $name a étée ajoutée avec succès ##Tache cron associé a l'anniversaire présente dans ## ~/.birthday.d/birthday.crons## $crons## Résumé de l'entréename=\"$name\"Birth Date=\"$jour $month\"" > $name# On vérifie ensuite que la tâche $crons n'est pas déjà présente dans ${BASE_DIR}/birthday.cronsif [[ -z $EXIST_BIRTH ]]# Si elle n'est pas présentethen# Alors on l'ajoute à ${BASE_DIR}/birthday.cronsecho "$crons" >> ${BASE_DIR}/birthday.cronsfi # On récupère ensuite les tâches crons planifées de façon local et on les stocke dans /tmp/$$ crontab -l > /tmp/$$ # If file exist if [[ -e /tmp/$$ ]] then EXIST_LOCAL="`grep $birth /tmp/$$`" # On vérifie que la tâche crons n'est pas non plus planifiée de façon locale if [[ -z $EXIST_LOCAL ]] then # Alors on ajoute la tâche crons au reste des tâches existantes echo "$crons" >> /tmp/$$ # Mise a jour des tâches crons crontab /tmp/$$ #suppression du fichier temporaire rm /tmp/$$ fi else # if no job is localy set then just add crons from birthday.crons crontab ${BASE_DIR}/birthday.crons fi # On récupère ensuite les tâches crons planifées de façon local et on les stocke dans /tmp/$$ crontab -l > /tmp/$$ # Si le fichier existe if [[ -e /tmp/$$ ]] then EXIST_LOCAL="`grep $birth /tmp/$$`" # On vérifie que la tâche crons n'est pas non plus planifiée de façon locale if [[ -z $EXIST_LOCAL ]] then # Alors on ajoute la tâche crons au reste des tâches existantes echo "$crons" >> /tmp/$$ # Mise a jour des tâches crons crontab /tmp/$$ #suppression du fichier temporaire rm /tmp/$$ fi else crontab ${BASE_DIR}/birthday.crons fi}function SaveBirth{source functions_reminderi=0mkdir $BASE_DIR/temp_savecd $BASE_DIRfor save_dir in `ls |grep ^[0-9][0-9][0-9][0-9]` do if [[ -d $save_dir ]] then savedir[$i]=$save_dir if [[ ! -e temp_save/$save_dir ]] then mkdir temp_save/$save_dir fi jour="`echo $save_dir | sed -e "s/\(^[0-9][0-9]\)/\1-/" | cut -d'-' -f1 | sed -e"s/\(^0\)/\1%/" |cut -d'%' -f2`" mois="`echo $save_dir | sed -e "s/\(^[0-9][0-9]\)/\1-/" | cut -d'-' -f2 | sed -e"s/\(^0\)/\1%/" |cut -d'%' -f2`" GetMonth mois echo "traitement des personnes nées le $jour $month" k=0 for files in `ls $save_dir` do filez[$k]=$files echo "sauvegarde de $save_dir/$files[$k] en cours" cp -p $save_dir/$files temp_save/$save_dir/$files echo $files[$*] ((k++)) done ((i++)) fidonesave_date="`date +%m-%d-%Y`"cd temp_savetar cf $save_date"-"$SOFT_NAME.tar * ../birthday.cronsmv $save_date"-"$SOFT_NAME.tar ../cd ../rm -fr temp_save}SaveBirth