scripts

My personal collection of scripts
git clone git://src.gearsix.net/scripts
Log | Files | Refs | Atom | README | LICENSE

install.sh (4924B)


      1 #!/usr/bin/env bash
      2 
      3 #===========
      4 # g6scripts
      5 #===========
      6 # file: install
      7 # created: 2020-01-15
      8 # updated: 2020-01-16
      9 # description: loops through files in ./src & installs them to configured destination
     10 
     11 #---------
     12 # GLOBALS
     13 #---------
     14 ln=0
     15 dir=0
     16 all=0
     17 force=0
     18 verbose=0
     19 default_destination=$HOME/.local/bin
     20 if [[ $USER == "root" ]]; then default_destination=/usr/local/bin/; fi
     21 source=./src/posix/*.*
     22 depreciated_src=./src/posix/depreciated/*.*
     23 scripts=()
     24 depreciated=0
     25 
     26 #-----------
     27 # FUNCTIONS
     28 #-----------
     29 ## print_help = prints the help dialog and exits
     30 print_help () {
     31 	echo "usage: ./install [ARGUMENTS] [SCRIPT]"
     32 	echo ""
     33 	echo "SCRIPTS"
     34 	echo "  Optionally, you can specify a script to install."
     35 	echo "  Any specified scripts must be the filepath to the"
     36 	echo "  script (not just the filename)."
     37 	echo ""
     38 	echo "ARGUMENTS"
     39 	echo "  -f, --force         overwrites files when installing"
     40 	echo "  -r,   --reinstall   re-install scripts already existing in"
     41 	echo "                      destination directory"
     42 	echo "  -d, --dest [DEST]   set the install destination"
     43 	echo "  -a, --all           install ALL the scripts ('y' to all prompts)"
     44 	echo "  -l, --link          'ln' the script (not cp)."
     45 	echo "  -h, --help          print this dialog"
     46 }
     47 install_script () {
     48 	src=$1
     49 	dest=$2
     50 
     51 	cmd=
     52 	if [[ $ln -eq 0 ]]; then
     53 		cmd=$(install -vp $src $dest)
     54 	else # ln the file
     55 		cmd=$(chmod +x $src && echo ""; ln -vsf $(readlink -f $src) $dest)
     56 	fi
     57 
     58 	if [[ ! $cmd ]]; then
     59 		echo "something went wrong"; exit 1;
     60 	else
     61 		echo $cmd
     62 	fi
     63 }
     64 ## install_yn = installs $1 to $2, prints $3 if user asks for "info"
     65 install_yn () {
     66 	src=$1  # path of the file to install
     67 	dest=$2 # path to install the file to
     68 	depr=$3 # if installing a depreciated script or not
     69 
     70 	ask=0
     71 	while [[ $ask -eq 0 ]]; do
     72 		if [[ $force -eq 0 ]]; then
     73 			printf "install %s [yes/no/info]? " $src
     74 			if [[ $all -eq 0 || $depr -eq 1 ]]; then
     75 				if [[ -e $dest && $reinstall -eq 1 ]]; then
     76 					yn="y"
     77 					printf "\treinstalling...\n"
     78 				else
     79 					read yn
     80 				fi
     81 			else
     82 				yn="y"
     83 				echo "y"
     84 			fi
     85 
     86 			if [[ $yn == "y" || $yn == "Y" || $yn == "yes" ]]; then
     87 				install_script $src $dest
     88 				ask=1
     89 				if [[ $reinstall ]]; then 
     90 					chmod +x "$src"
     91 				fi
     92 			elif [[ $yn == "n" || $yn == "N" || $yn == "no" ]]; then
     93 				printf "\tskipping...\n"
     94 				ask=1
     95 			elif [[ $yn == "i" || $yn == "I" || $yn == "info" ]]; then
     96 				echo ""
     97 				sh $(readlink -f $src) -h | sed 's/^/	/'
     98 				echo ""
     99 				#grep -i "description\|dependencies\|arguments\|depreciated" $script
    100 			fi
    101 		else 
    102 			install_script $src $dest
    103 			ask=1
    104 		fi
    105 	done
    106 }
    107 
    108 #------
    109 # MAIN
    110 #------
    111 ## parse args
    112 while [[ $# -gt 0 ]]; do
    113 	case $1 in
    114 		-f|--force)
    115 			force=1
    116 			echo "forcing overwrite on existing files"
    117 			shift
    118 			;;
    119 		-l|--link)
    120 			ln=1
    121 			echo "will only create symolic links to scripts, this will make the scripts in ./src/ executable"
    122 			shift
    123 			;;
    124 		-d|--dest)
    125 			dir=1
    126 			destination=$2
    127 			echo "install destination = $destination"
    128 			shift
    129 			shift
    130 			;;
    131 		-a|--all)
    132 			all=1
    133 			echo "installed ALL scripts (not including depreciated)"
    134 			shift
    135 			;;
    136 		-r|--reinstall)
    137 			reinstall=1
    138 			echo "reinstalling currently existing files"
    139 			shift
    140 			;;
    141 		-h|--help)
    142 			print_help
    143 			exit
    144 			;;
    145 		*) # assume it's a specifed script to install
    146 			scripts+=($1)
    147 			depreciated=1
    148 			shift
    149 			;;
    150 	esac
    151 done
    152 
    153 ## check install destination
    154 if [[ $dir -eq 0 ]]; then
    155 	printf "install destination (default = $default_destination): "
    156 	read destination
    157 	## set to default if empty input
    158 	if [ -z $destination ]; then
    159 		destination=$default_destination
    160 	else
    161 		destination=${destination/"~"/"/home/$USER"}
    162 	fi
    163 	## check exists
    164 	if [ ! -e $destination ]; then mkdir -p "$destination"; fi
    165 	## check if need root permission to r/w
    166 	if [ ! -w $destination ]; then echo "$destination: Permission denied"; exit 1; fi
    167 fi
    168 mkdir -p $destination
    169 
    170 ## install all $scripts
    171 if [ ${#scripts[@]} -eq 0 ]; then scripts=$source; fi
    172 for script in $scripts; do
    173 	if [[ -e ${destination//"~"/$HOME}/$(basename $script .sh) && $force -eq 0 && $reinstall -eq 0 ]]; then
    174 		printf "${destination//"~"/$HOME}/$(basename $script .sh) already exists, skipping...\n"
    175 	elif [[ $script != "src/depreciated" ]]; then # skip depreciated
    176 		install_yn $script ${destination//"~"/$HOME}/$(basename $script .sh)
    177 	fi
    178 done
    179 
    180 ## install depreciated scripts
    181 while [[ $depreciated -eq 0 ]]; do
    182 	printf "install depreciated scripts (default = no) [yes/no/info]? "
    183 	read yn
    184 	if [[ $yn == "y" || $yn == "Y" || $yn == "yes" ]]; then
    185 		for script in $depreciated_src; do
    186 			install_yn $script ${destination//"~"/$HOME}/$(basename $script .sh) 1
    187 		done
    188 		depreciated=1
    189 	elif [[ $yn == "n" || $yn == "N" || $yn == "no" ]]; then
    190 		depreciated=1
    191 	elif [[ $yn == "i" || $yn == "I" || $yn == "info" ]]; then
    192 		echo "depreciated scripts have been found redundant, details are provided in the \"info\" for each script"
    193 	fi
    194 done
    195 
    196 ## end
    197 echo "done"