11#! /usr/bin/env bash
22
3+ # Ensure that the common script exists and is readable, then verify it has no
4+ # syntax errors and defines the required function.
5+ common_script=" $( dirname " $0 " ) /common.sh"
6+ [ -r " $common_script " ] || { echo " [!] '$common_script ' not found or not readable." >&2 ; exit 1; }
7+ bash -n " $common_script " > /dev/null 2>&1 || { echo " [!] '$common_script ' contains syntax errors." >&2 ; exit 1; }
8+ source " $common_script "
9+ declare -F set_colors > /dev/null 2>&1 || { echo " [!] '$common_script ' does not define the required function." >&2 ; exit 1; }
10+
11+ set_colors
12+
13+ TOTAL_STEPS=5
14+ CURRENT_STEP=0
15+
16+ # 1. Validate the workspace
17+ (( CURRENT_STEP++ ))
18+ progress " $CURRENT_STEP " " $TOTAL_STEPS "
19+
320if ! test -d .git; then
4- echo " Execute scripts/install-git-hooks in the top-level directory."
5- exit 1
21+ throw " Execute scripts/install-git-hooks in the top-level directory."
22+ fi
23+
24+ workspace=$( git rev-parse --show-toplevel)
25+ if [ ! -d " $workspace " ]; then
26+ throw " The workspace path '$workspace ' contains non-ASCII characters."
627fi
728
29+ # 2. Check GitHub account
30+ (( CURRENT_STEP++ ))
31+ progress " $CURRENT_STEP " " $TOTAL_STEPS "
32+
833ACCOUNT=$( git config -l | grep -w remote.origin.url | sed -e ' s/^.*github.com[\/:]\(.*\)\/lab0-c.*/\1/' )
934
1035CURL=$( which curl)
1136if [ $? -ne 0 ]; then
12- echo " [!] curl not installed." >&2
13- exit 1
37+ throw " curl not installed."
1438fi
1539
1640CURL_RES=$( ${CURL} -s \
@@ -19,44 +43,51 @@ https://api.github.com/repos/${ACCOUNT}/lab0-c/actions/workflows)
1943
2044TOTAL_COUNT=$( echo ${CURL_RES} | sed -e ' s/.*"total_count": \([^,"]*\).*/\1/' )
2145case ${TOTAL_COUNT} in
22- * " Not Found" * )
23- echo " Check your repository. It should be located at https://github.com/${ACCOUNT} /lab0-c"
24- exit 1
46+ * " Not Found" * )
47+ throw " Check your repository. It should be located at https://github.com/${ACCOUNT} /lab0-c"
2548esac
2649
50+ # 3. Ensure this repository is frok from sysprog21/lab0-c'.
51+ (( CURRENT_STEP++ ))
52+ progress " $CURRENT_STEP " " $TOTAL_STEPS "
53+
2754if [[ " ${ACCOUNT} " != " sysprog21" ]]; then
28- REPO_FORKED=$( ${CURL} -s \
29- -H " Accept: application/vnd.github.v3+json" \
30- https://api.github.com/repos/${ACCOUNT} /lab0-c | grep -m 1 fork)
31- case ${REPO_FORKED} in
32- * true* )
33- ;;
34- * )
35- echo " Your repository MUST be forked from sysprog21/lab0-c"
36- exit 1
37- esac
55+ REPO_FORKED=$( ${CURL} -s \
56+ -H " Accept: application/vnd.github.v3+json" \
57+ https://api.github.com/repos/${ACCOUNT} /lab0-c | grep -m 1 fork)
58+ case ${REPO_FORKED} in
59+ * true* )
60+ ;;
61+ * )
62+ throw " Your repository MUST be forked from 'sysprog21/lab0-c'."
63+ esac
3864fi
3965
66+ # 4. Check GitHub Actions
67+ (( CURRENT_STEP++ ))
68+ progress " $CURRENT_STEP " " $TOTAL_STEPS "
69+
4070if [ ${TOTAL_COUNT} -eq " 0" ]; then
41- echo " Fatal: GitHub Actions MUST be activated."
42- case ${OSTYPE} in
43- " linux" * )
44- xdg-open https://github.com/${ACCOUNT} /lab0-c/actions > /dev/null 2>&1
45- ;;
46- " darwin" * )
47- open https://github.com/${ACCOUNT} /lab0-c/actions
48- ;;
49- * )
50- echo " Please activate at https://github.com/${ACCOUNT} /lab0-c/actions"
51- ;;
52- esac
53- echo " Check this article: https://docs.github.com/en/actions/managing-workflow-runs/disabling-and-enabling-a-workflow"
54- echo " Then, execute 'make' again."
55- exit 1
56- else
57- echo " GitHub Actions has been activated"
71+ printf " \n[!] GitHub Actions MUST be activated."
72+ case ${OSTYPE} in
73+ " linux" * )
74+ xdg-open https://github.com/${ACCOUNT} /lab0-c/actions > /dev/null 2>&1
75+ ;;
76+ " darwin" * )
77+ open https://github.com/${ACCOUNT} /lab0-c/actions
78+ ;;
79+ * )
80+ echo " Please activate at https://github.com/${ACCOUNT} /lab0-c/actions"
81+ ;;
82+ esac
83+ throw " Check this article: https://docs.github.com/en/actions/managing-workflow-runs/disabling-and-enabling-a-workflow\n\
84+ Then, execute 'make' again."
5885fi
5986
87+ # 5. Install Git hooks
88+ (( CURRENT_STEP++ ))
89+ progress " $CURRENT_STEP " " $TOTAL_STEPS "
90+
6091mkdir -p .git/hooks
6192
6293ln -sf ../../scripts/pre-commit.hook .git/hooks/pre-commit || exit 1
0 commit comments