Skip to content

Commit

Permalink
detect origin and warn user before forcing update + fix few things
Browse files Browse the repository at this point in the history
also fix download of maps with redirected URL
  • Loading branch information
jgigault authored Aug 10, 2018
2 parents d70e8e9 + 8479ad1 commit 10877f7
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 62 deletions.
8 changes: 6 additions & 2 deletions 42MapGenerator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ fi
GLOBAL_INSTALLDIR=$(mapgen_install_dir)
cd "${GLOBAL_INSTALLDIR}"

GLOBAL_VERSION=2
GLOBAL_DISK_USAGE=
GLOBAL_LOCALBRANCH=$(git branch | awk '$0 ~ /^\*/ {print $2; exit}')
MAPGENERATOR_SH=1
RETURNPATH=$(pwd | sed 's/ /\ /g')
CVERSION=$(git log --oneline 2>/dev/null | awk 'END {printf NR}' | sed 's/ //g')
if [ "$CVERSION" == "" ]; then CVERSION="???"; fi
GLOBAL_CVERSION=$(git log --oneline 2>/dev/null | awk 'END {printf NR}' | sed 's/ //g')
if [ -z "${GLOBAL_CVERSION}" ]; then GLOBAL_CVERSION="???"; fi


source includes/maps.sh
Expand All @@ -66,6 +69,7 @@ source includes/display_section.sh
source includes/display_error.sh
source includes/display_success.sh
source includes/display_right.sh
source includes/display_leftandright.sh
source includes/display_spinner.sh
source includes/generate_map.sh
source includes/download_map.sh
Expand Down
2 changes: 1 addition & 1 deletion includes/data_providers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ then

