various fixes
[distro-setup] / distro-end
index 40282d0089bcc2cb4729bb16dd7244a7ebc0badc..6625799d450e951f2c39bc6edd7ccd69626fa678 100755 (executable)
@@ -1,19 +1,41 @@
 #!/bin/bash -l
 # Copyright (C) 2016 Ian Kelling
-# This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
-set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+#     http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+errcatch
 
 set -x
 
+exec &> >(sudo tee -a /var/log/distro-end)
+echo "$0: $(date): starting now)"
+
+src="${BASH_SOURCE%/*}"
+
 end_msg() {
-    local y
+    =    local y
     IFS= read -r -d '' y  ||:
     end_msg_var+="$y"
 }
 
+spa() { # simple package add
+    simple_packages+=($@)
+}
+
 distro=$(distro-name)
 
+pending_reboot=false
+sed="sed --follow-symlinks"
 
 # template
 case $distro in
@@ -21,64 +43,961 @@ esac
 
 pup
 
-# universal packages
-x=(
-    bwm-ng
-    chromium
-    duplicity
-    fail2ban
-    fdupes
-    filelight
-    gdb
-    gnome-screenshot
+simple_packages=(
+    htop
     mailutils
-    meld
-    mpv
-    offlineimap
-    openvpn
-    p7zip
-    paprefs
-    pavucontrol
-    pianobar
-    pidgin
+    nmon
     rdiff-backup
-    slock
-    smartmontools
-    squashfs-tools
+    ruby
+    ruby-rest-client
     tree
-    virt-manager
+    vim
+    wcd
 )
 
-pi "${x[@]}"
+case $HOSTNAME in
+    lj|li) : ;;
+    *)
+        # universal packages
+        # swh-plugins is for karaoke pulsaudio filter.
+        # mutagen for pithos
+        simple_packages+=(
+            apache2
+            apache2-doc
+            apt-doc
+            apt-listchanges
+            aptitude-doc-en
+            bash-doc
+            beets
+            beets-doc
+            binutils-doc
+            bind9-doc
+            bwm-ng
+            chromium
+            cpio-doc
+            cloc
+            cron
+            debconf-doc
+            dirmngr
+            dnsutils
+            dtrx
+            duplicity
+            eclipse
+            evince
+            fdupes
+            feh
+            filelight
+            flashrom
+            gawk-doc
+            gcc-doc
+            gdb
+            gdb-doc
+            geoip-bin
+            git-doc
+            git-email
+            gitk
+            glibc-doc
+            goaccess
+            gnome-screenshot
+            gnome-session-flashback
+            i3lock
+            inetutils-traceroute
+            iproute2-doc
+            jq
+            kid3-qt
+            kid3-cli
+            linux-doc
+            locate
+            lshw
+            make-doc
+            manpages
+            manpages-dev
+            meld
+            mps-youtube
+            mumble
+            nagstamon
+            nginx-doc
+            nmap
+            offlineimap
+            oathtool
+            p7zip
+            paprefs
+            parted-doc
+            pavucontrol
+            pdfgrep
+            perl-doc
+            pianobar
+            pidgin
+            pry
+            python-autopep8
+            python3-doc
+            python3-mutagen
+            qrencode
+            reportbug
+            $(aptitude show ruby | sed -rn 's/Depends: (.*)/\1/p')-doc
+            sqlite3-doc
+            squashfs-tools
+            swh-plugins
+            tar-doc
+            tcpdump
+            telnet
+            transmission-remote-gtk
+            vlc
+            whois
+        )
+        spa $(apt-cache search ruby[.0-9]+-doc| awk '{print $1}')
+        ;;
+esac
+
+
+########### begin section including li ################
+
 
-# things with no equivalent in other distros:
 case $distro in
-    debian|ubuntu)
-        # for gui bug reporting
-        pi python-vte
-        pi apt-file aptitude
-        s apt-file update
-        # for debconf-get-selections
-        pi debconf-utils
+    fedora) spa unrar ;;
+    *) spa unrar-free ;;
+esac
+
+
+case $distro in
+    arch)
+        # ubuntu 14.04 uses b-cron,
+        # but its not maintained in arch.
+        # of the ones in the main repos, cronie is only one maintained.
+        # fcron appears abandoned software.
+        pi cronie
+        sgo cronie
         ;;
+    *) : ;; # other distros come with cron.
 esac
 
 
-####### misc packages ###########
+if isdeb; then
+    pi debian-goodies
+fi
 
 
+case $distro in
+    *) pi at ;;&
+    arch) sgo atd ;;
+esac
+
+
+case $distro in
+    debian) pi curl;;
+    arch) : ;;
+    # fedora: unknown
+esac
+
 case $distro in
     # tk for gitk
-    arch) pi git tk ;;
-    *) pi git ;;
+    arch) spa git tk ;;
+    *) spa git ;;
+esac
+
+case $distro in
+    arch) spa the_silver_searcher ;;
+    debian|ubuntu|trisquel) spa silversearcher-ag ;;
+    # fedora unknown
+esac
+
+case $distro in
+    debian|ubuntu|trisquel) spa ntp;;
+    arch)
+        pi ntp
+        sgo ntpd
+        ;;
+    # others unknown
 esac
 
+
+# no equivalent in other distros:
 case $distro in
-    arch) pi the_silver_searcher ;;
-    debian|ubuntu) pi silversearcher-ag ;;
+    debian|ubuntu|trisquel)
+        pi aptitude
+        if ! dpkg -s apt-file &>/dev/null; then
+            # this condition is just a speed optimization
+            pi apt-file
+            s apt-file update
+        fi
+        # for debconf-get-selections
+        spa debconf-utils
+        ;;
+esac
+
+case $distro in
+    ubuntu|trisquel|debian) spa ack-grep ;;
+    arch|fedora) spa ack ;;
     # fedora unknown
 esac
 
