#!/bin/bash
#
# Install sdm on an already-running system and configure it so that sdm plugins can be run
#

function askyn() {
    local ans
    echo -n "$1" '[y/n]? ' ; read $2 ans </dev/tty
    case "$ans" in
        y*|Y*) return 0 ;;
        *) return 1 ;;
    esac
}

[ $EUID -eq 0 ] && sudo="" || sudo="sudo"

if [ ! -d /etc/sdm ]
then
    echo $"
This script will install sdm onto your running system in /usr/local/sdm. You can use it to:

a) Customize RasPiOS IMGs per the documentation at https://github.com/gitbls/sdm
b) Run plugins on this system that modify the running system

Presumably you are using this script for b). If your goal is only to achieve a) above, please
consider exiting this script and doing:

    curl -L https://raw.githubusercontent.com/gitbls/sdm/master/EZsdmInstaller | bash

"
    askyn "Continue" || exit 0
fi

echo "> Install sdm into /usr/local/sdm"
curl -L https://raw.githubusercontent.com/gitbls/sdm/master/EZsdmInstaller | bash
echo "> Create /etc/sdm/cparams"
myuser=$(whoami)
hostname=$(hostname)
echo "> sdm primary user will be '$myuser'; sudo edit /etc/sdm/cparams to change this"
(cat <<EOF
version:"V12.6"
thishost:""
aptcache:""
aptdistupgrade:""
autologin:"0"
fbatch:"0"
b0script:""
b1script:""
bootscripts:"0"
burnplugins:""
cscript:""
csrc:""
datefmt:"%Y-%m-%d %H:%M:%S"
debugs:""
dimg:""
dimgdev:"0"
dimgdevname:""
domain:""
ecolors:"blue:gray:red"
expandroot:""
exports:""
fchroot:"0"
fdirtree:"0"
fnoexpandroot:"0"
hname:""
hostname:"$hostname"
loadlocal:""
logwidth:"192"
dgroups:"dialout,cdrom,floppy,audio,video,plugdev,users,adm,sudo,users,input,netdev,spi,i2c,gpio"
myuser:"$myuser"
nowaittimesync:"0"
os:""
pi1bootconf:""
plugindebug:"0"
poptions:""
raspiosver:""
reboot:""
fredact:"0"
regensshkeys:""
noreboot:"0"
rebootwait:"20"
redocustomize:"0"
sdmdir:"/usr/local/sdm"
sdmflist:"sdm sdm-phase0 sdm-phase1 sdm-cparse sdm-readparams sdm-cmdsubs sdm-firstboot sdm-apt sdm-apt-cacher sdm-cportal sdm-logmsg sdm-gburn"
showapt:"0"
src:"/usr/local/sdm"
swapsize:"0"
timezone:""
virtmode:"nspawn"
vqemu:""
wificountry:""
custom1:""
custom2:""
custom3:""
custom4:""
plugins:""
allplugins:""
EOF
    ) | $sudo bash -c "cat >/etc/sdm/cparams"

$sudo touch /etc/sdm/history

cat <<EOF
* How to run an sdm plugin on the running system:

  sudo sdm --runonly plugins --plugin plugin-name:"args if there are any"

* Complete example for 'hotspot' plugin creating a routed hotspot:

sudo sdm --runonly plugins --plugin hotspot:"type=routed|ipforward=eth0|hsenable"
sudo nmcli c show   # View the new hotspot connections

* Complete example for 'hotspot' plugin creating a bridged hotspot:

sudo sdm --runonly plugins --plugin hotspot:"type=bridged|hsenable"
sudo nmcli c show
#
# Reboot to fully activate the hotspot
#
sudo reboot
#
# View the new hotspot connections after reboot
#
EOF

exit

