Index: logrotate-3.7.7/test/test-config.41.in
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ logrotate-3.7.7/test/test-config.41.in	2014-02-27 11:38:54.661546350 +0100
@@ -0,0 +1,17 @@
+create
+
+&DIR&/test*.log {
+    size 5
+    rotate 1
+    nosharedscripts
+    prerotate
+		touch scriptout
+		echo -n $(basename $1) >> scriptout
+		echo -n ";" >> scriptout
+    endscript
+    postrotate
+		touch scriptout
+		echo -n $(basename $1) >>  scriptout
+		echo -n ";" >> scriptout
+    endscript
+}
Index: logrotate-3.7.7/test/test-config.43.in
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ logrotate-3.7.7/test/test-config.43.in	2014-02-27 11:49:31.275495941 +0100
@@ -0,0 +1,17 @@
+create
+
+&DIR&/test*.log {
+    size 5
+    rotate 1
+    nosharedscripts
+    prerotate
+		touch scriptout
+		echo -n $(basename $1) >> scriptout
+		echo -n ";" >> scriptout
+    endscript
+    postrotate
+		touch scriptout
+		echo -n $(basename $1) >>  scriptout
+		echo -n ";" >> scriptout
+    endscript
+}
Index: logrotate-3.7.7/test/test-config.45.in
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ logrotate-3.7.7/test/test-config.45.in	2014-02-27 11:38:54.662546361 +0100
@@ -0,0 +1,27 @@
+create
+
+&DIR&/test.log &DIR&/test2.log {
+    size 5
+    rotate 1
+    sharedscripts
+    prerotate
+		touch scriptout
+		IFS=$(echo -en "\n\b")
+		echo -n "\"" >> scriptout
+		for f in "$1"
+		do
+			echo -n $(basename $f) >>  scriptout
+			echo -n ";" >> scriptout
+		done
+    endscript
+    postrotate
+		touch scriptout
+		IFS=$(echo -en "\n\b")
+		for f in "$1"
+		do
+			echo -n $(basename $f) >>  scriptout
+			echo -n ";" >> scriptout
+		done
+		echo -n "\"" >> scriptout
+    endscript
+}
Index: logrotate-3.7.7/test/test
===================================================================
--- logrotate-3.7.7.orig/test/test	2014-02-27 11:38:54.608545779 +0100
+++ logrotate-3.7.7/test/test	2014-02-27 11:50:30.403142558 +0100
@@ -392,4 +392,128 @@ test.log 0
 test.1.log 0 zero
 EOF
 
+cleanup 41
+
+# ------------------------------- Test 41 ------------------------------------
+# Test that prerotate and postrotate scripts are called only when the log files
+# are actually rotated
+preptest test.log 41 1
+echo x > test2.log
+
+$RLR test-config.41
+
+checkoutput <<EOF
+test.log 0
+test.log.1 0 zero
+test2.log 0 x
+scriptout 0 test.log;test.log;
+EOF
+
+cleanup 42
+
+# ------------------------------- Test 42 ------------------------------------
+# Test that script is called only once when sharedscripts is defined
+preptest test.log 42 1
+echo number2 > test2.log
+
+$RLR test-config.42
+
+checkoutput <<EOF
+test.log 0
+test.log.1 0 zero
+test2.log 0
+test2.log.1 0 number2
+scriptout 0 "test*.log ;test*.log ;"
+EOF
+
+cleanup 43
+
+# ------------------------------- Test 43 ------------------------------------
+# Test that prerotate and postrotate scripts are called twice for two files
+# when sharedscripts defined
+preptest test.log 43 1
+echo number2 > test2.log
+
+$RLR test-config.43
+
+# Check both possible orders
+grep "test2.log;test2.log;test.log;test.log;" scriptout >/dev/null
+if [ $? != 0 ]; then
+	grep "test.log;test.log;test2.log;test2.log;" scriptout >/dev/null
+	if [ $? != 0 ]; then
+		echo "ERROR: scriptout should contain 'test2.log;test2.log;test.log;test.log;' or 'test.log;test.log;test2.log;test2.log;'"
+		exit 3
+	fi
+fi
+
+rm -f scriptout
+
+checkoutput <<EOF
+test.log 0
+test.log.1 0 zero
+test2.log 0
+test2.log.1 0 number2
+EOF
+
+cleanup 44
+
+# ------------------------------- Test 44 ------------------------------------
+# Test that prerotate and postrotate scripts are called once when nosharedscripts
+# is defined and one rotation fails
+preptest test.log 44 1
+
+$RLR test-config.44 2>error.log
+
+grep "error: stat of" error.log >/dev/null
+if [ $? != 0 ]; then
+	echo "No error printed, but there should be one."
+	exit 3
+fi
+
+rm -f error.log
+
+checkoutput <<EOF
+test.log 0
+test.log.1 0 zero
+scriptout 0 "test.log;test.log;"
+EOF
+
+cleanup 45
+
+# ------------------------------- Test 44 ------------------------------------
+# Test that prerotate and postrotate scripts are not called when sharedscripts
+# is defined and one rotation fails
+preptest test.log 45 1
+
+touch scriptout
+$RLR test-config.45 2>error.log
+
+grep "error: stat of" error.log >/dev/null
+if [ $? != 0 ]; then
+	echo "No error printed, but there should be one."
+	exit 3
+fi
+
+rm -f error.log
+
+checkoutput <<EOF
+test.log 0 zero
+scriptout 0
+EOF
+
+
+cleanup 52
+
+# ------------------------------- Test 52 ------------------------------------
+# sharedscripts are not run if the first log file does not exist
+preptest test.log 52 1 0
+
+$RLR test-config.52
+
+checkoutput <<EOF
+test.log 0
+test.log.1 0 zero
+scriptout 0 foo
+EOF
+
 cleanup