+case $distro in
+    arch|debian|ubuntu|trisquel)
+        spa bash-completion
+        ;;
+    # others unknown
+esac
+
+
+
+
+
+# disable motd junk.
+case $distro in
+    debian)
+        # allows me to pipe with ssh -t, and gets rid of spam
+        # http://forums.debian.net/viewtopic.php?f=5&t=85822
+        # i'd rather disable the service than comment the init file
+        # this says disabling the service, it will still get restarted
+        # but this script doesn't do anything on restart, so it should be fine
+        s dd of=/var/run/motd.dynamic if=/dev/null
+        # stretch doesn't have initscripts pkg installed by default
+        if [[ $(debian-codename) == jessie ]]; then
+            s update-rc.d motd disable
+        fi
+        ;;
+    ubuntu|trisquel)
+        # this isn't a complete solution. It still shows me when updates are available,
+        # but it's no big deal.
+        s t /etc/update-motd.d/10-help-text /etc/update-motd.d/00-header
+        ;;
+esac
+
+# automatic updates
+# reference:
+# https://debian-handbook.info/browse/stable/sect.regular-upgrades.html
+# /etc/cron.daily/apt calls unattended-upgrades
+# /usr/share/doc/unattended-upgrades# cat README.md
+# /etc/apt/apt.conf.d/50unattended-upgrades
+if isdebian; then
+    setup-debian-auto-update
+fi
+
+# we've got a few dependencies later on, so install them now.
+pi "${simple_packages[@]}"
+simple_packages=()
+
+
+### begin docker install ####
+# https://store.docker.com/editions/community/docker-ce-server-debian?tab=description
+pi  software-properties-common apt-transport-https
+curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
+sudo add-apt-repository \
+     "deb [arch=amd64] https://download.docker.com/linux/debian \
+         $(lsb_release -cs) \
+         stable"
+p update
+pi docker-ce
+sgo docker
+###  end docker install ####
+
+
+case $distro in
+    debian)
+        # note, need python-certbot-nginx for nginx, but it depends on nginx,
+        # and I'm not installing nginx by default right now
+        if isdebian-testing; then
+            pi --install-suggests certbot
+        else
+            pi --install-suggests -t jessie-backports certbot
+        fi
+        # make a version of the certbot timer that emails me.
+        x=/systemd/system/certbot
+        $sed -r -f - /lib$x.timer <<'EOF' |s dd of=/etc${x}mail.timer
+s,^Description.*,\0 mail version,
+EOF
+        $sed -r -f - /lib$x.service <<'EOF' |s dd of=/etc${x}mail.service
+s,(ExecStart=)(/usr/bin/certbot),\1/a/bin/log-quiet/sysd-mail-once certbotmail \2 --renew-hook /a/bin/distro-setup/certbot-renew-hook,
+EOF
+        ser daemon-reload
+        sgo certbotmail.timer
+
+        ;;
+    # todo: other distros unknown
+esac
+
+# website setup
+case $HOSTNAME in
+    lj|li)
+
+        case $HOSTNAME in
+            lj) domain=iank.bid; exit 0 ;;
+            li) domain=iankelling.org ;;
+        esac
+        /a/h/setup.sh $domain
+        /a/h/build.rb
+
+        sudo -E /a/bin/mediawiki-setup/mw-setup-script
+        #$src/phab-setup
+
+        pi-nostart mumble-server
+        s $sed -ri "s/^ *(serverpassword=).*/\1$(< /a/bin/bash_unpublished/mumble_pass)/" /etc/mumble-server.ini
+
+        # do certificate to avoid warning about unsigned cert,
+        # which is overkill for my use, but hey, I'm cool, I know
+        # how to do this.
+        web-conf apache2 mumble.iankelling.org
+        s rm -f /etc/apache2/sites-enabled/mumble.iankelling.org
+        sudo -i <<'EOF'
+export RENEWED_LINEAGE=/etc/letsencrypt/live/mumble.iankelling.org
+/a/bin/distro-setup/certbot-renew-hook
+EOF
+
+        sgo mumble-server
+
+        vpn-server-setup -d
+        tee /etc/openvpn/client-config/mail <<'EOF'
+ifconfig-push 10.8.0.4 255.255.255.0
+EOF
+
+
+        sudo dd of=/etc/systemd/system/vpnmail.service <<EOF
+[Unit]
+Description=Turns on iptables mail nat
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/a/bin/distro-setup/vpn-mail-forward start
+ExecStop=/a/bin/distro-setup/vpn-mail-forward stop
+
+[Install]
+WantedBy=openvpn.service
+EOF
+        ser daemon-reload
+        ser enable vpnmail.service
+        # needed for li's local mail delivery.
+        tu /etc/hosts <<<"10.8.0.4 mail.iankelling.org"
+        sgo openvpn
+        # setup let's encrypt cert
+        web-conf apache2 mail.iankelling.org
+        s rm /etc/apache2/sites-enabled/mail.iankelling.org{,-redir}.conf
+        ser reload apache2
+
+        domain=cal.iankelling.org
+        web-conf -f 10.8.0.4:5232 - apache2 $domain <<'EOF'
+#https://httpd.apache.org/docs/2.4/mod/mod_authn_core.html#authtype
+# https://stackoverflow.com/questions/5011102/apache-reverse-proxy-with-basic-authentication
+        <Location />
+                Options +FollowSymLinks +Multiviews +Indexes
+                AllowOverride None
+                AuthType basic
+                AuthName "Authentication Required"
+                # setup one time, with root:www-data, 640
+                AuthUserFile "/etc/caldav-htpasswd"
+                Require valid-user
+        <Location />
+EOF
+        # nginx version of above would be:
+        # auth_basic "Not currently available";
+        # auth_basic_user_file /etc/nginx/caldav/htpasswd;
+
+
+        ########## begin pump.io setup ##########
+
+        # once pump adds a logrotation script, turn off nologger,
+        # and add
+        #        "logfile": "/var/log/pumpio/pumpio.log",
+        #
+        s dd of=/etc/pump.io.json <<'EOF'
+{
+    "secret":  "SECRET_REPLACE_ME",
+    "driver":  "mongodb",
+    "params":  { "dbname": "pumpio" },
+    "noweb":  false,
+    "site":  "pump.iankelling.org",
+    "owner":  "Ian Kelling",
+    "ownerURL":  "https://iankelling.org/",
+    "port":  8001,
+    "urlPort": 443,
+    "hostname":  "pump.iankelling.org",
+    "nologger": true,
+    "datadir": "/home/pumpio/pumpdata",
+    "enableUploads": true,
+    "debugClient": false,
+    "disableRegistration": true,
+    "noCDN": true,
+    "key": "/home/pumpio/privkey.pem",
+    "cert": "/home/pumpio/fullchain.pem",
+    "address":  "localhost",
+    "sockjs": false
+}
+EOF
+        s sed -i "s#SECRET_REPLACE_ME#$(cat /p/c/machine_specific/li/pump-secret)#" /etc/pump.io.json
+
+        # jessie\'s node is too old
+        # https://nodejs.org/en/download/package-manager/
+        curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
+        pi nodejs
+        cd /home/ian
+        rm -rf pump.io.git
+        git clone https://github.com/pump-io/pump.io.git
+        cd pump.io
+        # note: doing this or the npm install pump.io as root had problems.
+        npm install
+        npm run build
+        # normally, next command would be
+        # s npm install -g databank-mongodb
+        # but it\'s this until a bug in pump gets fixed
+        s npm install -g databank-mongodb@0.19.2
+        s useradd -m -s /bin/false pumpio
+        sudo -u pumpio mkdir -p /home/pumpio/pumpdata
+        # for testing browser when only listening to localhost,
+        # in the pump.io.json, set hostname localhost, urlPort 5233
+        #ssh -L 5233:localhost:5233 li
+
+        s mkdir -p /var/log/pumpio/
+        s chown pumpio:pumpio /var/log/pumpio/
+
+        web-conf - apache2 pump.iankelling.org <<'EOF'
+# currently a bug in pump that we cant terminate ssl
+         SSLProxyEngine On
+         ProxyPreserveHost On
+         ProxyPass /  https://127.0.0.1:8001/
+         ProxyPassReverse /  https://127.0.0.1:8001/
+         # i have sockjs disabled per people suggesting that
+         # it won\'t work with apache right now.
+         # not sure if it would work with this,
+         # but afaik, this is pointless atm.
+         <Location /main/realtime/sockjs/>
+             ProxyPass wss://127.0.0.1:8001/main/realtime/sockjs/
+             ProxyPassReverse wss://127.0.0.1:8001/main/realtime/sockjs/
+         </Location>
+EOF
+
+        sudo -i <<'EOF'
+export RENEWED_LINEAGE=/etc/letsencrypt/live/pump.iankelling.org
+/a/bin/distro-setup/certbot-renew-hook
+EOF
+
+        s dd of=/etc/systemd/system/pump.service <<'EOF'
+[Unit]
+Description=pump.io
+After=syslog.target network.target
+
+[Service]
+Type=simple
+User=pumpio
+Group=pumpio
+ExecStart=/home/ian/pump.io/bin/pump
+Environment=NODE_ENV=production
+# failed to find databank-mongodb without this.
+# I just looked at my environment variables took a guess.
+Environment=NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
+
+[Install]
+WantedBy=multi-user.target
+EOF
+        ser daemon-reload
+        sgo pump
+        ########## end pump.io setup ############
+
+
+        ############# begin setup mastodon ##############
+
+        # I'd like to try gnu social just cuz of gnu, but it's not being
+        # well maintained, for example, simple pull requests
+        # languishing:
+        # https://git.gnu.io/gnu/gnu-social/merge_requests/143
+        # and I submitted my own bugs, basic docs are broken
+        # https://git.gnu.io/gnu/gnu-social/issues/269
+
+        # note, docker required, but we installed it earlier
+
+        # i subscrubed to https://github.com/docker/compose/releases.atom
+        # to deal with updates manually. So far, it means just reving the
+        # version number, then restarting docker-compose with
+        # cd ~/mastodon
+        # docker-compose up -d
+        curl -L https://github.com/docker/compose/releases/download/1.13.0/docker-compose-`uname -s`-`uname -m` | s dd of=/usr/local/bin/docker-compose
+        s chmod +x /usr/local/bin/docker-compose
+
+
+        cd ~
+        i clone https://github.com/tootsuite/mastodon
+        cd mastodon
+        # subbed to atom feed to deal with updates
+        git checkout $(git tag | grep -v rc | tail -n1)
+
+        # per instructions, uncomment redis/postgres persistence in docker-compose.yml
+        sed -i 's/^#//' docker-compose.yml
+
+        cat >.env.production <<'EOF'
+REDIS_HOST=redis
+REDIS_PORT=6379
+DB_HOST=db
+DB_USER=postgres
+DB_NAME=postgres
+DB_PASS=
+DB_PORT=5432
+
+LOCAL_DOMAIN=mast.iankelling.org
+LOCAL_HTTPS=true
+
+SINGLE_USER_MODE=true
+
+SMTP_SERVER=10.8.0.4
+SMTP_PORT=25
+SMTP_LOGIN=li
+SMTP_FROM_ADDRESS=notifications@mast.iankelling.org
+SMTP_DOMAIN=mast.iankelling.org
+SMTP_DELIVERY_METHOD=smtp
+EOF
+
+        for key in PAPERCLIP_SECRET SECRET_KEY_BASE OTP_SECRET; do
+            printf "%s=%s" $key "$(docker-compose run --rm web rake secret)" >>.env.production
+        done
+        s cat /etc/mailpass| while read -r domain port pass; do
+            if [[ $domain == mail.iankelling.org ]]; then
+                printf "SMTP_PASSWORD=%s" "$pass" >>.env.production
+                break
+            fi
+        done
+
+
+
+        docker-compose run --rm web rails assets:precompile
+
+        # docker daemon takes care of starting on boot.
+        docker-compose up -d
+
+        s a2enmod proxy_wstunnel headers
+        web-conf -f 3000 - apache2 mast.iankelling.org <<'EOF'
+   ProxyPreserveHost On
+   RequestHeader set X-Forwarded-Proto "https"
+   ProxyPass /500.html !
+   ProxyPass /oops.png !
+   ProxyPass /api/v1/streaming/ ws://localhost:4000/
+   ProxyPassReverse /api/v1/streaming/ ws://localhost:4000/
+   ErrorDocument 500 /500.html
+   ErrorDocument 501 /500.html
+   ErrorDocument 502 /500.html
+   ErrorDocument 503 /500.html
+   ErrorDocument 504 /500.html
+EOF
+
+
+        ############### !!!!!!!!!!!!!!!!!
+        ############### manual steps:
+
+        # only following 2 people atm, so not bothering to figure out backups
+        # when mastodon has not documented it at all.
+        #
+        # fsf@status.fsf.org
+        # cwebber@toot.cat
+        # dbd@status.fsf.org
+        # johns@status.fsf.org
+
+        # sign in page is at https://mast.iankelling.org/auth/sign_in
+        # register as iank, then
+        # https://github.com/tootsuite/documentation/blob/master/Running-Mastodon/Administration-guide.md
+        # docker-compose run --rm web bundle exec rails mastodon:make_admin USERNAME=iank
+
+        ############# end setup mastodon ##############
+
+        # we use nsupdate to update the ip of home
+        pi bind9
+
+        echo "$0: $(date): ending now)"
+        exit 0
+        ;;
+esac
+
+
+########### end section including li/lj ###############
+
+if [[ $HOSTNAME == treetowl ]]; then
+    # note, see bashrc for more documentation.
+    pi rss2email
+    s dd of=/etc/systemd/system/rss2email.service <<'EOF'
+[Unit]
+Description=rss2email
+After=multi-user.target
+
+[Service]
+User=ian
+Type=oneshot
+# about 24 hours of failures
+# it copies over its files without respecting symlinks, so
+# we pass options to use different location.
+ExecStart=/a/bin/log-quiet/sysd-mail-once -288 rss2email r2e -d /p/c/rss2email.json -c /p/c/rss2email.cfg run
+EOF
+    s dd of=/etc/systemd/system/rss2email.timer <<'EOF'
+[Unit]
+Description=rss2email
+
+[Timer]
+# for initial run. required.
+OnActiveSec=30
+# for subsequent runs.
+OnUnitInactiveSec=300
+
+[Install]
+WantedBy=timers.target
+EOF
+    s systemctl daemon-reload
+    sgo rss2email.timer
+fi
+
+######### begin  pump.io periodic backup #############
+if [[ $HOSTNAME == treetowl ]]; then
+    s dd of=/etc/systemd/system/pumpbackup.service <<'EOF'
+[Unit]
+Description=pump li backup
+After=multi-user.target
+
+[Service]
+User=ian
+Type=oneshot
+ExecStart=/a/bin/log-quiet/sysd-mail-once pump-backup /a/bin/distro-setup/pump-backup
+EOF
+    s dd of=/etc/systemd/system/pumpbackup.timer <<'EOF'
+[Unit]
+Description=pump li backup hourly
+
+[Timer]
+OnCalendar=hourly
+
+[Install]
+WantedBy=timers.target
+EOF
+    s systemctl daemon-reload
+    sgo pumpbackup.timer
+fi
+#########  end  pump.io periodic backup #############
+
+case $distro in
+    debian|ubuntu|trisquel)
+        # suggests because we want the resolvconf package.
+        # todo: check other distros to make sure it\'s installed
+        pi-nostart --install-suggests openvpn
+        # pi-nostart does not disable
+        ser disable openvpn
+        ;;
+    *) pi openvpn;;
+esac
+
+/a/bin/distro-setup/radicale-setup
+
+## android studio setup
+# this contains the setting for android sdk to point to
+# /a/opt/androidsdk, which is asked upon first run
+lnf /a/opt/.AndroidStudio2.2 ~
+# android site says it needs a bunch of packages for ubuntu,
+# but I googled for debian, and someone says you just need lib32stdc++6 plus the
+# jdk
+# https://pid7007blog.blogspot.com/2015/07/installing-android-studio-in-debian-8.html
+# see w.org for more android studio details
+spa lib32stdc++6 default-jdk
+
+
+if [[ $HOSTNAME == treetowl ]]; then
+    ############# begin syncthing setup ###########
+
+    # It\'s simpler to just worry about running it in one place for now.
+    # I assume it would work to clone it\'s config to another non-phone
+    # and just run it in one place instead of the normal having a
+    # separate config.  I lean toward using the same config, since btrfs
+    # syncs between comps.
+    case $distro in
+        arch) pi syncthing ;;
+        ubuntu|trisquel|debian)
+            # testing has relatively up to date packages
+            if ! isdebian-testing; then
+                # based on error when doing apt-get update:
+                # E: The method driver /usr/lib/apt/methods/https could not be found.
+                pi apt-transport-https
+                # google led me here:
+                # https://apt.syncthing.net/
+                curl -s https://syncthing.net/release-key.txt | sudo apt-key add -
+                s="deb http://apt.syncthing.net/ syncthing release"
+                if [[ $(cat /etc/apt/sources.list.d/syncthing.list) != $s ]]; then
+                    echo "$s" | s dd of=/etc/apt/sources.list.d/syncthing.list
+                    p update
+                fi
+            fi
+            pi syncthing
+            ;;
+    esac
+    lnf -T /w/syncthing /home/ian/.config/syncthing
+    sgo syncthing@ian # runs as ian
+
+    # these things persist in ~/.config/syncthing, which I save in
+    # /w/syncthing (not in /p, because syncthing should continue to
+    # run on home server even when using laptop as primary device)
+    # open http://localhost:8384/
+    # change listen address from default to tcp://:22001,
+    # this is because we do port forward so it doesn\'t have to use
+    # some external server, but the syncthing is broken for port forward,
+    # you get a message, something "like connected to myself, this should not happen"
+    # when connecting to other local devices, so I bump the port up by 1,
+    # based on
+    # https://forum.syncthing.net/t/connected-to-myself-should-not-happen/1763/19.
+    # Without this, it was being stuck syncing at 0%.
+    # Set gui username and password.
+    #
+    # install syncthing via f-droid,
+    # folder setting, turn off master folder (makes it read only).
+    # on phone, add device, click bar code icon
+    # on dekstop, top right, actions, device id
+    # after adding, notification will appear on desktop to confirm
+    #
+    # syncing folder. from phone to desktop: select desktop in the
+    # folder on phone\'s sync options, notification will appear in
+    # desktop\'s web ui within a minute. For the reverse, the
+    # notification will appear in android\'s notifications, you have to
+    # swipe down and tap it to add the folder. It won\'t appear in the
+    # syncthing ui, which would be intuitive, but don\'t wait for it
+    # there.
+    #
+    # On phone, set settings to run syncthing all the time, and
+    # show no notification.
+    #
+    # Folder versioning would make sense if I didn\'t already use btrfs
+    # for backups. I would choose staggered, or trash can for more space.
+    #
+    # if needed to install on a remote comp:
+    # ssh -L 8384:localhost:8384 -N frodo
+    # open http://localhost:8384/
+    #
+    # Note, the other thing i did was port forward port 22000,
+    # per https://docs.syncthing.net/users/firewall.html
+
+    #############  end syncthing setup ###########
+fi
+
+
+
+# no equivalent in other distros:
+case $distro in
+    debian|ubuntu|trisquel)
+        # for gui bug reporting
+        spa python-vte
+        ;;
+esac
+
+
+####### misc packages ###########
+
+# nagstamon setting which were set through the ui
+# in filters tab:
+# all unknown sources
+# all warning services
+# acknowledged hosts & services
+# hosts & services down for maintenence
+# services on down hosts
+# services on hosts in maintenece
+# services on unreachable osts
+# hosts in soft state
+# services in soft state
+# in display tab: icon in systray.
+
+case $distro in
+    debian|ubuntu|trisquel)
+        # it asks if it should make users in it's group capture packets without root,
+        # which is arguably more secure than running wireshark as root. default is no,
+        # which is what i prefer, since I plan to use tcpdump to input to wireshark.
+        s DEBIAN_FRONTEND=noninteractive pi wireshark-gtk
+        ;;
+    # others unknown
+esac
+
+
+case $distro in
+    debian|ubuntu|trisquel)
+        # no recommends because it wanted some other unstable package, something to
+        # do with math or something, which I didn't want to deal with.
+        p -y --no-install-recommends install python3-send2trash/unstable anki/unstable
+        ;;
+    # others unknown
+esac
+
+case $distro in
+    debian|ubuntu|trisquel)
+        # note i had to do this, which is persistent:
+        # cd /i/k
+        # s chgrp debian-transmission torrents partial-torrents
+
+        # syslog says things like
+        # 'Failed to set receive buffer: requested 4194304, got 425984'
+        # google suggets giving it even more than that
+        tu /etc/sysctl.conf<<'EOF'
+net.core.rmem_max = 67108864
+net.core.wmem_max = 16777216
+EOF
+        s sysctl -p
+
+        # some reason it doesn\'t seem to start automatically anyways
+        pi-nostart transmission-daemon
+
+        # the folder was moved here after an install around 02/2017.
+        # it contains runtime data,
+        # plus a simple symlink to the config file which it\'s
+        # not worth separating out.
+        s lnf -T /i/transmission-daemon /var/lib/transmission-daemon/.config/transmission-daemon
+        #
+        # config file documented here, and it\'s the same config
+        # for daemon vs client, so it\'s documented in the gui.
+        # https://trac.transmissionbt.com/wiki/EditConfigFiles#Options
+        #
+        # I originaly setup rpc-whitelist, but after using
+        # routing to a network namespace, it doesn\'t see the
+        # real source address, so it\'s disabled.
+        #
+        # Changed the cache-size to 256 mb, reduces disk use.
+        # It is a read & write cache.
+        #
+        s ruby <<'EOF'
+require 'json'
+p = '/etc/transmission-daemon/settings.json'
+File.write(p, JSON.pretty_generate(JSON.parse(File.read(p)).merge({
+'rpc-whitelist-enabled' => false,
+'rpc-authentication-required' => false,
+'incomplete-dir' => '/i/k/partial-torrents',
+'incomplete-dir-enabled' => true,
+'download-dir' => '/i/k/torrents',
+"speed-limit-up" => 800,
+"speed-limit-up-enabled" => true,
+"peer-port" => 61486,
+"cache-size-mb" => 256,
+"ratio-limit" => 5.0,
+"ratio-limit-enabled" => true,
+})) + "\n")
+EOF
+
+        # make sure its not enabled, not sure if this is needed
+        ser disable transmission-daemon
+        ;;
+    # todo: others unknown
+esac
+# adapted from /var/lib/dpkg/info/transmission-daemon.postinst
+if ! getent passwd debian-transmission > /dev/null; then
+    case $distro in
+        arch)
+            s useradd \
+              --system \
+              --create-home \
+              --home-dir /var/lib/transmission-daemon \
+              --shell /bin/false \
+              debian-transmission
+            ;;
+        *)
+            s adduser --quiet \
+              --system \
+              --group \
+              --no-create-home \
+              --disabled-password \
+              --home /var/lib/transmission-daemon \
+              debian-transmission
+            ;;
+    esac
+fi
+if [[ $HOSTNAME == treetowl ]]; then
+    sgo transmission-daemon-nn
+fi
+
+
+######### begin transmission client setup ######
+
+if [[ -e /p/transmission-rpc-pass ]]; then
+    # arch had a default config,
+    # debian had nothing until you start it.
+    # With a little trial an error, here is a minimal config
+    # taken from the generated one, plus changes that the
+    # settings ui does, without a bunch of ui crap settings.
+    #
+    # only settings I set were
+    # hostname
+    # auto-connect
+    # password
+
+    # the password is randomly generated on first run, i copied it out
+    # so it could be used by other hosts.
+    s ruby <<'EOF'
+require 'json'
+p = '/etc/transmission-daemon/settings.json'
+s = JSON.parse(File.read(p))
+s["rpc-password"] = File.read("/p/transmission-rpc-pass").chomp
+File.write p, JSON.pretty_generate(s)
+EOF
+
+    rpc_pass=$(</p/transmission-rpc-pass)
+    for f in /home/*; do
+        d=$f/.config/transmission-remote-gtk
+        u=${f##*/}
+        s -u $u mkdir -p $d
+        s -u $u dd of=$d/config.json <<EOF
+{
+  "profiles" : [
+    {
+      "profile-name" : "Default",
+      "hostname" : "transmission",
+      "rpc-url-path" : "/transmission/rpc",
+      "username" : "",
+      "password" : "$rpc_pass",
+      "auto-connect" : true,
+      "ssl" : false,
+      "timeout" : 40,
+      "retries" : 3,
+      "update-active-only" : false,
+      "activeonly-fullsync-enabled" : false,
+      "activeonly-fullsync-every" : 2,
+      "update-interval" : 3,
+      "min-update-interval" : 3,
+      "session-update-interval" : 60,
+      "exec-commands" : [
+      ],
+      "destinations" : [
+      ]
+    }
+  ],
+  "profile-id" : 0,
+  "add-options-dialog" : false
+}
+EOF
+    done
+fi
+
+# dunno why it\'s there, but get rid of it
+case $HOSTNAME in
+    li|lj) s rm -rf /home/linode ;;
+esac
+
+
+pi wget
+case $HOSTNAME in
+    tp|frodo)
+        case $distro in
+            debian|ubuntu|trisquel)
+                log=$(mktemp)
+                cd /a/opt
+                wget -nv -N https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
+                errallow
+                set -o pipefail
+                s dpkg -i google-chrome-stable_current_amd64.deb |& tee $log
+                code=$?
+                errcatch
+                case $code in
+                    0) : ;;
+                    *)
+                        # previously I had a more specific search, but dpkg
+                        # changed it\'s output as of 7/2016
+                        if grep 'dependency problems' \
+                                $log &>/dev/null; then
+                            s apt-get -fy install
+                        else
+                            exit 1
+                        fi
+                        ;;
+                esac
+                ;;
+            arch)
+                pi google-chrome
+                ;;
+        esac
+        ;;
+esac
+
 # printer
 case $distro in
     arch)
