#!/bin/bash
rw
service homegear-dc-connector stop
sed -i 's/persistence true/persistence false/g' /etc/mosquitto/mosquitto.conf
sed -i 's/workingDirectory =.*/workingDirectory = \/var\/log\/homegear-dc-connector/g' /etc/homegear/dc-connector.conf
sed -i 's/logfilePath =.*/logfilePath = \/var\/log\/homegear-dc-connector/g' /etc/homegear/dc-connector.conf
sed -i 's/cacheAssets = 2592000/cacheAssets = 0/g' /etc/homegear/rpcservers.conf
sed -i 's/restServer = true/resetServer = false/g' /etc/homegear/rpcservers.conf
sed -i 's/"td":"300"/"td":"0"/g' /data/homegear-data/node-blue/flows.json
homegear-dc-connector -e core setTime
systemctl disable systemd-timesyncd
dpkg --purge fake-hwclock
apt-get -y install bind9 ntp
sed -i 's/driftfile.*/driftfile \/data\/ntp.drift/g' /etc/ntp.conf

cat > "/etc/bind/named.conf.local" <<-'EOF'
//
// Do any local configuration here
//

// Consider adding the 1918 zones here, if they are not used in your
// organization
//include "/etc/bind/zones.rfc1918";

zone "android.pool.ntp.org" {
    type master;
    file "/etc/bind/zones/android.pool.ntp.org.zone";
};
EOF

cat > "/etc/bind/named.conf.local" <<-'EOF'
//
// Do any local configuration here
//

// Consider adding the 1918 zones here, if they are not used in your
// organization
//include "/etc/bind/zones.rfc1918";

zone "android.pool.ntp.org" {
    type master;
    file "/etc/bind/zones/android.pool.ntp.org.zone";
};
EOF

cat > "/etc/bind/named.conf.options" <<-'EOF'
options {
	directory "/var/tmp/bind";

	// If there is a firewall between you and nameservers you want
	// to talk to, you may need to fix the firewall to allow multiple
	// ports to talk.  See http://www.kb.cert.org/vuls/id/800113

	// If your ISP provided one or more IP addresses for stable 
	// nameservers, you probably want to use them as forwarders.  
	// Uncomment the following block, and insert the addresses replacing 
	// the all-0's placeholder.

	// forwarders {
	// 	0.0.0.0;
	// };

	//========================================================================
	// If BIND logs error messages about the root key being expired,
	// you will need to update your keys.  See https://www.isc.org/bind-keys
	//========================================================================
	dnssec-validation auto;

	listen-on { <<IP>>; };
	//listen-on-v6 { any; };
};
EOF

ip=$(ifconfig eth0.30 | grep "inet " | xargs | cut -d " " -f 2)
sed -i "s/<<IP>>/$ip/g" /etc/bind/named.conf.options
mkdir /etc/bind/zones
cat > "/etc/bind/zones/android.pool.ntp.org.zone" <<-'EOF'
;;
;; BIND forward data file for zone berry.home
;;
 
$TTL          86400   ; time-to-live - 24 hours could have been written as 24h or 1d
 
@             IN      SOA         ns1.android.pool.ntp.org. mail.android.pool.ntp.org. (
                                             2015061201       ; Serial - (NOTE: Needs to increment every time you restart BIND)
                                                 604800       ; Refresh
                                                  86400       ; Retry
                                                2419200       ; Expire
                                                 604800 )     ; Default TTL
 
              IN            NS           ns1.android.pool.ntp.org.      ; nameserver
              IN            A            <<IP>>                 ; loop-back address
 
ns1           IN            A            <<IP>>    
2             IN            A            <<IP>>
EOF
sed -i "s/<<IP>>/$ip/g" /etc/bind/zones/android.pool.ntp.org.zone

cat > "/lib/systemd/scripts/setup-tmpfs.sh" <<-'EOF'
#!/bin/bash

modprobe zram num_devices=3

echo 134217728 > /sys/block/zram0/disksize
echo 20971520 > /sys/block/zram1/disksize
echo 134217728 > /sys/block/zram2/disksize

mkfs.ext4 /dev/zram0
mkfs.ext4 /dev/zram1
mkfs.ext4 /dev/zram2

mount /dev/zram0 /var/log
mount /dev/zram1 /var/lib/homegear/db
mount /dev/zram2 /var/tmp

chmod 777 /var/log
chmod 770 /var/lib/homegear/db
chown homegear:homegear /var/lib/homegear/db
chmod 777 /var/tmp

mkdir /var/tmp/bind
chown bind:bind /var/tmp/bind
mkdir /var/tmp/lock
chmod 777 /var/tmp/lock
mkdir /var/tmp/dhcp
chmod 755 /var/tmp/dhcp
mkdir /var/tmp/spool
chmod 755 /var/tmp/spool
mkdir /var/tmp/systemd
chmod 755 /var/tmp/systemd
touch /var/tmp/systemd/random-seed
chmod 600 /var/tmp/systemd/random-seed
mkdir -p /var/spool/cron/crontabs
chmod 731 /var/spool/cron/crontabs
chmod +t /var/spool/cron/crontabs

mkdir -p /var/tmp/homegear
chown homegear:homegear /var/tmp/homegear
chmod 770 /var/tmp/homegear

mkdir /var/log/homegear-dc-connector
chown homegear:homegear /var/log/homegear-dc-connector

mkdir -p /var/log/mosquitto/
touch /var/log/mosquitto/mosquitto.log
chown mosquitto:mosquitto /var/log/mosquitto/mosquitto.log


[ -d /data ] && [ ! -d /data/homegear-data ] && mkdir /data/homegear-data
[ -d /data/homegear-data ] && [ ! -d /data/homegear-data/node-blue ] && mkdir /data/homegear-data/node-blue
[ -d /data/homegear-data ] && [ ! -d /data/homegear-data/families ] && mkdir /data/homegear-data/families
[ -d /data/homegear-data ] && chown -R homegear:homegear /data/homegear-data/*
[ -f /data/homegear-data/db.sql ] && cp -a /data/homegear-data/db.sql /var/lib/homegear/db/ && rm -f /data/homegear-data/db.sql

exit 0
EOF
ro
