about summary refs log tree commit diff stats
path: root/dscip
diff options
context:
space:
mode:
Diffstat (limited to 'dscip')
-rwxr-xr-xdscip75
1 files changed, 27 insertions, 48 deletions
diff --git a/dscip b/dscip
index 9621800..8d8b33c 100755
--- a/dscip
+++ b/dscip
@@ -17,45 +17,23 @@
 ################################################################################
 
 set -e
-
-# Variables that control the program. #
-# GIT Repo #
-DSCIP_GITREPO="https://www.example.com/example/example.git"
-# GIT MODE:                                                   #
-# pull: Doesn't delete previous clone and just pulls changes. #
-# clone: Deletes previous clone, and creates a fresh clone.   #
-DSCIP_GITMODE="clone"
-# Branch to check #
-DSCIP_BRANCH="master"
-WORKING_DIRECTORY="."
-# Commands to run before building. #
-DSCIP_PRE_CMD="$WORKING_DIRECTORY/pre.sh"
-# Commands to run to build program. #
-DSCIP_BUILD_CMD="$WORKING_DIRECTORY/build.sh"
-# Commands to run after building is done. #
-DSCIP_POST_CMD="$WORKING_DIRECTORY/post.sh"
-# Daemon mode options #
-DSCIP_DAEMON="false" # If daemon mode should be enabled or not. #
-DSCIP_DAEMON_FORK="true" # If the daemon should run in the background. #
-DSCIP_SLEEP="60" # How many seconds before the daemon re-runs itself. #
-# etc #
-DSCIP_DISREGARD_COMMIT_CHECK="false" # If the script should just rebuild even #
-# if upstream has not updated. #
+. "$(dirname "$0")/config.sh"
 
 ################################################################################
 
 # Check if script is currently running, and if not, reset LOCK #
-if [ -f $WORKING_DIRECTORY/LOCK ]; then
-	LOCKED_PID=$(cat $WORKING_DIRECTORY/LOCK)
-	if ps -U $USER -p "$LOCKED_PID"; then
-		echo "Script still running. Exiting..."
+if [ -f "$WORKING_DIRECTORY/LOCK" ]; then
+	LOCKED_PID=$(cat "$WORKING_DIRECTORY/LOCK")
+	if ps -p "$LOCKED_PID"; then
+		echo "Script still running. Exiting..." 2>> "$DSCIP_OUTPUT_TO"
 		exit 0
 	else
-		rm $WORKING_DIRECTORY/LOCK
+		echo "Script not running, resetting lock..." 2>> "$DSCIP_OUTPUT_TO"
+		rm "$WORKING_DIRECTORY/LOCK"
 	fi
 fi
 
-echo "$$" > $WORKING_DIRECTORY/LOCK
+echo "$$" > "$WORKING_DIRECTORY/LOCK"
 
 if [ "$DSCIP_DAEMON" = "true" ]; then
 	if [ "$DSCIP_DAEMON_FORK" = "true" ] && [ ! "$FORKED" = "true" ]; then
@@ -67,38 +45,38 @@ fi
 build () {
 	if [ "$DSCIP_GITMODE" = "clone" ]; then
 		rm -rf "$WORKING_DIRECTORY/wrkdir" # Clean Up #
-		git clone -b $DSCIP_BRANCH $DSCIP_GITREPO wrkdir # Clone git #
+		git clone -b "$DSCIP_BRANCH" "$DSCIP_GITREPO" wrkdir 2>> "$DSCIP_OUTPUT_TO" # Clone git #
 	elif [ "$DSCIP_GITMODE" = "pull" ]; then
 		if [ ! -d "$WORKING_DIRECTORY/wrkdir" ]; then
-			git clone -b $DSCIP_BRANCH $DSCIP_GITREPO wrkdir
+			git clone -b "$DSCIP_BRANCH" "$DSCIP_GITREPO" wrkdir 2>> "$DSCIP_OUTPUT_TO"
 		fi
 		cd wrkdir
-		git pull
+		git pull 2>> "$DSCIP_OUTPUT_TO"
 	else
-		echo "Invalid GITMODE, choose either 'clone' or 'pull'"
+		echo "Invalid GITMODE, choose either 'clone' or 'pull'" 2>> "$DSCIP_OUTPUT_TO"
 		exit 1
 	fi
-	cd $WORKING_DIRECTORY/wrkdir
-	$DSCIP_PRE_CMD
-	cd $WORKING_DIRECTORY/wrkdir
-	$DSCIP_BUILD_CMD
-	cd $WORKING_DIRECTORY/wrkdir
-	$DSCIP_POST_CMD
-	mkdir -p $WORKING_DIRECTORY/meta
-	echo "$CURRENT_COMMIT" > $WORKING_DIRECTORY/meta/LAST_COMMIT # Save commit #
+	cd "$WORKING_DIRECTORY/wrkdir"
+	$DSCIP_PRE_CMD 2>> "$DSCIP_OUTPUT_TO"
+	cd "$WORKING_DIRECTORY/wrkdir"
+	$DSCIP_BUILD_CMD 2>> "$DSCIP_OUTPUT_TO"
+	cd "$WORKING_DIRECTORY/wrkdir"
+	$DSCIP_POST_CMD 2>> "$DSCIP_OUTPUT_TO"
+	mkdir -p "$WORKING_DIRECTORY/meta"
+	echo "$CURRENT_COMMIT" > "$WORKING_DIRECTORY/meta/LAST_COMMIT" # Save commit #
 }
 
 run () {
-	mkdir -p $WORKING_DIRECTORY
-	cd $WORKING_DIRECTORY
+	mkdir -p "$WORKING_DIRECTORY"
+	cd "$WORKING_DIRECTORY"
 
 	# Loads last commit hash if it exists. # 
 	if [ -f "$WORKING_DIRECTORY/meta/LAST_COMMIT" ]; then
-		LAST_COMMIT="$(cat $WORKING_DIRECTORY/meta/LAST_COMMIT)"
+		LAST_COMMIT="$(cat "$WORKING_DIRECTORY/meta/LAST_COMMIT")"
 	fi
 
 	# Loads current commit hash #
-	CURRENT_COMMIT=$(git ls-remote $DSCIP_GITREPO | awk "/refs\/(heads|tags)\/$DSCIP_BRANCH/{print \$1}")
+	CURRENT_COMMIT=$(git ls-remote "$DSCIP_GITREPO" | awk "/refs\/(heads|tags)\/$DSCIP_BRANCH/{print \$1}")
 
 	# If LAST_COMMIT doesn't exist, that means it's a first run and we can go ahead and build. #
 	# Or skip the commit check if DSCIP_DISREGARD_COMMIT_CHECK is set to true.
@@ -111,12 +89,13 @@ run () {
 	else
 		build
 	fi
-	rm $WORKING_DIRECTORY/LOCK # Remove lock #
+	rm "$WORKING_DIRECTORY/LOCK" # Remove lock #
+	echo "Done." 2>> "$DSCIP_OUTPUT_TO"
 }
 
 run
 while [ "$DSCIP_DAEMON" = "true" ]; do
-	sleep $DSCIP_SLEEP
+	sleep "$DSCIP_SLEEP"
 	run
 done