@@ -91,35 +1010,30 @@ case $distro in
         # In debian, I could use hte recommended driver,
         # in arch, I had to pick out the 6L driver.
         ;;
-    debian|ubuntu)
-        pi hplip
+    debian|ubuntu|trisquel)
+        spa hplip
         ;;
     # other distros unknown
 esac
 
 
-case $distro in
-    ubuntu|debian) pi ack-grep ;;
-    arch|fedora) pi ack ;;
-    # fedora unknown
-esac
 case $distro in
     ubuntu|debian) pi --no-install-recommends mairix notmuch ;;
-    fedora|arch) pi mairix notmuch ;;
+    fedora|arch) spa mairix notmuch ;;
 esac
 case $distro in
-    arch) pi nfs-utils ;;
-    ubuntu|debian) pi nfs-client ;;
+    arch) spa nfs-utils ;;
+    ubuntu|debian) spa nfs-client ;;
 esac
 case $distro in
-    ubuntu|debian) pi par2 ;;
-    arch|fedora) pi par2cmdline ;;
+    ubuntu|debian) spa par2 ;;
+    arch|fedora) spa par2cmdline ;;
 esac
 
 # needed for my tex resume
 case $distro in
-    ubuntu|debian) pi texlive-full ;;
-    arch) pi texlive-most ;;
+    ubuntu|debian) spa texlive-full ;;
+    arch) spa texlive-most ;;
     # fedora unknown
 esac
 
