Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Keroi committed May 5, 2020
1 parent 846f300 commit dd48af3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 40 deletions.
11 changes: 5 additions & 6 deletions src/www/httpd/cgi-bin/get_configs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ elif [ -d "/home/yi-hack-v4" ]; then
fi

get_conf_type()
{
{
CONF="$(echo $QUERY_STRING | cut -d'=' -f1)"
VAL="$(echo $QUERY_STRING | cut -d'=' -f2)"

if [ "$CONF" == "conf" ] ; then
if [ $CONF == "conf" ] ; then
echo $VAL
fi
}
Expand All @@ -21,7 +21,7 @@ printf "Content-type: application/json\r\n\r\n"
CONF_TYPE="$(get_conf_type)"
CONF_FILE=""

if [ "$CONF_TYPE" == "mqtt" ] ; then
if [ $CONF_TYPE == "mqtt" ] ; then
CONF_FILE="$YI_HACK_PREFIX/etc/mqttv4.conf"
else
CONF_FILE="$YI_HACK_PREFIX/etc/$CONF_TYPE.conf"
Expand All @@ -37,9 +37,8 @@ while IFS= read -r LINE ; do
fi
done < "$CONF_FILE"

if [ "$CONF_TYPE" == "system" ] ; then
if [ $CONF_TYPE == "system" ] ; then
printf "\"%s\":\"%s\",\n" "HOSTNAME" "$(cat /etc/hostname)"
printf "\"%s\":\"%s\",\n" "TIMEZONE" "$(cat /etc/TZ)"
fi

# Empty values to "close" the json
Expand Down
42 changes: 8 additions & 34 deletions src/www/httpd/cgi-bin/set_configs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,11 @@ elif [ -d "/home/yi-hack-v4" ]; then
YI_HACK_PREFIX="/home/yi-hack-v4"
fi

#urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }

urldecode(){
echo -e "$(sed 's/+/ /g;s/%\(..\)/\\x\1/g;')"
}

sedencode(){
echo -e "$(sed 's/\\/\\\\\\/g;s/\&/\\\&/g;s/\//\\\//g;')"
}

get_conf_type()
{
{
CONF="$(echo $QUERY_STRING | cut -d'=' -f1)"
VAL="$(echo $QUERY_STRING | cut -d'=' -f2)"

if [ $CONF == "conf" ] ; then
echo $VAL
fi
Expand All @@ -29,7 +19,7 @@ get_conf_type()
CONF_TYPE="$(get_conf_type)"
CONF_FILE=""

if [ "$CONF_TYPE" == "mqtt" ] ; then
if [ $CONF_TYPE == "mqtt" ] ; then
CONF_FILE="$YI_HACK_PREFIX/etc/mqttv4.conf"
else
CONF_FILE="$YI_HACK_PREFIX/etc/$CONF_TYPE.conf"
Expand All @@ -43,38 +33,22 @@ for S in $PARAMS ; do
PARAM=$(echo "$S" | tr "=" " ")
KEY=""
VALUE=""

for SP in $PARAM ; do
if [ -z $KEY ]; then
KEY=$SP
else
VALUE=$SP
VALUE=$(echo "$SP" | urldecode)
fi
done

if [ "$KEY" == "HOSTNAME" ] ; then
if [ -z $VALUE ] ; then

# Use 2 last MAC address numbers to set a different hostname
MAC=$(cat /sys/class/net/wlan0/address|cut -d ':' -f 5,6|sed 's/://g')
if [ "$MAC" != "" ]; then
hostname yi-$MAC
else
hostname yi-hack
fi
hostname > /etc/hostname
else
hostname $VALUE

if [ $KEY == "HOSTNAME" ] ; then
if [ ! -z $VALUE ] ; then
echo "$VALUE" > /etc/hostname
fi
elif [ "$KEY" == "TIMEZONE" ] ; then
echo $VALUE > /etc/TZ
else
VALUE=$(echo "$VALUE" | sedencode)
sed -i "s/^\(${KEY}\s*=\s*\).*$/\1${VALUE}/" $CONF_FILE
fi

fi
done

# Yeah, it's pretty ugly.
Expand Down

0 comments on commit dd48af3

Please # to comment.