DATA_PROVIDERS[${#DATA_PROVIDERS[*]}]="MGDS (Marine Geoscience Data System / USA)"
DATA_PROVIDERS[${#DATA_PROVIDERS[*]}]="Horizontal accuracy up to 100 meters"
DATA_PROVIDERS[${#DATA_PROVIDERS[*]}]="http://www.marine-geo.org/services/GridServer?minlongitude=MAPGEN_WEST&maxlongitude=MAPGEN_EAST&minlatitude=MAPGEN_SOUTH&maxlatitude=MAPGEN_NORTH&format=esriascii&layer=topo&resolution=high"
DATA_PROVIDERS[${#DATA_PROVIDERS[*]}]="https://www.gmrt.org/services/GridServer?minlongitude=MAPGEN_WEST&maxlongitude=MAPGEN_EAST&minlatitude=MAPGEN_SOUTH&maxlatitude=MAPGEN_NORTH&format=esriascii&layer=topo&resolution=high"
DATA_PROVIDERS[${#DATA_PROVIDERS[*]}]="ocean_topography_available"
DATA_PROVIDERS[${#DATA_PROVIDERS[*]}]="MGDS"

Expand Down
2 changes: 1 addition & 1 deletion includes/display_center.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ then
printf "%"${MARGIN}"s" " "
printf "$1"
(( MARGIN= ${MARGIN} + (${COLUMNS} - ${LEN} - ${MARGIN} * 2) ))
printf "%"${MARGIN}"s" " "
printf "%"${MARGIN}"s\n" " "
else
printf "\n"
fi
Expand Down
25 changes: 17 additions & 8 deletions includes/display_header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ then

function display_header
{
local MARGIN
local MARGIN COLOR

utils_clear
utils_set_env
if [ "$1" != "" ]
COLOR=${1}
if [ -z "${COLOR}" ]
then
COLOR="${C_INVERTGREY}"
fi
if [ "${GLOBAL_LOCALBRANCH}" != "master" ]
then
printf "$1"
display_leftandright "${COLOR}" "${COLOR}" "${COLOR}" "DEVELOPMENT MODE" "${GLOBAL_LOCALBRANCH}"
else
printf ${C_INVERT}""
if [ "${GLOBAL_DISK_USAGE}" -gt "100" ]
then
display_leftandright "${C_INVERTGREY}" "${COLOR}" "${COLOR}" "DISK USAGE: ${GLOBAL_DISK_USAGE}M" "PRESS ESCAPE TO EXIT - V${GLOBAL_VERSION}.r${GLOBAL_CVERSION}"
else
display_leftandright "${COLOR}" "${COLOR}" "${COLOR}" "DISK USAGE: ${GLOBAL_DISK_USAGE}M" "PRESS ESCAPE TO EXIT - V${GLOBAL_VERSION}.r${GLOBAL_CVERSION}"
fi
fi
display_righttitle "PRESS ESCAPE TO EXIT - V2.r${CVERSION}"
display_center " _ _ ____ __ __ ____ "
display_center " | || ||___ \| \/ | __ _ _ __ / ___| ___ _ __ "
display_center " | || |_ __) | |\/| |/ _\ | '_ \| | _ / _ \ '_ \ "
display_center " |__ _/ __/| | | | (_| | |_) | |_| | __/ | | | "
display_center " |_||_____|_| |_|\__,_| .__/ \____|\___|_| |_| "
display_center " jgigault @ student.42.fr |_| 06 51 15 98 82 "
display_center " "
printf "${C_CLEAR}\n\n"
display_center " |_| "
printf "${C_CLEAR}\n"
}

fi
19 changes: 19 additions & 0 deletions includes/display_leftandright.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

if [ "${MAPGENERATOR_SH}" == "1" ]
then

function display_leftandright
{
local COLORLEFT="${1}" COLORCENTER="${2}" COLORRIGHT="${3}" TEXTLEFT="${4}" TEXTRIGHT="${5}" LEN

LEN="$(( ${COLUMNS} - ${#TEXTLEFT} - ${#TEXTRIGHT} ))"
if [ "${LEN}" -ge "0" ]
then
printf "${COLORLEFT}%s${COLORCENTER}% ${LEN}s${COLORRIGHT}%s${C_CLEAR}\n" "${TEXTLEFT}" " " "${TEXTRIGHT}"
else
printf "${COLORLEFT}%- ${COLUMNS}s\n${COLORRIGHT}% ${COLUMNS}s${C_CLEAR}\n" "${TEXTLEFT}" "${TEXTRIGHT}"
fi
}

fi
2 changes: 1 addition & 1 deletion includes/display_menu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ then
MENU[$TOTAL]="${!MAPSA}"
if [ "${MAPS[$((${MAPSA} * ${MAPS_SHIFT} + 3))]}" != "" ]
then
TITLE=`echo "${MAPS[$((${MAPSA} * ${MAPS_SHIFT}))]} (${MAPS[$((${MAPSA} * ${MAPS_SHIFT} + 3))]})" | sed 's/%/%%/g'`
TITLE=`echo "${MAPS[$((${MAPSA} * ${MAPS_SHIFT}))]} (Download size: ${MAPS[$((${MAPSA} * ${MAPS_SHIFT} + 3))]})" | sed 's/%/%%/g'`
else
TITLE=`echo "${MAPS[$((${MAPSA} * ${MAPS_SHIFT}))]}" | sed 's/%/%%/g'`
fi
Expand Down
2 changes: 1 addition & 1 deletion includes/display_spinner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ then

function display_spinner
{
local pid=$1 total_delay=0 total_delay2=65 delay=0.2 spinstr='|/-\' SEL
local pid=$1 total_delay=0 total_delay2=130 delay=0.2 spinstr='|/-\' SEL
CURRENT_CHILD_PROCESS_PID="${pid}"
printf "${C_BLUE}"
while [ "$(ps a | awk -v pid="${pid}" '$1 == pid {print $1}')" ];
Expand Down
8 changes: 4 additions & 4 deletions includes/download_map.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ then
display_section
printf "${C_BLUE} %s\n %s\n\n" "Downloading $1 from remote server..." "$2"
sleep 0.5
(curl --show-error --output "${MAPS_TMPDIR}$1" "$2") &
(curl --location --show-error --output "${MAPS_TMPDIR}$1" "$2") &
display_spinner $!
printf $C_CLEAR""
if [ ! -f "${MAPS_TMPDIR}$1" -o "$?" != "0" ]
Expand Down Expand Up @@ -156,13 +156,13 @@ then
HEADER_NCOLS="$(awk 'BEGIN{OFS=""; ORS=""} $1 == "ncols" {print $2/1; exit}' "${MAPS_TMPDIR}$1")"
HEADER_NROWS="$(awk 'BEGIN{OFS=""; ORS=""} $1 == "nrows" {print $2/1; exit}' "${MAPS_TMPDIR}$1")"
REAL_NCOLS="$(awk 'BEGIN{OFS=""; ORS=""} NF > 2 {print NF/1; exit}' "${MAPS_TMPDIR}$1")"
#REAL_NROWS="$(awk 'BEGIN{OFS=""; ORS=""; TOTAL=0} NF > 2 {TOTAL+=1} END {print TOTAL/1}' "${MAPS_TMPDIR}$1")"
REAL_NROWS="$(awk 'BEGIN{OFS=""; ORS=""; TOTAL=0} NF > 2 {TOTAL+=1} END {print TOTAL/1}' "${MAPS_TMPDIR}$1")"
if [ "${HEADER_NCOLS}" == "" ]; then HEADER_NCOLS=0; fi
if [ "${HEADER_NROWS}" == "" ]; then HEADER_NROWS=0; fi
if [ "${REAL_NCOLS}" == "" ]; then REAL_NCOLS=0; fi
#if [ "${REAL_NROWS}" == "" ]; then REAL_NROWS=0; fi
if [ "${REAL_NROWS}" == "" ]; then REAL_NROWS=0; fi
(( DIFF_NCOLS= "${HEADER_NCOLS}" - "${REAL_NCOLS}" ))
#(( DIFF_NROWS= "${HEADER_NROWS}" - "${REAL_NROWS}" ))
(( DIFF_NROWS= "${HEADER_NROWS}" - "${REAL_NROWS}" ))
if [[ "${HEADER_NCOLS}" -le "0" || "${HEADER_NROWS}" -le "0" || "${DIFF_NCOLS}" -lt -3 || "${DIFF_NCOLS}" -gt 3 ]] # || "${DIFF_NROWS}" -lt -3 || "${DIFF_NROWS}" -gt 3
then
rm -f "${MAPS_TMPDIR}$1"
Expand Down
Empty file removed includes/formats.sh
Empty file.
23 changes: 17 additions & 6 deletions includes/generate_map.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ then
PREVIEWLINK="http://www.marine-geo.org/tools/gmrt_image_1.php?maptool=1\&north=$(utils_maps_get_coor "NORTH" "${MAPS_ID}")\&west=$(utils_maps_get_coor "WEST" "${MAPS_ID}")\&east=$(utils_maps_get_coor "EAST" "${MAPS_ID}")\&south=$(utils_maps_get_coor "SOUTH" "${MAPS_ID}")\&mask=0"
display_menu\
"" "Select a format:"\
"select_map_format XXL" "XXL (original data)"\
"select_map_format XXL" "XXL (raw data)"\
"select_map_format XL" "XL (reduce factor: 2)"\
"select_map_format L" "L (reduce factor: 4)"\
"select_map_format M" "M (reduce factor: 8)"\
Expand All @@ -23,7 +23,7 @@ then
else
display_menu\
"" "Select a format:"\
"select_map_format XXL" "XXL (original data)"\
"select_map_format XXL" "XXL (raw data)"\
"select_map_format XL" "XL (reduce factor: 2)"\
"select_map_format L" "L (reduce factor: 4)"\
"select_map_format M" "M (reduce factor: 8)"\
Expand Down Expand Up @@ -201,13 +201,24 @@ then
display_success "Export was successful!"
display_success "Map size: ${EXPORTED_NCOLS} x ${EXPORTED_NROWS} (width x height)"
display_success "File size: ${EXPORTED_FILESIZE}"
printf "\n"
display_menu\
"" ""\
"${RETURNFUNCTION}" "OK"\
generate_map_open_export_directory "OPEN EXPORT DIRECTORY"
else
display_error "An error occured, no file exported"
printf "\n"
display_menu\
"" ""\
"${RETURNFUNCTION}" "OK"
fi
printf "\n"
display_menu\
"" ""\
"${RETURNFUNCTION}" "OK"
}

function generate_map_open_export_directory
{
open "${MY_EXPORT_PATH}"
${RETURNFUNCTION}
}

fi
2 changes: 1 addition & 1 deletion includes/google_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ then
fi
printf "${C_WHITE}"
tput cnorm
read -p " Type a ${ADDRESSTYPE} name: " -e TITLE
read -p " Type a ${ADDRESSTYPE} name (in English): " -e TITLE
tput civis
printf "${C_CLEAR}"
if [ "${TITLE}" == "" ]
Expand Down
2 changes: 1 addition & 1 deletion includes/maps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ then
MAPS[${#MAPS[*]}]=""
MAPS[${#MAPS[*]}]=""
MAPS[${#MAPS[*]}]=""
MAPS[${#MAPS[*]}]="http://pubs.usgs.gov/of/2006/1367/dems/Arc_grids/ULCN2005_grid.txt.gz"
MAPS[${#MAPS[*]}]="https://pubs.usgs.gov/of/2006/1367/dems/Arc_grids/ULCN2005_grid.txt.gz"

MAPS[${#MAPS[*]}]="Venus"
MAPS[${#MAPS[*]}]="JoliotCurie_preliminary%JoliotCurie_mrg.asc"
Expand Down
102 changes: 66 additions & 36 deletions includes/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,70 @@ then
}

function utils_update_check
{ if [ "${OPT_NO_UPDATE}" == "0" ]; then
local UPTODATE VERSION RET0 RET1 LOCALHASH REMOTEHASH LOCALBRANCH
{ if [ "${OPT_NO_UPDATE}" == "0" -a "${GLOBAL_LOCALBRANCH}" != "master" ]; then
local FORCE_UPDATE UPTODATE VERSION RET0 RET1 LOCALHASH REMOTEHASH REMOTEFETCHURL TOPLEVELINSTALLDIR

FORCE_UPDATE=${1}
REMOTEFETCHURL=$(git remote get-url origin)
TOPLEVELINSTALLDIR=$(git rev-parse --show-toplevel)
display_header
printf "${C_BLUE} %s\n" "Checking for updates..."
(utils_update_check_diff > .myret) &
display_spinner $!
UPTODATE=`cat .myret`
git fetch origin
if [ "${?}" == "0" ]
then
(utils_update_check_fetchurl > .myret && utils_update_check_diff > .myret) &
display_spinner $!
UPTODATE=`cat .myret`
else
UPTODATE="ERR3"
fi
case "${UPTODATE}" in
"1")
printf "continue" > .myret
"OK")
printf "continue" > .myret
;;
"3")
"ERR3")
display_header "${C_INVERTRED}"
display_error "Cannot check for updates: Your Internet connection is probably down..."
display_error "Cannot check for updates because fetching origin has failed..."
display_error "Use option '--no-update' at launch to avoid this step."
printf "\n"
display_menu\
"${C_INVERTRED}" ""\
"printf 'continue' > .myret" "SKIP UPDATE"\
"printf 'exit' > .myret" "EXIT"
;;
"0")
LOCALBRANCH=$(git branch | grep '^\*' | cut -d" " -f2)
LOCALHASH=`git show-ref | grep "refs/heads/${LOCALBRANCH}" | cut -d" " -f1 | awk '{print; exit}'`
REMOTEHASH=`git ls-remote 2>/dev/null | grep refs/heads/${LOCALBRANCH} | cut -f1 | awk '{print; exit}'`
CVERSION=$(git log --oneline "refs/heads/${LOCALBRANCH}" | awk 'END {printf NR}' | sed 's/ //g')
VERSION=$(git log --oneline "refs/remotes/origin/${LOCALBRANCH}" | awk 'END {printf NR}' | sed 's/ //g')
"ERR2")
display_header "${C_INVERTRED}"
display_error "42MapGenerator tried to update your local installation but it is not linked to the original repository \"${CONFIG_ORIGINAL_REPOSITORY_ID}\" on \"${CONFIG_ORIGINAL_REPOSITORY_HOST}\", but to the following remote URL:"
display_error " "
display_error "Remote directory: ${REMOTEFETCHURL}"
display_error "Local directory: ${TOPLEVELINSTALLDIR}"
display_error " "
display_error "If you confirm the update, it will erase your local directory with the remote one."
printf "\n"
display_menu\
"${C_INVERTRED}" ""\
"utils_update_check 1" "YES, CONTINUE UPDATE AND ERASE LOCAL DIRECTORY"\
"printf 'skip' > .myret" "NO, SKIP UPDATE"\
"printf 'exit' > .myret" "EXIT"
;;
"ERR1")
LOCALHASH=`git show-ref | grep "refs/heads/${GLOBAL_LOCALBRANCH}" | cut -d" " -f1 | awk '{print; exit}'`
REMOTEHASH=`git ls-remote 2>/dev/null | grep refs/heads/${GLOBAL_LOCALBRANCH} | cut -f1 | awk '{print; exit}'`
GLOBAL_CVERSION=$(git log --oneline "refs/heads/${GLOBAL_LOCALBRANCH}" | awk 'END {printf NR}' | sed 's/ //g')
VERSION=$(git log --oneline "refs/remotes/origin/${GLOBAL_LOCALBRANCH}" | awk 'END {printf NR}' | sed 's/ //g')
display_header "${C_INVERTRED}"
printf "${C_RED}"
if [ "${REMOTEHASH}" != "${LOCALHASH}" -a "${REMOTEHASH}" != "" -a "${CVERSION}" -lt "${VERSION}" ]
if [ "${REMOTEHASH}" != "${LOCALHASH}" -a "${REMOTEHASH}" != "" -a "${GLOBAL_CVERSION}" -lt "${VERSION}" ]
then
display_center "Your version of '42MapGenerator' is out-of-date."
display_center "REMOTE: r$VERSION LOCAL: r$CVERSION"
RET1=`git log --pretty=oneline "refs/remotes/origin/${LOCALBRANCH}" 2>/dev/null | awk -v lhash="${LOCALHASH}" '{if ($1 == lhash) {exit} print}' | cut -d" " -f2- | awk 'BEGIN {LIMIT=0} {print " -> "$0; LIMIT+=1; if(LIMIT==10) {print " -> (limited to 10 last commits...)"; exit}}'`
display_center "REMOTE: r${VERSION} LOCAL: r${GLOBAL_CVERSION}"
RET1=`git log --pretty=oneline "refs/remotes/origin/${GLOBAL_LOCALBRANCH}" 2>/dev/null | awk -v lhash="${LOCALHASH}" '{if ($1 == lhash) {exit} print}' | cut -d" " -f2- | awk 'BEGIN {LIMIT=0} {print " -> "$0; LIMIT+=1; if(LIMIT==10) {print " -> (limited to 10 last commits...)"; exit}}'`
if [ "$RET1" != "" ]
then
printf "\n\n Most recent commits:\n%s\n\n" "$RET1"
fi
else
display_center "Your copy of '42MapGenerator' has been modified locally."
display_center "Your copy of '42MapGenerator' has been locally modified."
display_center "Skip update if you don't want to erase your changes."
printf "\n\n"
fi
Expand All @@ -74,36 +99,41 @@ then
fi
}

function utils_update_check_fetchurl
{
if [ -z "${FORCE_UPDATE}" ]
then
if [ "${REMOTEFETCHURL}" != "git@${CONFIG_ORIGINAL_REPOSITORY_HOST}:${CONFIG_ORIGINAL_REPOSITORY_ID}" -a "${REMOTEFETCHURL}" != "https://${CONFIG_ORIGINAL_REPOSITORY_HOST}/${CONFIG_ORIGINAL_REPOSITORY_ID}" ]
then
printf "ERR2"
return 1
fi
fi
return 0
}

function utils_update_check_diff
{
local DIFF0
local LOCALBRANCH=$(git branch | grep '^\*' | cut -d" " -f2)
DIFF0=`git fetch --all 2>&1 | tee .myret2 | grep fatal`
if [ "${DIFF0}" != "" ]

DIFF0=`git diff "refs/remotes/origin/${GLOBAL_LOCALBRANCH}" 2>&1 | grep -E '^\+|^\-' | sed 's/\"//'`
if [ "$DIFF0" != "" ]
then
printf "3"
printf "ERR1"
else
DIFF0=`git diff "refs/remotes/origin/${LOCALBRANCH}" 2>&1 | grep -E '^\+|^\-' | sed 's/\"//'`
if [ "$DIFF0" != "" ]
then
printf "0"
else
printf "1"
fi
printf "OK"
fi
}

function utils_update_install
{
local RES0 LOCALBRANCH=$(git branch | grep '^\*' | cut -d" " -f2)
local RES0
local LOGFILENAME=".myret"

display_header
printf "\n"
printf "${C_BLUE} %s\n" "Updating 42MapGenerator..."
printf "\n${C_BLUE} %s\n" "Updating 42MapGenerator..."
rm -f ${LOGFILENAME}
(git fetch --all >/dev/null 2>&1) &
display_spinner $!
(git reset --hard "origin/${LOCALBRANCH}" 2>&1 | grep -v 'HEAD is now at' >${LOGFILENAME}) &
(git reset --hard "origin/${GLOBAL_LOCALBRANCH}" 2>&1 | grep -v 'HEAD is now at' >${LOGFILENAME}) &
display_spinner $!
RES0=`cat ${LOGFILENAME}`
sleep 0.5
Expand Down
3 changes: 3 additions & 0 deletions includes/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ if [ "${MAPGENERATOR_SH}" == "1" ]
then

declare MAPS_TMPDIR="./tmp/"
declare CONFIG_ORIGINAL_REPOSITORY_HOST="github.com"
declare CONFIG_ORIGINAL_REPOSITORY_ID="jgigault/42MapGenerator.git"

function utils_set_env
{
Expand All @@ -12,6 +14,7 @@ then
then
COLUMNS=80
fi
GLOBAL_DISK_USAGE="$(du -ms | awk '$2 == "." {print $1}')"
export LC_NUMERIC=C LC_COLLATE=C LANG=C
}

Expand Down

0 comments on commit 10877f7

Please sign in to comment.