@@ -127,7 +1041,7 @@ case $distro in
     ubuntu)
         # flash, unrar, codecs, ms fonts.
         # This has a manual prompt.
-        pi ubuntu-restricted-extras
+        spa ubuntu-restricted-extras
         ;;
     fedora)
         pi yum-utils
@@ -140,98 +1054,104 @@ esac
 
 case $distro in
     # optional dep for firefox for h.264 video
-    arch) pi gst-libav ;;
+    arch) spa gst-libav ;;
     # other distros, probably come by default
 esac
 
 case $distro in
-    fedora|ubuntu|debian) pi gnupg-agent ;;
+    fedora|ubuntu|trisquel|debian) spa gnupg-agent ;;
     arch) : ;;
 esac
 
 
 case $distro in
-    fedora|ubuntu|debian) pi transmission ;;
-    arch) pi transmission-gtk ;;
-esac
-
-
-case $distro in
-    fedora) pi pinentry-gtk ;;
+    fedora) spa pinentry-gtk ;;
     *) : ;; # comes default or with other packages
 esac
 
 case $distro in
-    arch) pi firefox pulseaudio;;
+    arch) spa firefox pulseaudio;;
     *) : ;; # comes default or with other packages
 esac
 
+
 case $distro in
-    arch|debian|ubuntu)
-        pi bash-completion
-        ;;
+    arch) spa ttf-dejavu;;
+    debian|ubuntu|trisquel) spa fonts-dejavu ;;
     # others unknown
 esac
 
 
 case $distro in
