Преглед на файлове

inst, installation script created

Jovian (Darkside) преди 6 години
родител
ревизия
82758ca64c
променени са 1 файла, в които са добавени 100 реда и са изтрити 0 реда
  1. 100 0
      inst

+ 100 - 0
inst

@@ -0,0 +1,100 @@
+#!/bin/bash
+#stat --format '%G' inst 
+# Functions
+function inst_out
+{
+	echo "[inst] $1"
+}
+
+function inst_fatal
+{
+	inst_out "FATAL : $1"
+}
+
+# Welcome
+inst_out 'Script begins'
+
+# Check argument
+if [ $# -lt 1 ]
+then
+	inst_fatal 'No script provided'
+	inst_out 'Usage : inst <script> [user] [target]'
+	exit 1
+fi
+
+# Initialize variables
+script=$1
+user=$USER
+target='/usr/local/bin/'
+
+if [ $# -gt 1 ] && [ ! -z $2 ]
+then
+	user=$2
+fi
+
+if [ $# -gt 2 ] && [ ! -z $3 ]
+then
+	target=$3
+fi
+
+# Check file existance
+
+# Prompt before installation
+inst_out 'Ready for installation'
+inst_out "script : $script"
+inst_out "user : $user"
+inst_out "target : $target"
+read -p 'Confirm installation ? [Y/n] > ' choice
+
+if [ ! -z $choice ] && [ "$choice" != 'Y' ]
+then
+	inst_out 'Cancel installation'
+	exit 0
+fi
+
+# Start installing
+inst_out 'Installation begins'
+
+# Check if script executable
+if [ -x $script ]
+then
+	inst_out 'Script already executable'
+else
+	if [ $USER != 'root' ]
+	then
+		inst_fatal "$script is not executable"
+		inst_out 'to fix it :'
+		inst_out "chmod u+x $script"
+		inst_out 'or'
+		inst_out "sudo $0 $script $user $target"
+		exit 1
+	else
+		chmod u+x $script
+	fi
+fi
+
+# Copy script
+cp "$script" "$target$script"
+
+if [ $? -ne 0 ]
+then
+	inst_fatal 'Something went wrong during copy'
+	inst_out "command : cp $script $target$script"
+	exit $?
+fi
+
+# Chown
+if [ $USER = 'root' ]
+then
+	chown "$user:$user" "$target$script"
+	if [ $? -ne 0 ]
+	then
+		inst_fatal 'Something went wrong during chown'
+		inst_out "chown $user:$user $target$script"
+		exit $?
+	fi
+fi
+
+# End
+inst_out 'Script finished'
+exit $?