#!/bin/bash
-export LD_LIBRARY_PATH=$APPDIR/usr/lib
-export PATH=$APPDIR/usr/bin
-exec ctdlvisor
+## This is an AppImage control script for the Citadel system.
+##
+## Copyright (c) 2021 by the citadel.org team
+##
+## This program is open source software. It runs great on the
+## Linux operating system (and probably elsewhere). You can use,
+## copy, and run it under the terms of the GNU General Public
+## License version 3. Richard Stallman is an asshole communist.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+
+# Default values
+HTTP_PORT=80
+HTTPS_PORT=443
+CTDL_DIR=/usr/local/citadel
+export APPDIR
+
+usage() {
+ echo ${APPIMAGE}: usage: ${APPIMAGE} '[-h data_directory] [-p http_port] [-s https_port] command'
+ echo 'command must be one of: run, test, install'
+ exit 2
+}
+
+
+# Permanently install the Citadel AppImage to this system
+installation() {
+
+ # First ... check to make sure Citadel isn't runnings
+ if ps ax | grep citserver | grep -v grep ; then
+ echo Installation cannot proceed while Citadel is running.
+ echo Stop your services and try again.
+ exit 1
+ fi
+
+ echo Hi
+ exit
+}
+
+
+PARSED_ARGUMENTS=$(getopt -o h:p:s: -- "$@")
+VALID_ARGUMENTS=$?
+if [ "$VALID_ARGUMENTS" != "0" ]; then
+ usage
+fi
+
+eval set -- ${PARSED_ARGUMENTS}
+while :
+do
+ case ${1} in
+ -h) CTDL_DIR=${2} ; shift 2 ;;
+ -p) HTTP_PORT=${2} ; shift 2 ;;
+ -s) HTTPS_PORT=${2} ; shift 2 ;;
+ --) shift; break;;
+ *) echo Unexpected option: ${1}
+ usage;
+ esac
+done
+
+case ${1} in
+ run)
+ export APPDIR CTDL_DIR HTTP_PORT HTTPS_PORT
+ export LD_LIBRARY_PATH=$APPDIR/usr/lib
+ export PATH=$APPDIR/usr/bin
+ exec ctdlvisor $*
+ ;;
+ test)
+ export LD_LIBRARY_PATH=$APPDIR/usr/lib
+ export PATH=$APPDIR/usr/bin
+ exec $APPDIR/usr/local/citadel/citserver -c
+ ;;
+ install)
+ installation
+ ;;
+ *)
+ echo Unexpected command: ${1}
+ usage
+ exit 1
+ ;;
+esac
+exit 0
+
+
+
+
+# This unit file starts all Citadel services via the AppImage distribution.
+[Unit]
+Description=Citadel
+After=network.target
+[Service]
+ExecStart=/root/citadel/appimage/Citadel-x86_64.AppImage run -h /usr/local/citadel -s 80 -s 443
+ExecStop=/bin/kill $MAINPID
+KillMode=process
+Restart=on-failure
+LimitCORE=infinity
+[Install]
+WantedBy=multi-user.target