-    arch) pi ttf-dejavu;;
-    debian|ubuntu) pi fonts-dejavu ;;
+    arch) spa xorg-xev;;
+    debian|ubuntu|trisquel) spa x11-utils ;;
     # others unknown
 esac
 
 case $distro in
-    arch|debian|ubuntu) pi ntp;;
+    arch) pi virt-install;;&
+    debian|ubuntu|trisquel) pi virtinst ;;&
+    *) pi virt-manager ;; # creates the libvirt group in debian at least
     # others unknown
 esac
+# allow user to run vms, from debian handbook
+for x in ian traci; do s usermod -a -G libvirt,kvm $x; done
+# bridge networking as user fails. google lead here, but it doesn\'t work:
+# oh well, I give up.
+# http://wiki.qemu.org/Features-Done/HelperNetworking
+# s mkdir /etc/qemu
+# f=/etc/qemu/bridge.conf
+# s dd of=$f <<'EOF'
+# allow br0
+# EOF
+# #s chown root:qemu $f # debian has somethig like qemu-libvirt. equivalent?
+# s chmod 640 $f
 
-case $distro in
-    arch) pi xorg-xev;;
-    debian|ubuntu) pi x11-utils ;;
-    # others unknown
-esac
 
 case $distro in
-    arch) pi virt-install;;
-    debian|ubuntu) pi virtinst ;;
+    arch) spa cdrkit;;
+    debian|ubuntu|trisquel) spa genisoimage;;
     # others unknown
 esac
 
 case $distro in
-    arch) pi cdrkit;;
-    debian|ubuntu) pi genisoimage;;
+    arch) spa spice-gtk3 ;;
+    debian|ubuntu|trisquel) spa spice-client-gtk;;
     # others unknown
 esac
 
+# general known for debian/ubuntu, not for fedora
+
 case $distro in
-    arch) pi spice-gtk3 ;;
-    debian|ubuntu) pi spice-client-gtk;;
+    debian|ubuntu|trisquel)
+        pi golang-go
+        # a bit of googling, and added settings to bashrc
+        go get -u github.com/mvdan/fdroidcl/cmd/fdroidcl
+        ;;
     # others unknown
 esac
 
-# general known for debian/ubuntu, not for fedora
+
 case $distro in
     arch)
         # cdrkit for cloud-init isos
-        # dnsmasq for nat networking in libvirt
+        # dnsmasq & ebtables for nat networking in libvirt
         # qemu for qemu-img, bind-tools for dig
-        pi unzip wget xorg-xmodmap \
+        # dmidecode just because syslog complains
+        pi unzip xorg-xmodmap dmidecode ebtables\
            bridge-utils dnsmasq qemu bind-tools
-        sgo ntpd
         # otherwise we get error about accessing kvm module.
         # seems like there might be a better way, but google was a bit vague.
-        s sed -ri '/^ *user *=/d' /etc/libvirt/qemu.conf
+        s $sed -ri '/^ *user *=/d' /etc/libvirt/qemu.conf
         echo 'user = "root"' | s tee -a /etc/libvirt/qemu.conf
         # https://bbs.archlinux.org/viewtopic.php?id=206206
         # # this should prolly go in the wiki
         sgo virtlogd.socket
-        sgo virtlogd.service
-        ;;
-esac
+        # guessing this is not needed
+        #sgo virtlogd.service
+        sgo libvirtd
 
-case $distro in
-    *) pi at ;;&
-    arch) sgo atd ;;
+        ;;
 esac
 
 case $distro in
@@ -241,57 +1161,118 @@ esac
 
 
 
-case $distro in
-    arch)
-        # ubuntu 14.04 uses b-cron,
-        # but it's not maintained in arch.
-        # of the ones in the main repos, cronie is only one maintained.
-        # fcron appears abandoned software.
-        pi cronie
-        sgo cronie
-        ;;
-    *) : ;; # other distros come with cron.
-esac
-
-
 case $distro in
     fedora) cabal install shellcheck ;;
-    *) pi shellcheck ;;
+    *) spa shellcheck ;;
     # unknown for older ubuntu
 esac
 
 
 case $distro in
-    arch|debian|ubuntu) pi pumpa ;;
+    arch|debian|ubuntu|trisquel) spa pumpa ;;
     # others unknown. do have a buildscript:
     # /a/bin/buildscripts/pumpa ;;
 esac
 
 
 case $distro in
-    debian|ubuntu) pi android-tools-adb ;;
-    arch) pi android-tools ;;
+    debian) pi adb ;;
+    debian|ubuntu|trisquel) spa android-tools-adbd/unstable ;;
+    arch) spa android-tools ;;
     # other distros unknown
 esac
 
+if [[ $HOSTNAME == treetowl ]]; then
+    case $distro in
+        debian)
+            if [[ `debian-archive`  == testing ]]; then
+                # has no unstable dependencies
+                pi bitcoind/unstable
+                src=/a/opt/bitcoin/contrib/init/bitcoind.service
+                s cp $src /etc/systemd/system
+                p=/etc/bitcoin/bitcoin
+                dst=/etc/systemd/system/bitcoinjm.service
+                # jm for joinmarket
+                $sed -r "/^\s*ExecStart/s,${p}.conf,${p}jm.conf," $src \
+                     >/etc/systemd/system/bitcoinjm.service
+
+                d=jm; jm=d # being clever for succinctness
+                for s in d jm; do
+                    s $sed -ri "/^\s*\[Unit\]/a Conflicts=bitcoin${!s}.service" \
+                      /etc/systemd/system/bitcoin${s}.service
+                done
+
+                ser daemon-reload
+
+                dir=/nocow/.bitcoin
+                s mkdir -p $dir
+                s chown -R bitcoin:bitcoin $dir
+                dir=/etc/bitcoin
+                s mkdir -p $dir
+                s chown -R root:bitcoin $dir
+                s chmod 750 $dir
+
+                # pruning decreases the bitcoin dir to 2 gb, keeps
+                # just the recent blocks. can\'t do a few things like
+                # import a wallet dump.
+                # pruning works, but people had to do
+                # some manual stuff in joinmarket. I dun need the
+                # disk space, so not bothering yet, maybe in a year or so.
+                # https://github.com/JoinMarket-Org/joinmarket/issues/431
+                #https://bitcoin.org/en/release/v0.12.0#wallet-pruning
+                #prune=550
+
+                f=$dir/bitcoin.conf
+                s dd of=$f <<EOF
+server=1
+rpcpassword=$(openssl rand -base64 32)
+rpcuser=$(openssl rand -base64 32)
+EOF
 
-case $distro in
-    fedora) pi unrar ;;
-    *) pi unrar-free ;;
-esac
 
