Advertisement
nyptus

indexgen.sh

Feb 20th, 2023
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.89 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. USERNAME='git'
  4. HOSTNAME=$(hostname -I | xargs | awk '{print $1;}')
  5. SERVER=$USERNAME'@'$HOSTNAME
  6. WORKDIR='repos'
  7. WORKDIRPATH='/home/repos'
  8. INDEXDIR='/var/www/html/'$WORKDIR
  9. INDEX_HTML=$INDEXDIR'/index.html'
  10. CURRENT_TIME=$(date +"%x %r %Z")
  11. TIME_STAMP="Generated $CURRENT_TIME"
  12. TITLE="Local Git Repo"
  13.  
  14. function readme_listing () {
  15.     cd $1
  16.     READMEBLOB=$(su -c "git ls-tree -r HEAD 2>/dev/null | grep -i  README.md" $USERNAME)
  17.     if test -z "$READMEBLOB"
  18.     then
  19.         #echo "\$README is NULL"
  20.         CONTENT='NO README FILE'
  21.     else
  22.         #echo "\$README is NOT NULL"
  23.         READMEHASH=$(echo $READMEBLOB | awk '{print $3}')
  24.         CONTENT=$(su -c "git cat-file -p $READMEHASH 2>/dev/null | sed -n '3p'" $USERNAME)
  25.     fi
  26.     echo $CONTENT
  27.    return
  28. }
  29.  
  30. function repos_listing () {
  31. cd $WORKDIRPATH
  32. for FILE in *; do
  33.     RML='This is not a repo'
  34.     if [[ -d $FILE ]]
  35.     then
  36.         RML=$(readme_listing "$FILE")
  37.     fi
  38. cat << __EOF__
  39.                 <tr><td onclick="c2cb('$FILE')">$FILE</td>
  40.                 <td onclick="c2cb('$SERVER:$WORKDIRPATH/$FILE')"> $WORKDIRPATH/$FILE</td>
  41.                 <td>$RML</td></tr>
  42. __EOF__
  43.         done
  44.     return
  45. }
  46.  
  47. function create_indexdir () {
  48.     $(sudo mkdir -p $INDEXDIR && sudo chmod 777 $INDEXDIR)
  49.     $(mkdir -p $INDEXDIR/css $INDEXDIR/js)
  50. cat <<CSS > "$INDEXDIR/css/style.css"
  51. @charset "utf-8";
  52. body{
  53.     margin: 0px;
  54.     color: #f5f0e1;
  55.     background-color: #1e3d59;
  56.     font-size: 100%;
  57. }
  58.  
  59. table, td, th{
  60.         font-size: 100%;
  61.     border: 1px solid #12a4d9;
  62.         border-collapse: collapse;
  63. }
  64.  
  65. table.center{
  66.         margin-left: auto;
  67.         margin-right: auto;
  68. }
  69.  
  70. h1, p{
  71.         text-align: center;
  72. }
  73. CSS
  74.  
  75. cat <<JS > "$INDEXDIR/js/script.js"
  76. function c2cb(ele){
  77.                 var tempTxt=document.createElement("input");
  78.                 tempTxt.value=ele;
  79.                 document.body.appendChild(tempTxt);
  80.                 tempTxt.select();
  81.                 document.execCommand("copy");
  82.                 document.body.removeChild(tempTxt);
  83.                 //alert("Copied: "+tempTxt.value);
  84. }
  85. JS
  86.     return
  87. }
  88.  
  89. function generate_html () {
  90. cd $WORKDIRPATH
  91. cat << __EOF__ > $INDEX_HTML
  92. <!DOCTYPE html>
  93. <html>
  94.         <head>
  95.         <meta name="viewport" content="width=device-width, initial-scale=1">
  96.         <title>$TITLE</title>
  97.         <link  rel="stylesheet"  href="./css/style.css">
  98.         </head>
  99.         <body>
  100.                 <h1>$TITLE</h1>
  101.                 <p>$TIME_STAMP</p>
  102.                 <div>
  103.                 <table class="center">
  104.                         $(repos_listing)
  105.                 </table>
  106.                 </div>
  107.         <script src="js/script.js"></script>
  108.         </body>
  109. </html>
  110. __EOF__
  111.  
  112.     return
  113. }
  114.  
  115. if [[ -d "$INDEXDIR" ]]; then
  116.     cd $WORKDIRPATH
  117.     $(generate_html)
  118. else
  119.     cd $WORKDIRPATH
  120.     $(create_indexdir)
  121.     $(generate_html)
  122. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement