init
This commit is contained in:
commit
428102c71b
2 changed files with 265 additions and 0 deletions
197
bash-ini-parser/bash-ini-parser
Normal file
197
bash-ini-parser/bash-ini-parser
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
#
|
||||
# based on http://theoldschooldevops.com/2008/02/09/bash-ini-parser/
|
||||
#
|
||||
|
||||
PREFIX="cfg_section_"
|
||||
|
||||
function debug {
|
||||
return #abort debug
|
||||
echo $*
|
||||
echo --start--
|
||||
echo "${ini[*]}"
|
||||
echo --end--
|
||||
echo
|
||||
}
|
||||
|
||||
function cfg_parser {
|
||||
shopt -p extglob &> /dev/null
|
||||
CHANGE_EXTGLOB=$?
|
||||
if [ $CHANGE_EXTGLOB = 1 ]
|
||||
then
|
||||
shopt -s extglob
|
||||
fi
|
||||
ini="$(<$1)" # read the file
|
||||
ini="${ini//[/\\[}" # escape [
|
||||
debug
|
||||
ini="${ini//]/\\]}" # escape ]
|
||||
debug
|
||||
IFS=$'\n' && ini=( ${ini} ) # convert to line-array
|
||||
debug
|
||||
ini=( ${ini[*]//;*/} ) # remove comments with ;
|
||||
debug
|
||||
ini=( ${ini[*]/#+([[:space:]])/} ) # remove init whitespace
|
||||
debug "whitespace around"
|
||||
ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around =
|
||||
debug
|
||||
ini=( ${ini[*]/#\\[/\}$'\n'"$PREFIX"} ) # set section prefix
|
||||
debug
|
||||
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
|
||||
debug
|
||||
ini=( ${ini[*]/=/=\( } ) # convert item to array
|
||||
debug
|
||||
ini=( ${ini[*]/%/ \)} ) # close array parenthesis
|
||||
debug
|
||||
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
|
||||
debug
|
||||
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
|
||||
debug
|
||||
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
|
||||
ini=( ${ini[*]/%\{/\{$'\n''cfg_unset ${FUNCNAME/#'$PREFIX'}'$'\n'} ) # clean previous definition of section
|
||||
debug
|
||||
ini[0]="" # remove first element
|
||||
debug
|
||||
ini[${#ini[*]} + 1]='}' # add the last brace
|
||||
debug
|
||||
eval "$(echo "${ini[*]}")" # eval the result
|
||||
EVAL_STATUS=$?
|
||||
if [ $CHANGE_EXTGLOB = 1 ]
|
||||
then
|
||||
shopt -u extglob
|
||||
fi
|
||||
return $EVAL_STATUS
|
||||
}
|
||||
|
||||
function cfg_writer {
|
||||
SECTION=$1
|
||||
OLDIFS="$IFS"
|
||||
IFS=' '$'\n'
|
||||
if [ -z "$SECTION" ]
|
||||
then
|
||||
fun="$(declare -F)"
|
||||
else
|
||||
fun="$(declare -F $PREFIX$SECTION)"
|
||||
if [ -z "$fun" ]
|
||||
then
|
||||
echo "section $SECTION not found" >2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
fun="${fun//declare -f/}"
|
||||
for f in $fun; do
|
||||
[ "${f#$PREFIX}" == "${f}" ] && continue
|
||||
item="$(declare -f ${f})"
|
||||
item="${item##*\{}" # remove function definition
|
||||
item="${item##*FUNCNAME*$PREFIX\};}" # remove clear section
|
||||
item="${item/\}}" # remove function close
|
||||
item="${item%)*}" # remove everything after parenthesis
|
||||
item="${item});" # add close parenthesis
|
||||
vars=""
|
||||
while [ "$item" != "" ]
|
||||
do
|
||||
newvar="${item%%=*}" # get item name
|
||||
vars="$vars $newvar" # add name to collection
|
||||
item="${item#*;}" # remove readed line
|
||||
done
|
||||
eval $f
|
||||
echo "[${f#$PREFIX}]" # output section
|
||||
for var in $vars; do
|
||||
eval 'local length=${#'$var'[*]}' # test if var is an array
|
||||
if [ $length == 1 ]
|
||||
then
|
||||
echo $var=\"${!var}\" #output var
|
||||
else
|
||||
echo ";$var is an array" # add comment denoting var is an array
|
||||
eval 'echo $var=\"${'$var'[*]}\"' # output array var
|
||||
fi
|
||||
done
|
||||
done
|
||||
IFS="$OLDIFS"
|
||||
}
|
||||
|
||||
function cfg_unset {
|
||||
SECTION=$1
|
||||
OLDIFS="$IFS"
|
||||
IFS=' '$'\n'
|
||||
if [ -z "$SECTION" ]
|
||||
then
|
||||
fun="$(declare -F)"
|
||||
else
|
||||
fun="$(declare -F $PREFIX$SECTION)"
|
||||
if [ -z "$fun" ]
|
||||
then
|
||||
echo "section $SECTION not found" >2
|
||||
return
|
||||
fi
|
||||
fi
|
||||
fun="${fun//declare -f/}"
|
||||
for f in $fun; do
|
||||
[ "${f#$PREFIX}" == "${f}" ] && continue
|
||||
item="$(declare -f ${f})"
|
||||
item="${item##*\{}" # remove function definition
|
||||
item="${item##*FUNCNAME*$PREFIX\};}" # remove clear section
|
||||
item="${item/\}}" # remove function close
|
||||
item="${item%)*}" # remove everything after parenthesis
|
||||
item="${item});" # add close parenthesis
|
||||
vars=""
|
||||
while [ "$item" != "" ]
|
||||
do
|
||||
newvar="${item%%=*}" # get item name
|
||||
vars="$vars $newvar" # add name to collection
|
||||
item="${item#*;}" # remove readed line
|
||||
done
|
||||
for var in $vars; do
|
||||
unset $var
|
||||
done
|
||||
done
|
||||
IFS="$OLDIFS"
|
||||
}
|
||||
|
||||
function cfg_clear {
|
||||
SECTION=$1
|
||||
OLDIFS="$IFS"
|
||||
IFS=' '$'\n'
|
||||
if [ -z "$SECTION" ]
|
||||
then
|
||||
fun="$(declare -F)"
|
||||
else
|
||||
fun="$(declare -F $PREFIX$SECTION)"
|
||||
if [ -z "$fun" ]
|
||||
then
|
||||
echo "section $SECTION not found" >2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
fun="${fun//declare -f/}"
|
||||
for f in $fun; do
|
||||
[ "${f#$PREFIX}" == "${f}" ] && continue
|
||||
unset -f ${f}
|
||||
done
|
||||
IFS="$OLDIFS"
|
||||
}
|
||||
|
||||
function cfg_update {
|
||||
SECTION=$1
|
||||
VAR=$2
|
||||
OLDIFS="$IFS"
|
||||
IFS=' '$'\n'
|
||||
fun="$(declare -F $PREFIX$SECTION)"
|
||||
if [ -z "$fun" ]
|
||||
then
|
||||
echo "section $SECTION not found" >2
|
||||
exit 1
|
||||
fi
|
||||
fun="${fun//declare -f/}"
|
||||
item="$(declare -f ${fun})"
|
||||
#item="${item##* $VAR=*}" # remove var declaration
|
||||
item="${item/\}}" # remove function close
|
||||
item="${item}
|
||||
$VAR=(${!VAR})
|
||||
"
|
||||
item="${item}
|
||||
}" # close function again
|
||||
|
||||
eval "function $item"
|
||||
}
|
||||
|
||||
|
||||
# vim: filetype=sh
|
||||
68
config.sh
Executable file
68
config.sh
Executable file
|
|
@ -0,0 +1,68 @@
|
|||
#!/bin/bash
|
||||
# PiPilot Config Script
|
||||
|
||||
INI_FILE="${1:-config.ini}"
|
||||
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
||||
PARSER=$DIR/bash-ini-parser/bash-ini-parser
|
||||
|
||||
echo
|
||||
echo "::::: PiPilot Configuration :::::"
|
||||
echo
|
||||
|
||||
if [ -f $INI_FILE ] ;
|
||||
then
|
||||
# INI initialization
|
||||
echo "Detected INI file: $INI_FILE"
|
||||
if [ -f $PARSER ];
|
||||
then
|
||||
source $PARSER
|
||||
echo "Loaded bash-ini-parser (https://github.com/albfan/bash-ini-parser)"
|
||||
cfg_parser "$INI_FILE"
|
||||
echo "Parsed $INI_FILE"
|
||||
|
||||
#Network Setup
|
||||
|
||||
cfg_section_network
|
||||
|
||||
if [ $device = "wifi" ];
|
||||
then
|
||||
NET_DEV="wlan0"
|
||||
echo "Network device set to: '$NET_DEV'"
|
||||
if nmcli d wifi list | grep "*" | grep $ssid;
|
||||
then
|
||||
echo "Already connected to wifi: $ssid"
|
||||
else
|
||||
ifconfig $NET_DEV down
|
||||
echo "Disconnecting wifi"
|
||||
if nmcli c | grep $ssid;
|
||||
then
|
||||
nmcli c delete $ssid
|
||||
fi
|
||||
set -e
|
||||
echo "Connecting to wifi: $ssid"
|
||||
nmcli device wifi connect "$ssid" password "$key" ifname $NET_DEV
|
||||
set +e
|
||||
fi
|
||||
else
|
||||
NET_DEV="eth0"
|
||||
echo "Network device set to: '$NET_DEV'"
|
||||
fi
|
||||
|
||||
if [ $inet = "dhcp" ];
|
||||
then
|
||||
echo "Waiting for IP lease from DHCP server"
|
||||
ifconfig $NET_DEV up 0.0.0.0 0.0.0.0
|
||||
dhclient $NET_DEV
|
||||
else
|
||||
echo "Setting static IP to: $ip, Netmask: $netmask, Network: $net, Gateway: $gateway"
|
||||
ifconfig $NET_DEV down
|
||||
ifconfig $NET_DEV $ip netmask $netmask up
|
||||
route add -net $net netmask $netmask gw $gateway dev $NET_DEV
|
||||
fi
|
||||
|
||||
fi
|
||||
fi
|
||||
|
||||
echo
|
||||
echo "::::: PiPilot Done! :::::"
|
||||
echo
|
||||
Loading…
Add table
Add a link
Reference in a new issue