+                f2=$dir/bitcoinjm.conf
+                s cp $f $f2
+                s tee -a $f2 >/dev/null <<EOF
+# Joinmarket
+walletnotify=curl -sI --connect-timeout 1 http://localhost:62602/walletnotify?%s
+alertnotify=curl -sI --connect-timeout 1 http://localhost:62602/alertnotify?%s
+wallet=joinmarket.dat
+EOF
+                # dunno about sharing a wallet between multiple instances
+                # manually did, wallet.dat symlinked in /nocow/.bitcoin
+                sgo bitcoind
+            fi
+            ;;
+        # other distros unknown
+    esac
+    pi libsodium-dev python-pip
+    cd /a/opt/joinmarket
+    # using develop branch, as it seems to be mostly bug fixes,
+    # and this is quite new software.
+    # note: python3 does not work.
+    # has seg fault error due to some bug, but it still works
+    pip install -r requirements.txt || [[ $? == 139 ]]
+    # note, the target must exist ahead of time, or bitcoin
+    # just overwrites the link, and it\'s not happy with an empty file,
+    # so we have to create the wallet, then move and link it.
+    s lnf -T /q/bitcoin/wallet.dat /nocow/.bitcoin/wallet.dat
+    s lnf -T /q/bitcoin/joinmarket.dat /nocow/.bitcoin/joinmarket.dat
+    # not technically needed, but seems cleaner not to have
+    # symlinks be root owned unlike everything else
+    s chown -h bitcoin:bitcoin /nocow/.bitcoin/*
+
+    for var in rpcuser rpcpassword; do
+        u="$(s sed -rn "s/^$var=(.*)/\1/p" /etc/bitcoin/bitcoin.conf)"
+        # escape backslashes
+        u="${u//\\/\\\\\\\\}"
+        # escape commas
+        u="${u//,/\\,}"
+        sed -ri "s,^(rpc_${var#rpc}\s*=).*,\1 $u," joinmarket.cfg
+    done
+    sed -ri "s/^\s*(blockchain_source\s*=).*/\1 bitcoin-rpc/" joinmarket.cfg
 
-# proprietary flash. going without for now
-# case $distro in
-#     debian)
-#         pi flashplugin-nonfree
-# esac
+fi
 
-case $distro in
-    debian) pi curl ;;
-    arch) : ;;
-    # fedora: unknown
-esac
 
 
 case $distro in
@@ -312,27 +1293,130 @@ case $distro in
     arch)
         pi  python2-pygments
         ;;
-    debian|ubuntu)
+    debian|ubuntu|trisquel)
         pi python-pygments
         ;;
 esac
 
 
-# leave this for last so it doesn't do a bunch of other apps
-# which I want explicitly installed in case I switch DE's
 case $distro in
     debian)
         pi task-cinnamon-desktop
         # in settings, change scrolling to two-finger,
         # because the default edge scroll doesn\'t work.
+        pu transmission-gtk
         ;;
     # others unknown
 esac
 
+case $distro in
+    arch) spa apg  ;;
+
+    # already in debian jessie
+esac
+
+
+
+
+
+# note this failed running at the beginning of this file,
+# because no systemd user instance was running.
+# Doing systemd --user resulted in
+# Trying to run as user instance, but $XDG_RUNTIME_DIR is not set
+
+if isdebian-testing; then
+    # as of 7/2016, has no unstable deps, and is not in testing anymore.
+    pi synergy/unstable
+else
+    pi synergy
+fi
+
+# case $distro in
+#     # ubuntu unknown. probably the same as debian, just check if the
+#     # init scripts come with the package.
+#     debian)
+#         # copied from arch, but moved to etc
+#         s dd of=/etc/systemd/user/synergys.service <<'EOF'
+# [Unit]
+# Description=Synergy Server Daemon
+# After=network.target
+
+# [Service]
+# User=%i
+# ExecStart=/usr/bin/synergys --no-daemon --config /etc/synergy.conf
+# Restart=on-failure
+
+# [Install]
+# WantedBy=multi-user.target
+# EOF
+#         s dd of=/etc/systemd/user/synergys.socket <<'EOF'
+# [Unit]
+# Conflicts=synergys@.service
+
+# [Socket]
+# ListenStream=24800
+# Accept=false
+
+# [Install]
+# WantedBy=sockets.target
+# EOF
+#         # had this fail with 'Failed to connect to bus: No such file or directory'
+#         # then when I tried it manually, it worked fine...
+#         if ! systemctl --user daemon-reload; then
+#             sleep 2
+#             echo retrying systemd user daemon reload
+#             systemctl --user daemon-reload
+#         fi
+#         ;;&
+#     *)
+#         # taken from arch wiki.
+#         s dd of=/etc/systemd/system/synergyc@.service <<'EOF'
+# [Unit]
+# Description=Synergy Client
+# After=network.target
+
+# [Service]
+# User=%i
+# ExecStart=/usr/bin/synergyc --no-daemon frodo
+# Restart=on-failure
+# # per man systemd.unit, StartLimitInterval, by default we
+# # restart more than 5 times in 10 seconds.
+# # And this param defaults too 200 miliseconds.
+# RestartSec=3s
+
+# [Install]
+# WantedBy=multi-user.target
+# EOF
+#         s systemctl daemon-reload
+#         case $HOSTNAME in
+#             x2|treetowl)
+#                 ser enable synergyc@ian
+#                 ser start synergyc@ian ||: # X might not be running yet
+#                 ;;
+#             frodo)
+#                 systemctl --user start synergys ||:
+#                 systemctl --user enable synergys
+#                 ;;
+#         esac
+#         ;;
+# esac
+
+
+
+### kdeconnect for gnome. started in /a/bin/distro-setup/desktop-20-autostart.sh
+pi libgtk-3-dev python3-requests-oauthlib valac cmake python-nautilus
+cd /a/opt/indicator-kdeconnect
+mkdir -p build
+cd build
+cmake .. -DCMAKE_INSTALL_PREFIX=/usr
+make
+sudo make install
+
+
 ######### end misc packages #########
 
 
-# packages I once used before and liked, but don't want installed now for
+# packages I once used before and liked, but don\'t want installed now for
 # various reasons:
 # python-sqlite is used for offlineimap
 # lxappearance python-sqlite dolphin paman dconf-editor
@@ -342,6 +1426,8 @@ esac
 ######## unfinished
 
 # todo, finish configuring smart.
+
+pi smartmontools
 # mostly from https://wiki.archlinux.org/index.php/S.M.A.R.T.
 # turn on smart. background on options:
 # first line, -a = test everyting on all devices.
@@ -350,8 +1436,8 @@ esac
 # -o on, turn on 4 hour period non-performance degrading testing.
 # short test daily 2-3am, extended tests Saturdays between 3-4am:
 sched="-s (S/../.././02|L/../../6/03)"
-s sed -i "s#^[[:space:]]*DEVICESCAN.*#\
-DEVICESCAN -a -o on -S on -n standby,q $sched\
+s sed -i --follow-symlinks "s#^[[:space:]]*DEVICESCAN.*#\
+DEVICESCAN -a -o on -S on -n standby,q $sched \
 -m ian@iankelling.org -M exec /usr/local/bin/smart-notify#" /etc/smartd.conf
 
 # in the default configuration of at least ubuntu 14.04, resolvconf is
@@ -360,7 +1446,7 @@ DEVICESCAN -a -o on -S on -n standby,q $sched\
 # consulted first. This is often slower and undesirable, ie. local dns
 # queries go from 0ms to 10+ or 100+ ms. To reverse the ordering, you
 # can do:
-#sudo sed -i '/tun\*\|tap\*/d' /etc/resolvconf/interface-order
+#sudo sed -i --follow-symlinks '/tun\*\|tap\*/d' /etc/resolvconf/interface-order
 # however, this breaks dns lookup for hosts on the openvpn lan.
 # I can\'t figure out why hosts on the normal lan would not be
 # broken under the default ordering, except the host I was
@@ -368,151 +1454,104 @@ DEVICESCAN -a -o on -S on -n standby,q $sched\
 
 ############# end unfinished
 
-case $distro in
-    arch)
-        # default is alsa, doesn\'t work with with pianobar
-        s dd of=/etc/libao.conf <<'EOF'
-default_driver=pulse
+########### misc stuff
+
+devs=()
+for dev in $(s btrfs fi show /boot | sed -nr 's#.*path\s+(\S+)$#\1#p'); do
+    devs+=($(devbyid $dev),)
+done
+devs[-1]=${devs[-1]%,} # jonied by commas
+
+# on grub upgrade, we get prompts unless we do this
+s debconf-set-selections <<EOF
+grub-pc        grub-pc/install_devices multiselect ${devs[*]}
 EOF
-        ;;
-esac
 
 
-case $distro in
-    arch|debian|ubuntu) pi btrbk ;;
-    # others unknown
-esac
+# the wiki backup script from ofswiki.org uses generic paths
+s lnf /p/c/machine_specific/li/mw_vars /root
+s lnf /k/backup/wiki_backup /root
 
