-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_diapo.sh
executable file
·109 lines (91 loc) · 3.5 KB
/
start_diapo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
# As user "pi", crontab -e
#@reboot rm /home/pi/diapo/diapo_mutex
#@reboot /home/pi/diapo/start_diapo.sh
#* * * * * /home/pi/diapo/start_diapo.sh
function main() {
source /home/pi/diapo/diapo.conf
if [ -f $TOOL_DIR/diapo_mutex ]
then
exit
fi
echo "[*] $0 demarre" &>$LOG_FILE
touch $TOOL_DIR/diapo_mutex
DIR_FBI=/tmp/symlinkdiapo
DIR_WORK=/tmp/resized_copy
rm -fr $DIR_FBI $DIR_WORK
mkdir $DIR_FBI $DIR_WORK
# Prepare the symbolic links for the N last photos
echo "[*] init - linking photos into directory $DIR_FBI" &>>$LOG_FILE
make_links
NB_FIC=`find $DIR_FBI -type l |wc -l`
# The refresh of photos will be done 10 seconds before the end of the current loop
DUREE_TEMPO=$(($NB_FIC * $DUREE_PHOTO))
if [ $DUREE_TEMPO -lt 1 ]; then
DUREE_TEMPO=$(($NB_FIC - 1))
fi
DELAI_CLEAN=$(($NB_FIC * $DUREE_PHOTO / 60))
if [ $DELAI_CLEAN -lt 1 ]; then
DELAI_CLEAN=5
fi
echo "[*] init - delay for files to be cleaned = $DELAI_CLEAN minutes" &>>$LOG_FILE
# run fbi (in background)
run_fbi
PID_FBI=$(pgrep fbi)
echo "[+] fbi launched (PID $PID_FBI) on the directory $DIR_FBI" &>>$LOG_FILE
# Main loop
while [ ! -f $TOOL_DIR/STOP ]; do
# wait a while before the end of the current fbi loop then make links for new photos (for the next fbi loop)
echo "[*] wait $DUREE_TEMPO sec" &>>$LOG_FILE
sleep $DUREE_TEMPO &>/dev/null
echo "[+] wait $DUREE_TEMPO sec [DONE]" &>>$LOG_FILE
# refresh the photo list (run it in background to keep sync'd with the fbi loop)
# nevermind if it's not finished when fbi starts next loop, because it will start by reading the first links which are already up-to-date
echo "[*] linking photos into directory $DIR_FBI" &>>$LOG_FILE
make_links clean &
watchdog
done
rm $TOOL_DIR/diapo_mutex
}
# arg1 : if set, clean must be done
function make_links() {
clean=$1
symlink_i=1
find $PHOTO_DIR -maxdepth 1 -type f -printf "%C@ %p\n" | egrep -i "*.jpg|*.jpeg|*.bmp|*.png|*.gif" | sort -n | cut -f2- -d" " | tail -$NB_PHOTO_DIAPO | tr '\n' '\0' |
while IFS= read -r -d '' file; do
i=`printf "%03d" $symlink_i`
copy=`basename "$file"`
#echo "link_$i"
if [ ! -f ${DIR_WORK}/"$copy" ]; then
convert "$file" -resize $RESO_PHOTO -background black -compose Copy -gravity center -extent $RESO_PHOTO -auto-orient ${DIR_WORK}/"$copy"
fi
ln -sf ${DIR_WORK}/"$copy" ${DIR_FBI}/"link_$i"
symlink_i=$(($symlink_i + 1))
done
echo "[+] linking photos into directory $DIR_FBI [DONE]" &>>$LOG_FILE
# clean the photos not used in the next loop of fbi display, and being too old (i.e. not being displayed in current loop)
if [ ! -z $clean ]; then
clean_old_photos
fi
}
function clean_old_photos() {
find $DIR_WORK -type f -mmin +$DELAI_CLEAN -print0 | xargs -0 -n 1 basename 2>/dev/null | sort >/tmp/files_work
if [ -s /tmp/files_work ]; then
readlink -z $DIR_FBI/* | xargs -0 -n 1 basename | sort >/tmp/files_fbi
pushd . &>/dev/null
cd $DIR_WORK
comm -2 -3 /tmp/files_work /tmp/files_fbi | xargs -I {} rm -f "{}"
popd &>/dev/null
fi
echo "[+] cleaned directory $DIR_WORK" &>>$LOG_FILE
}
function run_fbi() {
sudo fbi -noverbose -T 1 -a -t $DUREE_PHOTO -cachemem 0 -nocomments $DIR_FBI/* >/dev/null 2>&1
}
function watchdog() {
if ! pgrep fbi &>/dev/null; then
echo "[*] fbi no longer running, restarting" &>>$LOG_FILE
run_fbi
fi
}
main