github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/hack/make/generate-index-listing (about)

     1  #!/bin/bash
     2  set -e
     3  
     4  # This script generates index files for the directory structure
     5  # of the apt and yum repos
     6  
     7  : ${DOCKER_RELEASE_DIR:=$DEST}
     8  APTDIR=$DOCKER_RELEASE_DIR/apt
     9  YUMDIR=$DOCKER_RELEASE_DIR/yum
    10  
    11  if [ ! -d $APTDIR ] && [ ! -d $YUMDIR ]; then
    12  	echo >&2 'release-rpm or release-deb must be run before generate-index-listing'
    13  	exit 1
    14  fi
    15  
    16  create_index() {
    17  	local directory=$1
    18  	local original=$2
    19  	local cleaned=${directory#$original}
    20  
    21  	# the index file to create
    22  	local index_file="${directory}/index"
    23  
    24  	# cd into dir & touch the index file
    25  	cd $directory
    26  	touch $index_file
    27  
    28  	# print the html header
    29  	cat <<-EOF > "$index_file"
    30  	<!DOCTYPE html>
    31  	<html>
    32  	<head><title>Index of ${cleaned}/</title></head>
    33  	<body bgcolor="white">
    34  	<h1>Index of ${cleaned}/</h1><hr>
    35  	<pre><a href="../">../</a>
    36  	EOF
    37  
    38  	# start of content output
    39  	(
    40  	# change IFS locally within subshell so the for loop saves line correctly to L var
    41  	IFS=$'\n';
    42  
    43  	# pretty sweet, will mimick the normal apache output
    44  	for L in $(find -L . -mount -depth -maxdepth 1 -type f ! -name 'index' -printf "<a href=\"%f\">%f|@_@%Td-%Tb-%TY %Tk:%TM  @%f@\n"|sort|column -t -s '|' | sed 's,\([\ ]\+\)@_@,</a>\1,g');
    45  	do
    46  		# file
    47  		F=$(sed -e 's,^.*@\([^@]\+\)@.*$,\1,g'<<<"$L");
    48  
    49  		# file with file size
    50  		F=$(du -bh $F | cut -f1);
    51  
    52  		# output with correct format
    53  		sed -e 's,\ @.*$, '"$F"',g'<<<"$L";
    54  	done;
    55  	) >> $index_file;
    56  
    57  	# now output a list of all directories in this dir (maxdepth 1) other than '.' outputting in a sorted manner exactly like apache
    58  	find -L . -mount -depth -maxdepth 1 -type d ! -name '.' -printf "<a href=\"%f\">%-43f@_@%Td-%Tb-%TY %Tk:%TM  -\n"|sort -d|sed 's,\([\ ]\+\)@_@,/</a>\1,g' >> $index_file
    59  
    60  	# print the footer html
    61  	echo "</pre><hr></body></html>" >> $index_file
    62  
    63  }
    64  
    65  get_dirs() {
    66  	local directory=$1
    67  
    68  	for d in `find ${directory} -type d`; do
    69  		create_index $d $directory
    70  	done
    71  }
    72  
    73  get_dirs $APTDIR
    74  get_dirs $YUMDIR