-if [[ $HOSTNAME == treetowl ]]; then
-    pi fail2ban
-    sgo fail2ban
-fi
+s cedit /etc/goaccess.conf <<'EOF' || [[ $? == 1 ]]
+# all things found from looking around the default config
+# copied existing NCSA Combined Log Format with Virtual Host, plus %L
+log-format %^:%^ %h %^[%d:%t %^] "%r" %s %b "%R" "%u" %D
+time-format %H:%M:%S
+date-format %d/%b/%Y
+log-file /var/log/apache2/access.log
+color-scheme 2
 
+# tip: copy access.log files to a stretch host directory, then run
+# jessie's goaccess is too old for some options, and it\'s
+# not easily installed from a testing.
+# goaccess --ignore-crawlers -f <(cat *) -a -o html > x.html
+EOF
 
 
-# disable motd junk.
-case $(distro-name) in
-    debian)
-        # allows me to pipe with ssh -t, and gets rid of spam
-        # http://forums.debian.net/viewtopic.php?f=5&t=85822
-        # i'd rather disable the service than comment the init file
-        # this says disabling the service, it will still get restarted
-        # but this script doesn't do anything on restart, so it should be fine
-        s dd of=/var/run/motd.dynamic if=/dev/null
-        s update-rc.d motd disable
+case $distro in
+    debian|ubuntu|trisquel)
+        case `debian-archive` in
+            stable)
+                s dd of=/etc/apt/preferences.d/unison-gtk <<'EOF'
+Explanation: Allow unison-gtk to be upgraded
+Package: unison-gtk
+Pin: release a=testing
+Pin-Priority: 500
+EOF
+                # dont think using testing is needed since I figured out how to
+                # deal with mismatching unison compilers, but I dont
+                # see any reason to revert it, since it only installs
+                # a single package which is primarily a single binary
+                ;;
+        esac
+        pi unison/testing
+        pi unison-gtk/testing # after to make it the default unison
         ;;
-    ubuntu)
-        # this isn't a complete solution. It still shows me when updates are available,
-        # but it's no big deal.
-        s t /etc/update-motd.d/10-help-text /etc/update-motd.d/00-header
+    arch)
+        pi unison gtk2
         ;;
 esac
 
-# automatic updates
-# reference:
-# https://debian-handbook.info/browse/stable/sect.regular-upgrades.html
-# /etc/cron.daily/apt calls unattended-upgrades
-# /usr/share/doc/unattended-upgrades# cat README.md
-# /etc/apt/apt.conf.d/50unattended-upgrades
-if isdebian; then
-    pi unattended-upgrades
-    s dd of=/etc/apt/apt.conf.d/10periodic <<'EOF'
-# this file was mostly just comments.
-APT::Periodic::Update-Package-Lists "1";
-APT::Periodic::Download-Upgradeable-Packages "1";
-APT::Periodic::AutocleanInterval "7";
-APT::Periodic::Unattended-Upgrade "1";
-EOF
-    { cat  <<'EOF'
-Unattended-Upgrade::Mail "root";
-Unattended-Upgrade::MailOnlyOnError "true";
-Unattended-Upgrade::Remove-Unused-Dependencies "true";
-Unattended-Upgrade::Origins-Pattern {
-# default is just upgrade main and security, not updates.
-EOF
-      if isdebian-testing; then
-          cat <<'EOF'
-# for stable, only do security updates.
-       "origin=Debian,codename=${distro_codename},label=Debian-Security";
-EOF
-          cat <<'EOF'
-# These are stable packages only getting bugfixes anyways.
-       "origin=*";
-EOF
-          cat <<'EOF'
-};
-EOF
-      fi
-    } | s dd of=/etc/apt/apt.conf.d/50unattended-upgrades
-    echo $- > /tmp/x
-fi
-
-
+case $distro in
+    arch)
+        # default is alsa, doesn\'t work with with pianobar
+        s dd of=/etc/libao.conf <<'EOF'
+default_driver=pulse
+EOF
+        ;;
+esac
 
-######### begin  postfix ########
-# based on,http://www.postfix.org/qmgr.8.html and my notes in gnus
-# originally tried moving specific directories under /var/spool/postfix,
-# but postfix didn't like that
-if [[ ! -L /var/spool/postfix ]]; then
-    ser stop postfix
-    if [[ -e /q/postfix ]]; then
-        echo "$0: error: /q/postfix exists but not the link to it"
-    fi
-    s mv /var/spool/postfix /q
-    s lnf /q/postfix /var/spool
-    ser start postfix
-    journalctl -n 20
+# note, for jessie, it depends on a higher version of btrfs-tools.
+#
+# # disabled due to my patch being in btrbk
+# case $distro in
+#     arch|debian|ubuntu|trisquel) pi btrbk ;;
+#     # others unknown
+# esac
+cd /a/opt/btrbk
+s make install
+spa pv # for progress bar when running interactively.
+if [[ $HOSTNAME == treetowl ]]; then
+    # backup/sync manually on others hosts for now.
+    sgo btrbk.timer
+    # note: to see when it was last run,
+    # ser list-timers
 fi
 
-
-# This also works instead of ~/.forward
-# s sed -i '/^root/d' /etc/aliases ||:
-#echo "root: $HOSTNAME@bog.mm.st" | s tee -a /etc/aliases
-# this can't be a symlink and has permission restrictions
-# it might work in /etc/aliases, but this seems more proper.
-
-if s grep amazonaws /etc/postfix/sasl_passwd &>/dev/null; then
-    forward=x@sallymae.club
-else
-    forward=$HOSTNAME@bog.mm.st
+if [[ $HOSTNAME == treetowl ]] && [[ `debian-archive` != testing ]]; then
+    # fail2 ban is broken, with a workaround, per
+    # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=770171
+    # ill wait a while to see if it gets fixed
+    pi fail2ban
+    sgo fail2ban
 fi
-e $forward > ~/.forward
-e $forward | s tee /root/.forward
-s newaliases
-
-# if I wanted the from address to be renamed and sent to a different address,
-# echo "sdx@localhost development@localhost" | sudo dd of=/etc/postfix/recipient_canonical
-# sudo postmap hash:/etc/postfix/recipient_canonical
-# sudo service postfix reload
 
 
-# i'm assuming mail just won't work on systems without the sasl_passwd.
-postconfin <<'EOF'
-smtp_sasl_auth_enable = yes
-smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
-smtp_sasl_security_options = noanonymous
-smtp_tls_security_level = secure
-message_size_limit =  20480000
-smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
-EOF
-# ^ I ran into a log file not sending cuz of size. double from 10 to 20 meg limit
 
-s postmap hash:/etc/postfix/sasl_passwd
-# offlineimap uses this too, it is much easier to use one location than to
-# condition it's config and postfix's config
-case $distro in
-    fedora) s lnf -T ca-certificates.crt /etc/ssl/ca-bundle.trust.crt ;;
-    *) :
-esac
-
-s service postfix reload
-sgo postfix
-
-############ end postfix #######
 
 
 case $distro in
-    debian|ubuntu) s gpasswd -a ian adm ;; #needed for reading logs
+    debian|ubuntu|trisquel) s gpasswd -a ian adm ;; #needed for reading logs
 esac
 
 # tor
@@ -590,32 +1629,242 @@ EOF
         pi nfs-utils
         sgo nfs-server
         ;;
-    debian|ubuntu)
+    debian|ubuntu|trisquel)
         pi nfs-server
         ;;
     arch)
-        pi nfs-utils
+        pi nfs-utils || pending_reboot=true
         sgo rpcbind
+        # this failed until I rebooted
         sgo nfs-server
         ;;
 esac
 