Index: logrotate-3.7.7/test/test-config.42.in
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ logrotate-3.7.7/test/test-config.42.in	2014-02-27 11:38:54.662546361 +0100
@@ -0,0 +1,27 @@
+create
+
+&DIR&/test*.log {
+    size 5
+    rotate 1
+    sharedscripts
+    prerotate
+		touch scriptout
+		IFS=$(echo -en "\n\b")
+		echo -n "\"" >> scriptout
+		for f in "$1"
+		do
+			echo -n $(basename $f) >>  scriptout
+			echo -n ";" >> scriptout
+		done
+    endscript
+    postrotate
+		touch scriptout
+		IFS=$(echo -en "\n\b")
+		for f in "$1"
+		do
+			echo -n $(basename $f) >>  scriptout
+			echo -n ";" >> scriptout
+		done
+		echo -n "\"" >> scriptout
+    endscript
+}
Index: logrotate-3.7.7/test/test-config.44.in
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ logrotate-3.7.7/test/test-config.44.in	2014-02-27 11:38:54.662546361 +0100
@@ -0,0 +1,27 @@
+create
+
+&DIR&/test.log &DIR&/test2.log {
+    size 5
+    rotate 1
+    nosharedscripts
+    prerotate
+		touch scriptout
+		IFS=$(echo -en "\n\b")
+		echo -n "\"" >> scriptout
+		for f in "$1"
+		do
+			echo -n $(basename $f) >>  scriptout
+			echo -n ";" >> scriptout
+		done
+    endscript
+    postrotate
+		touch scriptout
+		IFS=$(echo -en "\n\b")
+		for f in "$1"
+		do
+			echo -n $(basename $f) >>  scriptout
+			echo -n ";" >> scriptout
+		done
+		echo -n "\"" >> scriptout
+    endscript
+}
Index: logrotate-3.7.7/logrotate.c
===================================================================
--- logrotate-3.7.7.orig/logrotate.c	2014-02-27 11:38:54.655546285 +0100
+++ logrotate-3.7.7/logrotate.c	2014-02-27 11:39:00.172605728 +0100
@@ -1473,9 +1473,11 @@ int rotateLogSet(struct logInfo *log, in
 	    hasErrors |= logHasErrors[i];
 	}
 
+	/* (nemam chyby nebo shared) a (nemam chyby nebo nonshared) */
 	if (log->pre
-	    && (! ( (logHasErrors[j] && !(log->flags & LOG_FLAG_SHAREDSCRIPTS))
-		   || (hasErrors && (log->flags & LOG_FLAG_SHAREDSCRIPTS)) ) )) {
+		&& (! ( (logHasErrors[j] && !(log->flags & LOG_FLAG_SHAREDSCRIPTS))
+		|| (hasErrors && (log->flags & LOG_FLAG_SHAREDSCRIPTS)) ) )
+		&& ((log->flags & LOG_FLAG_SHAREDSCRIPTS) || state[j]->doRotate)) {
 	    if (!numRotated) {
 		message(MESS_DEBUG, "not running prerotate script, "
 			"since no logs will be rotated\n");
@@ -1509,8 +1511,10 @@ int rotateLogSet(struct logInfo *log, in
 	}
 
 	if (log->post
-	    && (! ( (logHasErrors[j] && !(log->flags & LOG_FLAG_SHAREDSCRIPTS))
-		   || (hasErrors && (log->flags & LOG_FLAG_SHAREDSCRIPTS)) ) )) {
+		&& (! ( (logHasErrors[j] && !(log->flags & LOG_FLAG_SHAREDSCRIPTS))
+		|| (hasErrors && (log->flags & LOG_FLAG_SHAREDSCRIPTS)) ) )
+		&& ((log->flags & LOG_FLAG_SHAREDSCRIPTS) || state[j]->doRotate)) {
+
 	    if (!numRotated) {
 		message(MESS_DEBUG, "not running postrotate script, "
 			"since no logs were rotated\n");
Index: logrotate-3.7.7/test/test-config.52.in
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ logrotate-3.7.7/test/test-config.52.in	2014-02-27 11:38:54.662546361 +0100
@@ -0,0 +1,13 @@
+create
+
+/var/log/does_not_exist.log &DIR&/test.log {
+	rotate 14
+	size 2
+	missingok
+	sharedscripts
+	postrotate
+		touch scriptout
+		echo $(cat scriptout) foo > foo
+		mv foo scriptout
+	endscript
+}
\ No newline at end of file
