Naoki Shiota | 9a1e6d1 | 2014-04-03 14:47:12 -0700 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
| 3 | # read-conf {filename} {parameter name} [default value] |
| 4 | function read-conf { |
| 5 | local value=`grep ^${2} ${1} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'` |
| 6 | if [ -z "${value}" ]; then |
| 7 | echo $3 |
| 8 | else |
| 9 | echo ${value} |
| 10 | fi |
| 11 | } |
| 12 | |
| 13 | # revert-file {filename} |
| 14 | # revert "filename" from "filename.bak" if "filename.tmp" exists. |
| 15 | function revert-file { |
| 16 | local filename=$1 |
| 17 | local temp="${filename}.tmp" |
| 18 | local backup="${filename}.bak" |
| 19 | |
| 20 | if [ -f "${temp}" ]; then |
| 21 | echo -n "reverting ${filename} ... " |
| 22 | mv ${backup} ${filename} |
| 23 | rm ${temp} |
| 24 | echo "DONE" |
| 25 | fi |
| 26 | } |
| 27 | |
| 28 | # create-conf-interactive {filename} {function to create conf} [param to function] |
| 29 | function create-conf-interactive { |
| 30 | local filepath=$1 |
| 31 | local filename=`basename ${filepath}` |
| 32 | local func=$2 |
| 33 | |
| 34 | if [ -f ${filepath} ]; then |
| 35 | # confirmation to overwrite existing config file |
| 36 | echo -n "Overwriting ${filename} [Y/n]? " |
| 37 | while [ 1 ]; do |
| 38 | read key |
| 39 | if [ -z "${key}" -o "${key}" == "Y" -o "${key}" == "y" ]; then |
| 40 | ${func} $3 |
| 41 | break |
| 42 | elif [ "${key}" == "N" -o "${key}" == "n" ]; then |
| 43 | break |
| 44 | fi |
| 45 | echo "[Y/n]?" |
| 46 | done |
| 47 | else |
| 48 | ${func} $3 |
| 49 | fi |
| 50 | } |
| 51 | |
| 52 | # begin-conf-creation {config file name} |
| 53 | function begin-conf-creation { |
| 54 | local conf=${1} |
| 55 | local backup="${conf}.bak" |
| 56 | local temp="${conf}.tmp" |
| 57 | |
| 58 | if [ -f ${conf} ]; then |
| 59 | mv ${conf} ${backup} |
| 60 | local filename=`basename ${backup}` |
| 61 | fi |
| 62 | |
| 63 | if [ -f ${temp} ]; then |
| 64 | rm ${temp} |
| 65 | fi |
| 66 | |
| 67 | touch ${temp} |
| 68 | |
| 69 | echo ${temp} |
| 70 | } |
| 71 | |
| 72 | |
| 73 | # end-conf-creation {config file name} |
| 74 | function end-conf-creation { |
| 75 | local conf=${1} |
| 76 | local temp="${conf}.tmp" |
| 77 | |
| 78 | mv ${temp} ${conf} |
| 79 | } |
Yuta HIGUCHI | e748ecc | 2014-05-28 13:55:03 -0700 | [diff] [blame] | 80 | |
| 81 | # Confirm user whether to continue if running as root. |
| 82 | function confirm-if-root { |
| 83 | if [ "`whoami`" == "root" ]; then |
| 84 | echo "This script should not be run as root. Are you sure you want to continue?[y/N]" |
| 85 | local key |
| 86 | read key |
| 87 | if [ "${key}" == "Y" -o "${key}" == "y" ]; then |
| 88 | echo "Continue running as root." |
| 89 | else |
| 90 | echo "Exiting script" |
| 91 | exit 1 |
| 92 | fi |
| 93 | fi |
| 94 | } |