#!/bin/bash # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. cedit() { # [-v] [section_name] FILE local s diff name init local file_dir="$(dirname "$file")" local exists=true local verbose=false if [[ $1 == -v ]]; then verbose=true shift fi if (( $# == 2 )); then name=": $1" shift fi local file="$1" local file_name="${file##*/}" local begin="#_#_# start delimiter of cedit section$name. do not modify. #_#_#" local end="#_#_# end delimiter of cedit section$name. do not modify. #_#_#" if [[ ! -e $file_dir ]]; then if ! mkdir -p $file_dir; then s=sudo $s mkdir -p $file_dir || return 1 fi fi if [[ ! -e $file ]]; then exists=false if ! $s touch $file; then s=sudo $s touch $file || return 1 fi fi [[ -w $file ]] || s=sudo if $exists; then local temp="$(mktemp -d)/$file_name" cp "$file" "$temp" cp /dev/null "$file" local in_section=false while IFS= read -r line; do if [[ $line == $begin ]]; then in_section=true; fi if ! $in_section; then printf '%s\n' "$line" >> $file fi if [[ $line == $end ]]; then in_section=false; fi done < "$temp" fi IFS= read -d '' -n 1 -r init if [[ $init ]]; then $s tee -a "$file" >/dev/null <<<"$begin" printf '%s' "$init" | $s tee -a "$file" >/dev/null $s tee -a "$file" >/dev/null $s tee -a "$file" >/dev/null <<<"$end" fi if ! $exists; then ret=0 if $verbose; then echo "New file $file:" cat "$file" fi elif type -t diff &>/dev/null; then diff=$(diff -u "$temp" "$file") ret=$? if $verbose; then if (( $ret == 0 )); then echo "No changes made to $file" fi else echo "backup of original at $temp" echo diff -u "$temp" "$file": echo "$diff" fi else diff=$(cmp "$temp" "$file") ret=$? if $verbose; then echo "$diff" fi fi return $ret } cedit "$@"