#!/bin/bash

# This file contains script functions to help modifying files.
# confirm_append   Appends to the end of the file
# confirm_replace  Locates a line and replaces a substring
#
# The functions are controlled through variables:
# file="/path/to/edited/file"
# pattern="installed"  Will skip if pattern matches. Leave empty to disable.
# 
# confirm_append:
# append="String to be appended.\n - Here's another line"
#
# confirm_replace:
# line="Line search pattern"
# search="String search pattern"
# replace="Replace matched substring with this"

# Text Colors:
esc="\033["
tc000="${esc}30m"
tcf00="${esc}31m"
tc0f0="${esc}32m"
tcff0="${esc}33m"
tc00f="${esc}34m"
tcf0f="${esc}35m"
tc0ff="${esc}36m"
tcfff="${esc}37m"
tcRst="${esc}0m"
# Background Colors:
bc000="${esc}40m"
bcf00="${esc}41m"
bc0f0="${esc}42m"
bcff0="${esc}43m"
bc00f="${esc}44m"
bcf0f="${esc}45m"
bc0ff="${esc}46m"
bcfff="${esc}47m"


check () {
s=$?
if [ "$s" = "0" ]; then
 printf "$tc0f0> Success!$tcRst\n"
else
 printf "$tcf00> PhaiL!!!$tcRst\n$tcff0> Smash your head on keyboard to continue.$tcRst\n"
 read -n 1 -s > /dev/null
fi
return $s
}

confirm () {
loop=1
while [ "$loop" = "1" ]; do
 printf "Do you want to continue? [Y/n/?] "
 read -n 1 -s answer
 case "$answer" in
  "Y" | "y" | "" )
   printf "${tc0f0}Yes$tcRst\n"
   loop=0
   return 0
   ;;
  "?" )
   printf "${tc00f}?$tcRst\nIt's really just a ${tc0f0}Yes$tcRst or ${tcf00}No$tcRst >:-[\n"
   ;;
  "N" | "n" )
   printf "${tcf00}No$tcRst\n"
   loop=0
   return 1
   ;;
  * )
   printf "${tcff0}$answer$tcRst\n"
   ;;
 esac
done
}

confirm_section () {
confirm || {
 printf "$tc00f> Skipping entire section.$tcRst\n"
 exit 1
}
}

backup () {
printf "Creating backup ${file}~\n"
cp -a "$file" "${file}~"
check
return $?
}

file_check () {
s=0
if [[ "$create" && ! -f "$file" ]]; then
 create=
 printf "  Create $file\n"
 printf "$content" | tee "$file"
 check || return $?
 s=1
fi
if [[ "$owner" && "`stat -c %U:%G \"$file\"`" != "$owner" ]]; then
 printf "  Change ownsership of $file\n"
 chown "$owner" "$file"
 check || return $?
 s=1
fi
if [[ "$perm" && "`stat -c %a \"$file\"`" != "$perm" ]]; then
 printf "  Change permissions of $file\n"
 chmod "$perm" "$file"
 check || return $?
 s=1
fi
if [ "$s" = "0" ]; then
 printf "${tc00f}> SKIPPED:$tcRst Already applied\n"
fi
return 0
}

pattern_count () {
awk "/$pattern/{n++}; END {print n+0}" "$file"
}
line_count () {
awk "/$line/{n++}; END {print n+0}" "$file"
}

pattern_confirm () {
if [ ! -f "$file" ]; then
 printf "${tcff0}> WARNING:$tcRst Could not find $file\n"
 return 1
fi
if [[ ( "$pattern"  && "`pattern_count`" -gt "0" ) || ( ! "$pattern" && "`line_count`" = "0" ) ]]; then
 printf "${tc00f}> SKIPPED:$tcRst Already applied\n"
 return 1
fi
return 0
}

append () {
printf "Appending to $file\n"
printf "$append" | tee -a "$file" > /dev/null
check
return $?
}

replace () {
printf "Scanning $file\n"
result="`awk \"/$line/{sub(/$search/, \\\"$replace\\\")}; {print}\" \"${file}\"`"
check && {
 printf "Writing $file\n"
 printf "$result" | tee "$file" > /dev/null
}
check
return $?
}

confirm_append () {
pattern_confirm && confirm && append
return $?
}

confirm_replace () {
pattern_confirm && confirm && replace
return $?
}