-if [[ -e /i/video ]]; then
+
+########### begin kodi setup ############
+pi kodi
+
+# based on https://wiki.debian.org/SecuringNFS
+# but the quota stuff is either outdated or optional,
+# i guessed that it was not needed and it worked fine.
+s dd of=/etc/sysctl.d/nfs-static-ports.conf <<'EOF'
+fs.nfs.nfs_callback_tcpport = 32764
+fs.nfs.nlm_tcpport = 32768
+fs.nfs.nlm_udpport = 32768
+EOF
+s sysctl --system
+s $sed -ri -f - /etc/default/nfs-common <<'EOF'
+/^\s*STATDOPTS=/d
+$a STATDOPTS="--port 32765 --outgoing-port 32766"
+EOF
+
+s $sed -ri -f - /etc/default/nfs-kernel-server <<'EOF'
+/^\s*RPCMOUNTDOPTS=/d
+$a RPCMOUNTDOPTS="--manage-gids --port 32767"
+EOF
+ser restart nfs-kernel-server
+
+if [[ $HOSTNAME == treetowl ]]; then
+    # persistent one time steps for webdav:
+    # create persistent password, put it in ~/.kodi/userdata/advancedsettings.xml,
+    # per http://kodi.wiki/view/MySQL/Sync_other_parts_of_Kodi
+    # htpasswd -c /p/c/filesystem/etc/davpass dav
+    # chmod 640 /p/c/filesystem/etc/davpass
+    # in conflink, set group to www-data.
+    # In kodi, i set the music source, server address: my domain,
+    # path: k/music. Then copied the file
+    # /p/c/subdir_files/.kodi/userdata/sources.xml to save that setting.
+    s a2enmod dav dav_fs
+    web-conf -r /a/c/playlists - apache2 dav.$HOME_DOMAIN <<'EOF'
+<Directory /a/c/playlists>
+    DAV On
+  AuthType Basic
+  AuthName "Authentication Required"
+  AuthUserFile "/etc/davpass"
+  Require valid-user
+
+# outside the standard /var/www, so use this:
+  Order allow,deny
+  Allow from all
+</Directory>
+EOF
+    s mkdir -p /var/www/davlock
+    s chown www-data:www-data /var/www/davlock
+    s sed -i "1i DavLockDB /var/www/davlock/davlock" /etc/apache2/sites-enabled/dav.$HOME_DOMAIN.conf
+    ser reload apache2
+
+    teeu /etc/exports "/k/music *(ro,nohide,async,no_subtree_check,insecure)"
+    exportfs -ra
+
+    # kodi uses sqlite by default, but supports mysql.
+    pi mariadb-server
+
+    # see ofswiki.org for explanation.
+    dbpass="$(cat /p/mysql-root-pass)"
+    if ! echo exit|mysql -uroot "-p$dbpass"; then
+        echo -e "\n\n$dbpass\n$dbpass\n\n\n\n\n" | mysql_secure_installation
+    fi
+    mysql -uroot "-p$dbpass" <<EOF
+GRANT ALL PRIVILEGES ON *.* TO 'kodi' IDENTIFIED BY '$(</p/mysql-kodi-pass)';
+EOF
+    s sed -ri 's/^(\s*bind-address\s*=).*/\1 0.0.0.0/' /etc/mysql/mariadb.conf.d/50-server.cnf
+    ser restart mariadb
+
+fi
+
+###########  end kodi setup ############
+
+
+if [[ $HOSTNAME == treetowl ]]; then
     # nohide = export filesystems mounted deeper than the export point
     # fsid=0 makes this export the "root" export
     # not documented in the man page, but this means
     # 1. it can be mounted with a shorthand of server:/
     # 2. exports that are subdirectories of this one will automatically be mounted
-    tu /etc/exports '/i/video 192.168.1.0/24(rw,fsid=0,nohide,no_root_squash,async,no_subtree_check,insecure)'
+    tu /etc/exports <<'EOF'
+/k 192.168.1.0/24(rw,fsid=0,nohide,no_root_squash,async,no_subtree_check,insecure)
+EOF
     s exportfs -rav
-    showmount -e localhost
 fi
 
 
-# cron
-f=/a/bin/$HOSTNAME-crontab
-if [[ -e $f ]]; then
-    $f
+e "$end_msg_var"
+
+
+# persistent virtual machines
+
+case $distro in
+    debian|ubuntu|trisquel)
+        pi libosinfo-bin;
+        ;;
+esac
+
+# distro may not know about win 10 yet.
+variant=win7
+if ! virt-install --os-variant list &>/dev/null; then # we are using a newer virt-install
+    for v in 10 8.1 8; do
+        if osinfo-query os | gr "^\s*win${v/./\\.}\s" &>/dev/null; then
+            variant=win$v
+            break
+        fi
+    done
 fi
 
-e "$end_msg_var"
+if ! s virsh list --all --name | grep -xF win10 &>/dev/null; then
+
+    # created account with
+    # win10vmian@outlook.com, and easy to remember password
+    # win 10 virtio, makes disk way way way faster
+    # wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-virtio/virtio-win.iso
+    # https://wiki.archlinux.org/index.php/QEMU#Change_Existing_Windows_VM_to_use_virtio
+    # for installing virtio after initial install instead of with initial iso:
+    # qemu-img create -f qcow2 fake.qcow2 1G
+    #    --disk=/a/images/virtio-win.iso,device=cdrom \
+        #          --disk=/a/images/fake.qcow2,bus=virtio
+    #  Also,
+    #   went to device manager, saw 2 pci devices with yellow !,
+    #   did search for drivers, pick  cdrom location, done.
+    #
+    # from http://www.tenforums.com/tutorials/4189-fast-startup-turn-off-windows-10-a.html.
+    # google said there was a control panel option for it, but
+    # that turned out to be a lie.
+    # Put this in a .bat file and run as administrator to turn off
+    # hyberboot which fucks things up.
+    # REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /V HiberbootEnabled /T REG_dWORD /D 0 /F
+    # power settings, turn off display: never
+    # run "control userpasswords2", turn on automatic login.
+    # note: when changing devices, I just undefine, the create the vm again.
+
+    if [[ -e /nocow/user/vms/win10.qcow2 ]]; then
+        s virt-install --noautoconsole --graphics spice,listen=0.0.0.0 \
+          --disk=/a/images/win10.qcow2,bus=virtio --vcpus 2 -r 4096 -w bridge=br0 \
+          -n win10 --import --os-variant $variant --cpu host-model-only
+
+        s virsh destroy win10
+    fi
+
+    if [[ -e /nocow/user/vms/win7.qcow2 ]]; then
+        # this one hasn\'t had the virtio fix done yet.
+        s virt-install --noautoconsole --graphics spice,listen=0.0.0.0 \
+          --disk=/a/images/win7.qcow2 --vcpus 2 -r 4096 -w bridge=br0 \
+          -n win7 --import --os-variant win7 --cpu host-model-only
+        s virsh destroy win7
+        # had a problem with --cpu host, so trying out
+        # --cpu host-model-only
+    fi
+fi
+
+
+if [[ $HOSTNAME == treetowl ]]; then
+    pi samba
+    # note samba re-reads it\'s config every 1 minute
+    case $distro in
+        arch) s cp /etc/samba/smb.conf.default /etc/samba/smb.conf ;;
+    esac
+
+    # add 2 lines after workgroup option
+    s sed -ri --follow-symlinks '/^\s*encrypt passwords\s*=/d' /etc/samba/smb.conf
+    s sed -ri --follow-symlinks '/^\s*map to guest\s*=/d' /etc/samba/smb.conf
+    s sed -i --follow-symlinks 's/\(\s*workgroup\s*=\).*/\1 WORKGROUP\n\tencrypt passwords = yes\n\tmap to guest = bad password/'  /etc/samba/smb.conf
+    # remove default homes section. not sharing that.
+    s sed -ri --follow-symlinks '/^\s*\[homes\]/,/\s*\[/d' /etc/samba/smb.conf
+
+    if ! grep -xF '[public]' /etc/samba/smb.conf &>/dev/null; then
+        s tee -a /etc/samba/smb.conf <<'EOF'
+[public]
+      guest ok = yes
+      read only = no
+      path = /kr
+EOF
+    fi
+
+    case $distro in
+        debian|ubuntu|trisquel)
+            # systemd claims it generates units from /etc/init.d, but it
+            # clearly doesn\'t in debian. I have no idea how they are
+            # related. fuck debian right now. It\'s not documented.  samba
+            # has a systemd init file linked to /dev/null.  There\'s this
+            # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=769714 which
+            # claims samba\'s sub-services will be started automatically by
+            # systemd... it didn\'t on install, wonder if it will on
+            # boot. It clued me in how to start it manually though. Nothing
+            # in /usr/share/doc/samba, debian admin guide says nothing about
+            # any of this. (this is in debian testing as of 4/2016).
+
+            s /etc/init.d/samba start
+            ;;
+        arch)
+            sgo samba
+            ;;
+    esac
+fi
+
+tu /etc/hosts <<< "127.0.1.1 $(hostname).lan $(hostname)"
+
+
+######### begin stuff belonging at the end    ##########
+
+
+# Apps we want to override others for default file handler:
+# simplest way in debian is to just install them last.
+simple_packages+=(
+    mpv
+)
+
+case $distro in
+    ubuntu|debian)
+        spa spacefm-gtk3 ;;
+    arch)
+        spa spacefm ;;
+esac
+
+
+pi "${simple_packages[@]}"
+
+
+if $pending_reboot; then
+    echo "$0: pending reboot and then finished. doing it now."
+    s reboot now
+else
+    echo "$0: $(date): ending now)"
+fi