| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | #!/bin/bash# Functionsfunction inst_out{	echo "[inst] $1"}function inst_fatal{	inst_out "FATAL : $1"}function inst_fail{	inst_fatal 'installation has failed'}# Welcomeinst_out 'Script begins'# Check argumentif [ $# -lt 1 ]then	inst_fatal 'No script provided'	inst_out 'Usage : inst <script> [user] [prompting:enable|disable] [target]'	exit 1fi# Initialize variablesscript=$1user=$USERprompting='enable'target='/usr/local/bin/'if [ $# -gt 1 ] && [ ! -z $2 ]then	user=$2fiif [ $# -gt 2 ] && [ ! -z $3 ]then	prompting=$3fiif [ $# -gt 3 ] && [ ! -z $4 ]then	target=$4fi# Check file existenceif [ ! -e $script ]then	inst_fatal "$script does not exist"	exit 1fi# Prompt before installationinst_out 'Ready for installation'inst_out "script : $script"inst_out "user : $user"inst_out "target : $target"if [ $prompting = 'enable' ]then	read -p 'Confirm installation ? [Y/n] > ' choiceelse	choice='Y'fiif [ ! -z $choice ] && [ "$choice" != 'Y' ]then	inst_out 'Cancel installation'	exit 0fi# Start installinginst_out 'Installation begins'# Check if script executableif [ -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	fifi# Copy scriptcp "$script" "$target$script"if [ $? -ne 0 ]then	inst_fatal 'Something went wrong during copy'	inst_out "command : cp $script $target$script"	exit $?fi# Chownif [ $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 $?	fifi# Check if all rightoutput="$target$script"owner_user=`stat --format '%U' $output`owner_group=`stat --format '%G' $output`if [ ! -e $output ]then	inst_fatal "$output does not exist"	inst_fail	exit 1elif [ $owner_user != $user ]then	inst_fatal "$output owner user is not $user but $owner_user"	inst_fail	exit 1elif [ $owner_group != $user ]then	inst_fatal "$output group is not $user but $owner_group"	inst_fail	exit 1elif [ ! -x $output ]then	inst_fatal "$output is not executable"	inst_out "fix : sudo chmod u+x $output"	inst_fail	exit 1fi# Endinst_out 'Script finished'exit $?
 |