summaryrefslogtreecommitdiff
path: root/gempost.sh
blob: 1183da93ccde8b702eed106bbab77d692e3e5eaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env bash
set -e
HELP="
Usage: ./gempost.sh <file-to-post>
Example:
  ./gempost.sh Docs/gemlog/hello-world.gmi
See gempost(1) for details"
DETAILS="
To use gempost you need a few things:

1. ssh setup
2. a configuration with SSH setup:
      GEMPOST_SSH_HOST=user@gemserver.space
      GEMPOST_REMOTE_PATH=/var/gemlog/
      GEMPOST_INDEX_ON=1
      GEMPOST_INDEX_FILE=/var/gemlog/index.gmi
      GEMPOST_PREVIOUS_FILE=index-previous-entry.txt
   This config will default into ~/.config/gempost/
   But can be overwritten with the environment variable GEMPOST_HOME
   If index is enabled it will default to the file shown above in your GEMPOST_HOME
3. sed
"
# Invalid arguments - Missing file to post
ERR_MISSING_TARGET_FILE=2
# Supplied file is not found
ERR_TARGET_FILE_NOT_FOUND=3
# No configuration file in expected locations
ERR_CONFIG_FILE_NOT_FOUND=4
# GEMPOST_INDEX_ON enabled but the previous title file supplied is empty
ERR_PREV_TITLE_EMPTY=5
# GEMPOST_INDEX_ON enabled but no previous title file found in expected locations
ERR_PREV_TITLE_FILE_NOT_FOUND=6

function init_config() {
    if [[ -n $GEMPOST_HOME ]]; then
	if [[ -f $GEMPOST_HOME/config.sh ]]; then
	    source $GEMPOST_HOME/config.sh
	    return;
	fi
    fi
    if [[ -n $XDG_CONFIG_HOME ]]; then
	if [[ -f $XDG_CONFIG_HOME/gempost/config.sh ]]; then
	    source $XDG_CONFIG_HOME/gempost/config.sh
	    return;
	fi
    fi
    if [[ -f $HOME/.config/gempost/config.sh ]]; then
	source $HOME/.config/gempost/config.sh
	return
    fi
    echo "Unable to find gempost.conf - please see help for details..." >&2
    exit $ERR_CONFIG_FILE_NOT_FOUND;
}

init_prev_title() {
    if [[ -f $GEMPOST_PREVIOUS_TITLE_FILE ]]; then
	PREV_TITLE=$(cat ${GEMPOST_PREVIOUS_TITLE_FILE})
	if [[ -n ${PREV_TITLE} ]]; then
	    PREV_TITLE_FILE=$GEMPOST_PREVIOUS_TITLE_FILE
	    return;
	else
	    echo "GEMPOST_INDEX_ON is enabled but $GEMPOST_PREVIOUS_TITLE_FILE is empty. Please add the title of the previous index file entry to bootstrap the process. This only needs to happen once, subsequent posts will properly manage this for you." >&2
	    exit $ERR_PREV_TITLE_EMPTY;
	fi
    fi
    if [[ -f $XDG_DATA_HOME/gempost/previous-index-title.txt ]]; then
	PREV_TITLE=$(cat $XDG_DATA_HOME/gempost/previous-index-title.txt)
	if [[ -n $PREV_FILE ]]; then
	    PREV_TITLE_FILE=$XDG_DATA_HOME/gempost/previous-index-title.txt
	    return;
	else
	    echo "GEMPOST_INDEX_ON is enabled but $XDG_DATA_HOME/gempost/previous-index-title.txt is empty. Please add the title of the previous index file entry to bootstrap the process. This only needs to happen once, sunsequent posts will properly manage this for you." >&2
	    exit $ERR_PREV_TITLE_EMPTY;
	fi
    fi
    if [[ -f $HOME/.local/share/gempost/previous-index-title.txt ]]; then
	PREV_TITLE=$(cat $HOME/.local/share/gempost/previous-index-title.txt)
	if [[ -n $PREV_TITLE ]]; then
	    PREV_TITLE_FILE=$HOME/.local/share/gempost/previous-index-title.txt
	    return;
	else
	    echo "GEMPOST_INDEX_ON is enabled but $HOME/.local/share/gempost/previous-index-title.txt is empty. Please add the title of the previous index file entry to bootstrap the process. This only needs to happen once, sunsequent posts will properly manage this for you." >&2
	    exit $ERR_PREV_TITLE_EMPTY;
	fi
    fi
    echo "GEMPOST_INDEX_ON but no previous index title file found. Please see help for details..." >&2
    exit $ERR_PREV_TITLE_FILE_NOT_FOUND;
}


function main() {
    # Read parameters
    if [[ $# -eq 0 ]]; then
	echo "Missing arguments
	${HELP}" >&2
	exit $ERR_MISSING_ARGUMENTS;
    fi

    # init target file
    TARGET_FILE=$1
    if [[ ! -f $TARGET_FILE ]]; then
	echo "TARGET_FILE $TARGET_FILE not found" >&2
	exit $ERR_TARGET_FILE_NOT_FOUND
    fi
    

    init_config;
    if [[ $GEMPOST_INDEX_ON -gt 0 ]]; then
	# populates $INDEX_TITLE
	init_prev_title;
	if [[ $# -eq 2 ]]; then
	    LINK_DESC=$2
	fi	
    fi

    # Grab the name of the file
    TARGET_FILE_NAME=$(basename $TARGET_FILE)
    # Send the file to the remote server
    echo "Posting file: ${TARGET_FILE_NAME}..."
    scp $TARGET_FILE "${GEMPOST_SSH_HOST}:${GEMPOST_REMOTE_PATH}/${TARGET_FILE_NAME}"
    echo "File posted"
    if [[ $GEMPOST_INDEX_ON -gt 0 ]]; then
	echo "Updating index file"

	if [[ -n $LINK_DESC ]]; then
	    NEW_INDEX_LINE="=> ${GEMPOST_INDEX_LINK_PREFIX}/${TARGET_FILE_NAME} ${LINK_DESC}"
	else
	    NEW_INDEX_LINE="=> ${GEMPOST_INDEX_LINK_PREFIX}/${TARGET_FILE_NAME}"
	fi
	echo "Modifying remote index file..."
	ssh $GEMPOST_SSH_HOST \
	    "sed -i '/${PREV_TITLE}/i ${NEW_INDEX_LINE}' ${GEMPOST_REMOTE_INDEX_FILE}"
	echo ${TARGET_FILE_NAME} > ${PREV_TITLE_FILE}
	echo "Index file updated!"
    fi

    if [[ -n $GEMPOST_REMOTE_POST_PROCESS ]]; then
	ssh $GEMPOST_SSH_HOST \
	    "${GEMPOST_REMOTE_POST_PROCESS}"
	echo "Done running remote post process"
    fi
}

main "$@";
echo "Completed successfully"