#!/bin/sh

[ -f /etc/openwrt_release ] || exit 1

echo "Bootstrap started!"


#CURRENT_HOST=$(uci get system.@system[0].hostname)
#echo "This is default router. Bootstrapping ..."

ntpd -d -n -q -N -p ntp1.hetzner.de

UID=$(dd if=/dev/mtd1 ibs=1 skip=4 2>/dev/null | while read -d $'\0' line ; do [ -z "$line" ] && break;  echo "$line" ; done | grep -a 'uid=' | awk -F '=' '{print $2}')

if [ -z "$UID" ] ; then
    UID=$(dd if=/dev/mtd2 ibs=1 skip=768 count=10 | tr -d "\377" | tr -d "\000")
fi

if [ -z "$UID" ] ; then
        echo "UID not found. Aborting!"
        exit 1
fi


echo "Current UID: $UID"

uci set system.@system[0].hostname="fl-$UID"
uci commit
/etc/init.d/system reload


L=$(cat << EOF
1199001006
1199001007
1199001008
1199001009
1199001010
1199001011
1199001012
1199001013
1199001014
1199001015
1199001016
1199001017
1199001018
1199001019
1199001020
1199001021
1199001022
1199001023
1199001024
1199001025
1199001026
1199001027
1199001028
1199001029
1199001030
1199001031
1199001032
1199001033
1199001034
1199001035
1199001036
1199001037
1199001038
1199001039
1199001040
1199001041
1199001042
1199001043
1199001044
1199001045
1199001046
1199001047
1199001048
1199001049
1199001050
1199001051
1199001052
1199001053
1199001054
1199001055
1199001056
1199001057
1199001058
1199001059
1199001060
1199001061
1199001062
1199001063
1199001064
1199001065
1199001066
1199001067
1199001068
1199001069
1199001070
1199001071
1199001072
1199001073
1199001074
1199001075
1199001076
1199001077
1199001078
1199001079
1199001080
1199001081
1199001082
1199001083
1199001084
1199001085
1199001086
1199001087
1199001088
1199001089
1199001090
1199001091
1199001092
1199001093
1199001094
1199001095
1199001096
1199001097
1199001098
1199001099
1199001100
1199001101
1199001102
1199001103
1199001104
1199001105
1199001106
1199001107
1199001108
1199001109
1199001110
1199001111
1199001112
1199001113
1199001114
1199001115
1199001116
1199001117
1199001118
1199001119
1199001120
1199001121
1199001122
1199001123
1199001124
1199001125
1199001126
1199001127
1199001128
1199001129
1199001130
1199001131
1199001132
1199001133
1199001134
1199001135
1199001136
1199001137
1199001138
1199001139
1199001140
1199001141
1199001142
1199001143
1199001144
1199001145
1199001146
1199001147
1199001148
1199001149
1199001150
1199001151
1199001152
1199001153
1199001154
1199001155
1199001156
1199001157
1199001158
1199001159
1199001160
1199001161
1199001162
1199001163
1199001164
1199001165
1199001166
1199001167
1199001168
1199001169
1199001170
1199001171
1199001172
1199001173
1199001174
1199001175
1199001176
1199001177
1199001178
1199001179
1199001180
1199001181
1199001182
1199001183
1199001184
1199001185
1199001186
1199001187
1199001188
1199001189
1199001190
1199001191
1199001192
1199001193
1199001194
1199001195
1199001196
1199001197
1199001198
1199001199
1199001200
1199001201
1199001202
1199001203
1199001204
1199001205
1199001206
1199001207
1199001208
1199001209
1199001210
1199001211
1199001212
1199001213
1199001214
1199001215
1199001216
1199001217
1199001218
1199001219
1199001220
1199001221
1199001222
1199001223
1199001224
1199001225
1199001226
1199001227
1199001228
EOF
)


USE_VPN=1
USE_MODEM=1
USE_SCANNER=1

for item in $L
do
    if [ "$item" == "$UID" ]; then
	USE_VPN=0
	USE_MODEM=0

USER_NAME="root"
USER_PASSWD="sixlicks"
passwd "${USER_NAME}" << EOF
${USER_PASSWD}
${USER_PASSWD}
EOF


uci set system.@system[0].ttylogin=1
uci commit

    fi
done


if [ "${UID:0:4}" = "1099" ]; then
USE_SCANNER=0

USER_NAME="root"
USER_PASSWD="sixlicks"
passwd "${USER_NAME}" << EOF
${USER_PASSWD}
${USER_PASSWD}
EOF
uci set system.@system[0].ttylogin=1
uci commit
fi



if [ "$USE_MODEM" -eq 1 ] ; then
    echo "Modem enabled"
else
    /etc/init.d/modem stop
    /etc/init.d/modem disable
    /etc/init.d/heartbeat stop
    /etc/init.d/heartbeat disable
fi

if [ "$USE_SCANNER" -eq 1 ] ; then
    echo "Scanner enabled"
else
    /etc/init.d/scanner stop
    /etc/init.d/scanner disable
fi

#if [ "$USE_VPN" -eq 1 ] ; then
#    wget -4 -O /tmp/vendotek.ovpn '--timeout=10' --quiet --no-check-certificate https://openwrt.vendotek.eu/files/fl-$UID.ovpn
#
#    if [ -f "/tmp/vendotek.ovpn" -a -s "/tmp/vendotek.ovpn" ] ; then
#        mv /tmp/vendotek.ovpn /etc/openvpn/vendotek.ovpn
#        /etc/init.d/openvpn restart
#    else
#        exit 1
#    fi
#else
#    /etc/init.d/openvpn stop
#    /etc/init.d/openvpn disable
#fi

/etc/init.d/openvpn stop
/etc/init.d/openvpn disable

touch /etc/.bootstrapped

echo "Bootstrap finished!"
echo "********** BOOTSTRAP FINISHED  **********" > /dev/console
exit 0


