#!/usr/bin/env bash

# delete webmin root user for secury reasons

ROOT_PASS=`cat /etc/shadow |  grep ^root | cut -d : -f 2`

if [ "$ROOT_PASS" != "!!" ]; then
    echo "user root have password - no need for superadmin"
    exit 1
fi

echo Setup user superadmin for pbxmanager setup

# echo -n > /etc/webmin/miniserv.users

# create superadmin/superadmin webmin user as pbxmanager admin

cat >>/etc/webmin/config <<-EOF
notabs_superadmin=2
lang_superadmin=
gotoone_superadmin=1
theme_superadmin=
EOF

mysql -uroot -ppassw0rd pbxconf <<-EOF

INSERT INTO webmin_user (id, name, pass) VALUES (1, 'superadmin', '\$1\$87213638\$NKSQDZ30W7SqdSEHBXaKh0');

INSERT INTO webmin_user_attr (id, attr, value) VALUES (1, 'gotoone', '1');
INSERT INTO webmin_user_attr (id, attr, value) VALUES (1, 'lang', 'en.UTF-8');
INSERT INTO webmin_user_attr (id, attr, value) VALUES (1, 'modules', 'asterisk');
INSERT INTO webmin_user_attr (id, attr, value) VALUES (1, 'notabs', '2');
INSERT INTO webmin_user_attr (id, attr, value) VALUES (1, 'sync', '0');
INSERT INTO webmin_user_attr (id, attr, value) VALUES (1, 'theme', '');
INSERT INTO webmin_user_attr (id, attr, value) VALUES (1, 'tz', '');

INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, '', 'rpc', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'agent', 0);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'agents', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'branding', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'ccPermissions', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'ccReportManagement', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'cdr', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'cdr_settings', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'cdr_stats', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'cluster', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'conferences', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'connectchannels', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'contacts', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'contactsources', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'crm', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'db_settings', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'devices', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'dialing_permissions', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'dids', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'enduser', 0);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'enduser_company_contacts', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'eventhooks', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'extensions', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'files', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'generator', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'huntlists', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'license', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'links', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'mailboxes', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'menus', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'musiconhold', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'noconfig', 0);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'officemode', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'outbound_conferences', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'pbxinfo', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'phones', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'pickupgroups', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'preferences', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'provisioning_settings', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'queues', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'recordedcalls', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'recordings', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'rosters', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'routes', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'rpc', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'schedules', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'scripts', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'system_preferences', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'trunks', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'users', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'variables', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'webhooks', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'webmin', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'widgets', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'wiz', 1);
INSERT INTO webmin_user_acl (id, module, attr, value) VALUES (1, 'asterisk', 'miniforms', 0);
EOF
