commit b2e7591aa2dbc90ceb36f607218eb2d48dc0bc45
Author: Jorge L. Florit <jlflorit@gmail.com>
Date:   Tue Feb 25 17:15:36 2014 +0000

    Change nightlycompile script to approach better to check and compile each target for each community

diff --git a/Makefile b/Makefile
index bc8ebaa..3406874 100644
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,7 @@ OWRT_SVN = svn://svn.openwrt.org/openwrt/branches/attitude_adjustment
 OWRT_PKG_SVN =  svn://svn.openwrt.org/openwrt/branches/packages_12.09
 QMP_GIT_RW = ssh://gitosis@qmp.cat:221/qmp.git
 QMP_GIT_RO = git://qmp.cat/qmp.git
-QMP_GIT_BRANCH ?= master
+QMP_GIT_BRANCH ?= testing
 BUILD_DIR = build
 CONFIG_DIR = configs
 MY_CONFIGS = $(BUILD_DIR)/configs
@@ -265,6 +265,10 @@ list_targets:
 	$(info $(HW_AVAILABLE))
 	@exit 0
 
+target_name:
+	$(info $(NAME))
+	@exit 0
+
 config:
 	@select HW in $(HW_AVAILABLE); do break; done; echo $$HW > .config.tmp;
 	mv .config.tmp .config
diff --git a/nightlycompile.sh b/nightlycompile.sh
index ed687cf..f013f84 100755
--- a/nightlycompile.sh
+++ b/nightlycompile.sh
@@ -1,6 +1,7 @@
 #!/bin/bash
 
-# This is a dummy script to be used with crontab for nighly compilations and so
+# This is a dummy script to be used with crontab for nighly compilations and so.
+# Not recommended to use in a read/write development environment.
 #
 # Example of usage:
 # FORCE="1" TARGETS="alix rspro" COMMUNITY="myNet" BRANCH="testing" ./nightlycompile.sh
@@ -24,33 +25,42 @@ NOTARGETS=${NOTARGETS:-}
 # Extra packages (separated by spaces)
 EXTRA_PACKS=${EXTRA_PACKS:-}
 
-
-# If FORCE is 1, compilation process will be forced
+# If $FORCE is not set, then not forced by default
 [ -z "$FORCE" ] && FORCE=0
 
-[ $FORCE -eq 0 ] && {
-	if make is_up_to_date QMP_GIT_BRANCH=$BRANCH >& /dev/null
-	   then
-	   echo "Nothing to compile, qMp in last version"
-	   exit 0
-	fi
-}
+# If no "IMAGES" file is supossed there are no binary images compiled, then force to compile
+[ ! -f "images/IMAGES" ] && FORCE=1
+
+# Check if it is up to date
+make is_up_to_date QMP_GIT_BRANCH=$BRANCH >& /dev/null
+[ $? -ne 0 ] && make update_all
+
+# Date of the last commit
+LAST_COMMIT_DATE=$(cd build/qmp && git log -1 origin/${BRANCH} --format="%ct")
 
-make update_all
 
 (cd build/qmp && git checkout $BRANCH)
 
 for t in $TARGETS; do
-	[[ "$NOTARGETS" =~ "$t" ]] && continue
+	[[ "$NOTARGETS" =~ "$t" ]] && echo "Ignoring target $t. Not compiling." && continue
+	# If not forced, check if there is already compiled an image for the target with the last commit
+	[ $FORCE -ne 1 ] && [ -f "images/IMAGES" ] && {
+		TARGET_NAME=$(make target_name T=$t)
+		[ -z "$TARGET_NAME" ] && echo "Target $t unavailable. Not compiling." && continue
+		IMAGE_DATE=$(cat images/IMAGES | egrep "${TARGET_NAME}.*${COMMUNITY}_" | awk '{print $2}' | awk -F\- '{print $NF}' | sort -n -r | awk NR==1 | cut -d "." -f1 | sed s/_/\ /)
+		[ ! -z "$IMAGE_DATE" ] && {
+			IMAGE_DATE=$(date -d "${IMAGE_DATE}" +%s)
+			[ $IMAGE_DATE -ge $LAST_COMMIT_DATE ] && echo "qMp for target $t in last version." && continue
+		}
+	}
+
 	echo "Compiling target $t"
 	nice -n 25 make T=$t build J=2 QMP_GIT_BRANCH=$BRANCH COMMUNITY=$COMMUNITY EXTRA_PACKS=$EXTRA_PACKS
-
-	[ $? -ne 0 ] && [ ! -z "$MAIL" ] && 
-	  echo "Error detected during QMP compilation process" | mail -s "[qMp] build system" $MAIL
+	[ $? -ne 0 ] && [ ! -z "$MAIL" ] && echo "Error detected during QMP compilation process (for target $t)" | mail -s "[qMp] build system" $MAIL && ./scripts/extra-packages.script POST_BUILD "ar71xx" $t $EXTRA_PACKS
 done
 
-[ $DAYS_TO_PRESERVE -gt 0 ] && 
-  find images/ -iname "*.bin" -type f -mtime +$DAYS_TO_PRESERVE -exec rm -f '{}' \;
-  
+exit 0
+[ $DAYS_TO_PRESERVE -gt 0 ] && find images/ -iname "*.bin" -type f -mtime +$DAYS_TO_PRESERVE -exec rm -f '{}' \;
+
 (cd images && md5sum *.bin > IMAGES)
 
